【发布时间】: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