Skip to content

Commit

Permalink
Introduce JsonP support, close #1517
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephane Landelle committed Nov 14, 2013
1 parent 6b4d58e commit 969c2e3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Expand Up @@ -18,7 +18,7 @@ package io.gatling.http.check
import io.gatling.core.check.extractor.jsonpath.JsonFilter
import io.gatling.core.check.extractor.regex.GroupExtractor
import io.gatling.core.session.Expression
import io.gatling.http.check.body.{ HttpBodyCssCheckBuilder, HttpBodyJsonPathCheckBuilder, HttpBodyRegexCheckBuilder, HttpBodyStringCheckBuilder, HttpBodyXPathCheckBuilder }
import io.gatling.http.check.body.{ HttpBodyCssCheckBuilder, HttpBodyJsonPathCheckBuilder, HttpBodyJsonpJsonPathCheckBuilder, HttpBodyRegexCheckBuilder, HttpBodyStringCheckBuilder, HttpBodyXPathCheckBuilder }
import io.gatling.http.check.checksum.HttpChecksumCheckBuilder
import io.gatling.http.check.header.{ HttpHeaderCheckBuilder, HttpHeaderRegexCheckBuilder }
import io.gatling.http.check.status.HttpStatusCheckBuilder
Expand All @@ -37,6 +37,8 @@ trait HttpCheckSupport {

val jsonPath = jsonPathTyped[String] _
def jsonPathTyped[T](path: Expression[String])(implicit groupExtractor: JsonFilter[T]) = HttpBodyJsonPathCheckBuilder.jsonPath[T](path)
val jsonpJsonPath = jsonpJsonPathTyped[String] _
def jsonpJsonPathTyped[T](regex: String, path: Expression[String])(implicit groupExtractor: JsonFilter[T]) = HttpBodyJsonpJsonPathCheckBuilder.jsonpJsonPath[T](regex, path)

val bodyString = HttpBodyStringCheckBuilder.bodyString

Expand Down
@@ -0,0 +1,57 @@
/**
* Copyright 2011-2013 eBusiness Information, Groupe Excilys (www.ebusinessinformation.fr)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gatling.http.check.body

import com.typesafe.scalalogging.slf4j.Logging

import io.gatling.core.check.Preparer
import io.gatling.core.check.extractor.jsonpath.{ CountJsonPathExtractor, JsonFilter, JsonPathExtractor, MultipleJsonPathExtractor, SingleJsonPathExtractor }
import io.gatling.core.config.GatlingConfiguration.configuration
import io.gatling.core.session.Expression
import io.gatling.core.validation.{ FailureWrapper, SuccessWrapper }
import io.gatling.http.check.{ HttpCheckBuilders, HttpMultipleCheckBuilder }
import io.gatling.http.response.Response

case class JsonpPreparer(regex: String) extends Preparer[Response, Any] with Logging {

def apply(response: Response) =
try {
val bodyString = response.getResponseBody(configuration.core.encoding)
bodyString match {
case regex.r(jsonp) => JsonPathExtractor.parse(jsonp).success
case _ =>
val message = s"Regex $regex could not extract JSON object from JSONP response"
logger.info(message)
message.failure
}

} catch {
case e: Exception =>
val message = s"Could not extract JSON object from JSONP response : ${e.getMessage}"
logger.info(message, e)
message.failure
}
}

object HttpBodyJsonpJsonPathCheckBuilder extends Logging {

def jsonpJsonPath[X](regex: String, path: Expression[String])(implicit groupExtractor: JsonFilter[X]) =
new HttpMultipleCheckBuilder[Any, X](HttpCheckBuilders.bodyCheckFactory, JsonpPreparer(regex)) {
def findExtractor(occurrence: Int) = new SingleJsonPathExtractor(path, occurrence)
def findAllExtractor = new MultipleJsonPathExtractor(path)
def countExtractor = new CountJsonPathExtractor(path)
}
}

0 comments on commit 969c2e3

Please sign in to comment.