在加特林建立动态检查

问题描述 投票:0回答:1

我想动态验证响应体。我有一个端点,根据用户权限,返回不同的主体。例如:

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

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

我知道我可以使用jsonPath以这种方式验证是否存在一个json字段:

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

但是,我想让它可配置,使用馈线或类似的东西:

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

思考?

scala gatling scala-gatling
1个回答
1
投票

我终于找到了解决这个要求的方法。

首先,您需要定义一个检查列表:

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

然后使用它:

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

_ *操作员负责使魔法发生。

© www.soinside.com 2019 - 2024. All rights reserved.