使用馈线传递标头值(加特林) [英] Using a feeder to pass in header values (Gatling)

查看:84
本文介绍了使用馈线传递标头值(加特林)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对使用 HAWK身份验证的服务器使用加特林.问题在于,需要为每个请求生成标头,并传递密钥ID和密钥.这使得很难从多个用户发送请求.我已经使用硬编码键来使用 Hawk Java库.但是,我想使用馈线模拟多个用户.由于供料器未正确传递到函数调用中,因此我似乎无法正常工作.

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.

我有以下代码:

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}",而不是供稿器值.我想做的事有可能吗?

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?

推荐答案

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

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

但是,我们有一个用于生成此类令牌的API,请参见

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

这篇关于使用馈线传递标头值(加特林)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆