Using a feeder to pass in header values (Gatling)

巧了我就是萌 提交于 2019-12-11 10:55:22

问题


I'm attempting to use gatling against a server that uses 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 working with hard coded keys. However, I'd like to simulate multiple users using a feeder. I can't seem to get this working as the feeders aren't properly passed into function calls.

I have the following code:

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

When I do this, key and secret are evaluated as "${keyId}"/"${keySecret}" instead of the feeder values. Is what I'm trying to do possible?


回答1:


You have to pass a function if you want something to be executed every time, not just when the scenario is loaded, see 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"))

But then, we have an API for generating such tokens, see SignatureCalculator.



来源:https://stackoverflow.com/questions/29439235/using-a-feeder-to-pass-in-header-values-gatling

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!