Search code examples
scalagatling

Gatling - Check if response body string key is certain value


I have a scenario in Gatling and I want to check if the response body value maps to an error string. The response is a 400:

{"error": "ERROR_1"}

The check is failing with a compilation error:

 http("Some Request")
  .put("/endpoint")
  .asJson
  .check(jsonPath("$.error") == "ERROR_1")
  .check(status.is(400))

Also tried saving the error as a variable

.check(jsonPath("$.error").saveAs("error"))
.check("${error}" == "ERROR_1")

And realised the .check("${error}".is("ERROR_1")) won't work either since .is only works for ints. The gatling docs don't explain expressions too much either https://gatling.io/docs/current/http/http_check#validating

Any ideas?


Solution

  • Your statement that .is only works for ints is incorrect - and this is how you should construct this check.

    Here's a working example with a passing check

    def test : ScenarioBuilder = scenario("test")
    .exec(
      http("test call")
        .post("http://httpbin.org/anything")
        .body(StringBody("""{"error": "ERROR_1"}"""))
        .check(jsonPath("$..error").is("ERROR_1"))
    )
    

    You can't use == as a gatling check needs one or more HttpChecks and == returns a boolean.