Accessing response bode as String for using JSON assertion frameworks to check validity

Hello Galing community!
I was trying to use JSON assertion framework( in my case JSONAssert) to check the validity of response from REST service, but unfortunately I found out that I only can use gatling’s methods such as bodyString, regex, jsonPath and other…and dont have( or just can’t find it) access to responseBody? Does anybody know whether its possible or not?

Hi,

bodyString is what you call responseBody.

Then you have to use the matchWith method and write a custom MatchStrategy.

How familiar are you with Scala?

Be aware that this API have been redesigned, so I’ll have to migrate your custom stuff once Gatling 2 is released.

Cheers,

Stéphane

Here’s an example:

import org.skyscreamer.jsonassert._
import com.excilys.ebi.gatling.core.check._
val strategy = (value: Option[String], session: Session) => value.map { actualStr =>
val result = JSONCompare.compareJSON(“expectedStr”, actualStr, JSONCompareMode.STRICT)
if (result.failed) Failure(result.getMessage)
else Success(value)
}.getOrElse(Failure(“Missing body”))

check(bodyString.matchWith(strategy))

Yes! That’s what I want! I’m familiar with Scala ( going to visit ScalaDays 2013 :slight_smile: ). Thanks for operative answer!

You’re welcome.

Be aware that I’ve been using JSONCompare and not JSONAssert so I don’t generate costly Exceptions just to trap them.
We’ll release a 2.0.0-M1 in a few weeks, so stay tuned and feel free to ask about migration then.

Have fun!

Stéphane

val jsonFileFeeder = jsonFile(“test_data.json”)
val strategy = (value: Option[String], session: Session) => value.map { jsonFileFeeder =>
val result = JSONCompare.compareJSON(“expectedStr”, jsonFileFeeder, JSONCompareMode.STRICT)
if (result.failed) Failure(result.getMessage)
else Success(value)
}.getOrElse(Failure(“Missing body”))

val login = exec(http(“Login”)
.get("/login"))
.pause(1)
.feed(feeder)
.exec(http(“authorization”)
.post("/auth")
.headers(headers_10)
.queryParam(""“email”"", “${email}”)
.queryParam(""“password”"", “${password}”)
.check(status.is(200))
.check(bodyString.matchWith(jsonFileFeeder)))
.pause(1)

I’m getting error, what I’m missing ?

value matchWith is not a member of io.gatling.core.check.DefaultFindChe
ckBuilder[io.gatling.http.check.HttpCheck,io.gatling.http.response.Response,String,String]
15:10:01.963 [ERROR] i.g.a.ZincCompiler$ - .check(bodyString.matchWith(jsonFileFeeder)))

s\lib\Login.scala:18: not found: value JSONCompare
15:10:05.224 [ERROR] i.g.a.ZincCompiler$ - val result = JSONCompare.compareJSON(jsonFileFeeder, j
sonFileFeeder, JSONCompareMode.STRICT)
^
15:10:05.631 [ERROR] i.g.a.ZincCompiler$ - two errors found
Compilation failed

matchWith doesn’t exist. What you want is “is”.
Then, you can’t directly pass a feeder. You first have to inject the expected value in the session with feed, then match against this attribute.