Gatling checkIf syntax
Asked Answered
H

1

7

A legacy app has the below working Gatling test

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

I need to change it such that either the status is 200, in which case it checks that jsonnode ID exists (just like above), or the status is 401, in which case it checks that jsonnode Description exists. Any other status should result in a failure.

This is what I have so far but it doesn't appear to be working as intended because Gatling considers all responses failures, even the ones that are 200 with ID and 401 with Description.

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

I've looked at the docs and searched around the web but haven't been able to figure out the correct way to achieve what I want, thus asking here.

Hymanhymen answered 14/6, 2018 at 23:53 Comment(3)
Try .check(status.in(200, 401)) before the subsequent checks.Leibniz
Does this answer your question? How do you use Gatling's checkIf method?Security
Just to add that import io.gatling.core.Predef.value2Success is required when using the checkIf(condition: (R, Session) => Validation[Boolean])(thenCheck: C) overloadSlumgullion
M
0

Sample code to handle this issue. Save response body or status code. Then validate response as per obtained 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
  })
Marcin answered 12/10, 2023 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.