Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce JsonP support, close #1517
- Loading branch information
Stephane Landelle
committed
Nov 14, 2013
1 parent
6b4d58e
commit 969c2e3
Showing
2 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...ng-http/src/main/scala/io/gatling/http/check/body/HttpBodyJsonpJsonPathCheckBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |