使用馈线传递 header 值 (Gatling)

Using a feeder to pass in header values (Gatling)

我正在尝试对使用 HAWK Authentication. The issue is that the header needs to be generated for each request and pass in the key id and key. Which makes sending requests from multiple users hard. I've gotten this Hawk java library 使用硬编码密钥的服务器使用 gatling。但是,我想使用馈线模拟多个用户。我似乎无法让它工作,因为馈线没有正确传递到函数调用中。

我有以下代码:

class TestRampSimulation extends Simulation {
{
...
def generateHawk(key: String, secret: String, method: String, url: String): String = {
    val hawkCredentials: HawkCredentials = new HawkCredentials.Builder()
                                                     .keyId(key)
                                                     .key(secret)
                                                     .algorithm(HawkCredentials.Algorithm.SHA256)
                                                     .build();
    val hawkClient: HawkClient = new HawkClient.Builder().credentials(hawkCredentials).build();
    val authorizationHeader: String = hawkClient.generateAuthorizationHeader(URI.create(url), method, null, null, null, null);
    return authorizationHeader
  }

  val nbUsers = Integer.getInteger("users", 1).toInt
  val rmpTime = Integer.getInteger("ramp", 1).toInt

  val feeder = csv("tokens.csv").random

  val scn = scenario("Test API")
    .feed(feeder)
    .exec(http("[POST] /some/api/call")
      .post("/some/api/call")
      .headers(Map("Authorization" -> "".concat(generateHawk("${keyId}",
                                                      "${keySecret}",
                                                      "POST",
                                                      "http://localhost:8080/some/api/call"))))
      .check(status.is(201)))

  setUp(scn.inject(rampUsers(nbUsers) over (rmpTime seconds)).protocols(httpConf))
}

当我这样做时,密钥和机密被评估为“${keyId}”/“${keySecret}”,而不是馈线值。我正在尝试做的事情可行吗?

如果您希望每次都执行某些操作,而不仅仅是在加载场景时,则必须传递一个函数,请参阅 doc

.header("Authorization", session =>
  for {
    keyId <- session("keyId").validate[String]
    keySecret <- session("keySecret").validate[String]
  } yield generateHawk(keyId, keySecret, "POST", "http://localhost:8080/some/api/call"))

但是,我们有一个 API 用于生成此类标记,请参阅 SignatureCalculator