Gatling checkIf 语法

fre*_*red 7 scala gatling scala-gatling

遗留应用程序具有以下工作 Gatling 测试

private val getUserByTuid = scenario("GetActiveUserInfoWrapper")
    .feed(users.toArray.circular)
    .exec(
      http("GET /v1/foo/{id}")
        .get("/v1/foo/${id}")
        .header("Content-Type", "application/json")
        .header("User-Agent", "gatling")
        .check(status is 200)
        .check(jsonPath("$..ID") exists)
    )
Run Code Online (Sandbox Code Playgroud)

我需要更改它,以便状态为 200,在这种情况下它检查 jsonnode ID 是否存在(就像上面一样),或者状态为 401,在这种情况下它检查 jsonnode Description 是否存在。任何其他状态都应导致失败。

这是我到目前为止所做的,但它似乎没有按预期工作,因为 Gatling 考虑了所有响应失败,即使是 ID 为 200 和描述为 401 的响应失败。

private val getUserByTuid = scenario("GetActiveUserInfoWrapper")
    .feed(users.toArray.circular)
    .exec(
      http("GET /v1/foo/{id}")
        .get("/v1/foo/${id}")
        .header("Content-Type", "application/json")
        .header("User-Agent", "gatling")
        .check(checkIf((response: Response, session: Session) => response.status.get.getStatusCode == 200)(jsonPath("$..ID") exists))
        .check(checkIf((response: Response, session: Session) => response.status.get.getStatusCode == 401)(jsonPath("$..Description") exists))
    )
Run Code Online (Sandbox Code Playgroud)

我查看了文档并在网上搜索,但无法找出实现我想要的正确方法,因此在这里询问。

小智 0

处理此问题的示例代码。保存响应正文或状态代码。然后根据获得的 statusCode 验证响应。

exec(
  http("User Login")
    .post("user/login/")
    .body(StringBody("{    \"email\": \"sample.com\",\n    \"password\": \"password\"\n}"))
    .check(bodyString.saveAs("responseBody"))
    .check(status.saveAs("statusCode"))
)
  .exec(session => {
    if (session("statusCode").as[String].equals("200")) {
      val token = JsonPath.read[String](session("responseBody").as[String], "$.authentication.token")
      session.set("token", token)
    }
    else if (session("statusCode").as[String].equals("401")) {
      session("responseBody").as[String].equals("Invalid email or password")
    }
    session
  })
Run Code Online (Sandbox Code Playgroud)