Search code examples
scalagatlingscala-gatling

Build dynamic checks in Gatling


I'm looking to validate the response body dynamically. I have an endpoint which, depending on the user permissions, returns different bodies. For example:

user: a
{
   "one": "a",
   "two": "b"
}

user: b
{
   "one": "a",
   "three": "c"
}

I know that I can use jsonPath to validate if one json field exists or not in this way:

http("Request")
  .get(url)
  .header(user_a_token)
  .check(jsonPath("one").exists)
  .check(jsonPath("two").exists)
  .check(jsonPath("three").notExists)

However, I want to make it configurable, using a feeder or something like:

http("Request")
  .get(url)
  .header(user_token)
  // iterate a list of Strings with the json field names

Thoughts?


Solution

  • I finally found out a way to deal with this requirement.

    First of all, you need to define a list of checks:

    val jsonPathChecks: List[HttpCheck] = List(jsonPath("one").exists, jsonPath("two").exists, jsonPath("three").exists)
    

    And then use it:

    http("Request")
      .get(url)
      .header(user_token)
      .check(jsonPathChecks: _*)
    

    The _* operator is in charge of making the magic happens.