Using a feeder to pass in header values (Gatling)

2019-08-08 08:08发布

问题:

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.



标签: scala gatling