如果未设置会话变量,如何执行Auth调用

时间:2019-04-16 18:12:07

标签: scala gatling

我正在尝试编写一些进行auth调用的负载测试,然后对场景中的每个调用使用相同的令牌。

我无法让它检查是否已设置令牌,如果没有,请致电以获取令牌。否则,将设置令牌,然后执行调用。

我执行此检查的代码如下:

val scn = scenario("Get All Events")

    .doIf(session => session("bearerToken").asOption[String].isEmpty) {
      exec(getAuth.getBearerToken)
    }
    .pause(2)
    .exec (http("Get Events")
      .get("v1/events")
      .header("x-request-id", "perf-test")
      .header("HttpLogRequestId", s"${BaseHelpers.getUUID}")
      .header("x-api-key", s"${BaseHelpers.getXAPIKey}")
      .header("Authorization", "Bearer ${bearerToken}")
      .check(status.is(200))
  )

当它调用设置身份验证令牌时,它看起来像这样:

val getBearerToken = exec(http("Get Authorization Token")
    .post(baseAuthURL + "v1/oauth2/token?grant_type=password&client_id=" + client_id + "&username=" + username + "&password=" + password)
    .basicAuth(client_id, client_secret)
    .header("x-request-id", "perf-test")
    .header("HttpLogRequestId", s"${BaseHelpers.getUUID}")
    .check(status.is(200))
    .check(jsonPath("$.id_token").saveAs("bearerToken"))
  )

对我来说,结果通常是两件事之一。要么每次都会进行此调用,要么以某种方式忽略该检查,否则它将错误地告诉我找不到密钥。

我尝试了几种不同的方法,但还是没有运气。希望再看到另一只眼睛,可能会指出我要去哪里。

1 个答案:

答案 0 :(得分:1)

sessions are private to each user - so I would expect that your scenario as written would always execute your doIf block.

The gatling EL also has support for your use case (not that it would change the result you're seeing). You could write...

doIf("${bearerToken.exists()}") {...}

But the bigger question is, what do you want your simulation to accomplish? As you've described it, you want one user to login and then simultaneously make calls to "v1/events". As you've written it, you want multiple users to each login then make that call.