加特林 checkIf 语法

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

旧版应用程序具有以下有效的加特林测试

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

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

这是我到目前为止所拥有的,但它似乎没有按预期工作,因为加特林考虑了所有响应失败,即使是 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))
    )

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

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

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

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
  })
© www.soinside.com 2019 - 2024. All rights reserved.