如何解决使用Gatling将Bearer令牌传递给变量
我正在学习加特林工具,并停留在开发安全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))
}
解决方法
您的令牌未得到使用,因为您将其传输到标准的scala变量,而不是仅将其传递给会话。
加特林构建器在启动时执行一次,因此当您的<html class="loggedin">
引用html.loggedin
{
// do your custom stuff in this block.
}
时,它会在发出任何请求之前一次从该var中获取值-因此{{1}的值}将为httpConf
。
由于您似乎想通过一次调用获取令牌,然后在第二种情况下所有用户都使用该令牌,因此需要将$token
var中的值加载到会话中并更新标头{{1 }}使用Gatling EL(它将从会话中提取值)
token
,
从以下链接中得到我的答案: Gatling Scala:Unable to send auth token to the method using session variable
我需要在场景中传递令牌方法,以便在会话中获取令牌。
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())
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。