【问题标题】:Passing Bearer token through variable using Gatling使用 Gatling 通过变量传递 Bearer 令牌
【发布时间】:2020-08-20 15:32:30
【问题描述】:

我正在学习 Gatling 工具并坚持开发用于安全 Http API 调用的场景。我创建了一个场景,我可以获取不记名令牌并将其保存在变量(令牌)中,但变量(令牌)没有在授权标头中传递其值。

这是我的代码,请查看它,

令牌变量的值不是通过以下代码行获取的, .authorizationHeader(s"Bearer $token")

================================================ =======


import io.gatling.core.Predef._

import io.gatling.http.Predef._

import scala.concurrent.duration._

import scala.collection.JavaConversions._


class SampleToken2 extends Simulation {




  //Token define
  
  private var token: String = ""
  
  val auth = scenario("Retrieve Token")
    .exec(
      http("POST Auth Req")
        .post("http://iserver:9092/login")
        .body(ElFileBody("bodies/inventalogin.json")).asJson
        .check(bodyString.saveAs("Auth_Response"))
        .check(status.is(200))
        .check(jsonPath("$.token").find.saveAs("accesskey")))
    .exec{session => { token = session("accesskey").as[String]
      session}}
  

  

  //Header Define  
  
  val httpConf = http
    .baseUrl("http://iaserver:9092")
    .authorizationHeader(s"Bearer $token")
    .acceptHeader("application/json, */*")
    .acceptCharsetHeader("UTF-8") // Here are the common headers
    .doNotTrackHeader("1")
    .acceptLanguageHeader("en-UK,en;q=0.5")
    .acceptEncodingHeader("gzip, deflate")
    .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")
    .shareConnections
    .proxy(Proxy("localhost", 8888).httpsPort(8888))


    def  myTestObjectMethod = {
      exec { session => println("token print2"); session }
      exec { session => println(token:String); session }
      exec(http("Get all devices with pagination")
        .get("/devices/getAllDevices?page=0&size=200")
        .check(status.in(200 to 210)))
        .pause(1, 20)
    }



  val scn = scenario("my_actual_load_test").exec(myTestObjectMethod)



  setUp(
    auth.inject(constantUsersPerSec(1) during (1 seconds)),
    scn.inject(nothingFor(2 seconds),
      constantUsersPerSec(50) during (300 seconds)
    )
    .protocols(httpConf))
    .assertions(global.responseTime.max.lt(500)) 
    .assertions(forAll.failedRequests.percent.lte(1)) 
    .assertions(global.responseTime.mean.lte(100))

}

【问题讨论】:

    标签: automation performance-testing gatling bearer-token web-api-testing


    【解决方案1】:

    您的令牌没有被使用,因为您将其传输到标准 scala 变量,而不是仅仅通过会话传递它。

    Gatling 构建器在启动时执行一次,因此当您的 httpConf 引用 $token 时,它会在发出任何请求之前从该 var 获取值 - 因此 token 的值将是 "" .

    由于您似乎想要一个调用来获取一个令牌,然后在第二种情况下由所有用户使用,您需要将 token var 中的值加载到会话中并更新您的标题 httpConf 以使用Gatling EL(将从会话中提取值)

    val httpConf = http
    .baseUrl("http://iaserver:9092")
    .authorizationHeader("Bearer ${token}")
    .acceptHeader("application/json, */*")
    ...
    
    def  myTestObjectMethod = {
      exec(session => session.set("token", token)
      .exec(http("Get all devices with pagination")
        .get("/devices/getAllDevices?page=0&size=200")
        .check(status.in(200 to 210))
      )
      .pause(1, 20)
    }
    

    【讨论】:

    • 感谢您的回复。我做了你指导我的事。我收到一个错误 ==> 10:36:57.562 [ERROR] i.g.h.a.HttpRequestAction - 'httpRequest-11' 未能执行:没有定义名为 'token' 的属性。如果您查看我的代码(令牌定义),我正在尝试将令牌保存在 accesskey 中,然后将 accesskey 值保存到令牌变量中。有什么方法可以在 myTestObjectMethod 中获取 accesskey 的值吗?实际上 API 由 Bearer auth 保护,我无法在 API 调用中添加/传递承载令牌。
    • 你能给我提供在 scala Gatling 中测试安全令牌承载 API 的代码吗?了解整个情况对我很有帮助。
    • @FouadIlyas 你把它保存为accesskey,你为什么用名字token访问它?
    【解决方案2】:

    从以下链接得到我的答案: Gatling Scala:Unable to send auth token to the method using session variable

    我需要在场景中传递token方法,才能在session中获取token。

    
    
      val authAPI = exec(
        exec(
          http("POST Auth API")
            .post("http://iserver:9092/login")
            .body(ElFileBody("bodies/inventalogin.json")).asJson
            .check(bodyString.saveAs("Auth_Response"))
            .check(status.is(200))
            .check(jsonPath("$.token").find.saveAs("token")))
          exec{session => { tokenAPI = session("token").as[String]
          session}}
    
    var headers_10 = Map("Content-Type" -> """application/json""", "Authorization" -> "Bearer ${token}")
    
      def  getAllDevices() = {
        exec { session => println("token print2"); session }
        exec { session => println(tokenAPI:String); session }
        exec(session => session.set("token", tokenAPI))
        exec(http("Get all devices")
          .get("/devices/getAllDevices")
          .headers(headers_10)
          .check(status.in(200 to 210)))
          //.exec { session => println(session); session }
    
          .pause(1, 20)
      }
    
    // Scenario Definition
      val scn = scenario("Basic")
        .exec(authAPI)
        .pause(1)
        .exec(getAllDevices())
        .pause(1)
        .exec(getAllDevicesPagination())
        .pause(1)
        .exec(logintest())
    
    
    

    【讨论】:

      猜你喜欢
      • 2019-08-30
      • 2019-01-18
      • 1970-01-01
      • 2018-05-26
      • 2018-11-23
      • 2012-11-02
      • 2020-10-28
      • 2014-04-19
      • 2020-03-28
      相关资源
      最近更新 更多