Gatling - 检查响应主体字符串键是否为特定值

Gatling - Check if response body string key is certain value

我在 Gatling 中有一个场景,我想检查响应正文值是否映射到错误字符串。响应是 400:

{"error": "ERROR_1"}

检查因编译错误而失败:

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

还尝试将错误保存为变量

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

并意识到 .check("${error}".is("ERROR_1")) 也不起作用,因为 .is 仅适用于整数。加特林文档也没有过多解释表达式 https://gatling.io/docs/current/http/http_check#validating

有什么想法吗?

您关于 .is 仅适用于整数的说法是不正确的 - 这就是您构建此检查的方式。

这是一个通过检查的工作示例

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"))
)

您不能使用 ==,因为 gatling 检查需要一个或多个 HttpChecks 和 == returns 一个布尔值。

试试这个:

.check(
      status.is(400),
      jsonPath("$.error").is("ERROR_1")
    )

谢谢大家的回答!似乎我错过了 .在 .error 中,所以这对我有用:

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