【问题标题】:Gatling - Check if response body string key is certain valueGatling - 检查响应正文字符串键是否为某个值
【发布时间】:2023-03-21 09:40:02
【问题描述】:

我在 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 仅适用于整数。 gatling 文档也没有过多解释表达式 https://gatling.io/docs/current/http/http_check#validating

有什么想法吗?

【问题讨论】:

    标签: scala gatling


    【解决方案1】:

    您关于 .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"))
    )
    

    您不能使用 ==,因为加特林检查需要一个或多个 HttpCheck,而 == 返回一个布尔值。

    【讨论】:

    • 请注意,递归过滤器通常比精确路径慢,因为它需要扫描整个树。如果您知道要查找的确切路径并且足够简单,则应该使用它而不是 ..
    【解决方案2】:

    试试这个:

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

    【讨论】:

      【解决方案3】:

      感谢所有答案!似乎我错过了 .在 .error 中,所以这对我有用:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-05
        相关资源
        最近更新 更多