简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM