如何从 cookie 中保存 XSRF 令牌并在 Gatling 的下一个请求 header 中将其传递给服务器
How can save XSRF token from cookie and pass it to server in next request header in Gatling
我正在尝试使用 Gatling 执行负载测试,我需要从 cookie 中保存 X-XSRF-TOKEN
并在下一个请求 header 中传递它。这是我的场景:
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import scala.util.Random
class addvehicle extends Simulation {
object randomIntegerGenerator {
def randomInteger(range: Int) =
scala.util.Random.nextInt(range).toString
}
val feeder = Array(
Map( "Authorization" -> "Bearer XXXX" ),
Map( "Authorization" -> "Bearer YYYY" )
).random
val httpProtocol = http
.baseUrl("https://192.168.165.176:30479")
.inferHtmlResources()
.acceptHeader("application/json, text/plain, */*")
.acceptEncodingHeader("gzip, deflate")
.acceptLanguageHeader("IR")
.userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0")
val headers_8 = Map("X-XSRF-TOKEN" -> "f50b2810-ee6a-435b-87d6-5a40dd45bbb1")
val headers_9 = Map(
"Content-Type" -> "application/json",
"Origin" -> "https://192.168.165.176:30479"
val headers_10 = Map(
"Content-Type" -> "application/json",
"Origin" -> "https://192.168.165.176:30479"
val headers_11 = Map("X-XSRF-TOKEN" -> "050e6282-febd-4452-a68b-e88fbc8d3134")
val req = s"""{"entryDate":"2020-10-03","vehicleVersion":"vehicleVersionValue"]}"""
var randomSession = Iterator.continually(
Map(
"randsession" -> (req
.replace("vehicleVersionValue", randomIntegerGenerator.randomInteger(10))
)
))
val scn = scenario("addvehicle")
.feed(feeder)
.feed(randomSession)
.pause(1)
.exec(
http("request_8")
.get("/find/usable?category=Vehicle")
.header("Authorization","${Authorization}")
.headers(headers_8))
.pause(1)
.exec(getCookieValue(CookieKey("XSRF-TOKEN").saveAs("X-XSRF-TOKEN")))
.exec(
http("request_9")
.post("/api/universal/search")
.header("Authorization","${Authorization}")
.header("X-XSRF-TOKEN","${X-XSRF-TOKEN}")
.headers(headers_9)
.body(ElFileBody("C:/Users/test/Downloads/Gatling/user-files/resources/addvehicle/0009_request.json")))
.pause(1)
.exec(getCookieValue(CookieKey("XSRF-TOKEN").saveAs("X-XSRF-TOKEN1")))
.exec(
http("request_10")
.post("/api/vehicle/create")
.header("Authorization","${Authorization}")
.header("X-XSRF-TOKEN","${X-XSRF-TOKEN1}")
.headers(headers_10)
.body(StringBody("""${randsession}"""))
.resources(http("request_11")
.get("/api/bank/find")
.header("Authorization","${Authorization}")
.headers(headers_11),
http("request_12")
.get("/api/vehicle/find?page=0&size=6&sort=createdDate,DESC")
.header("Authorization","${Authorization}")
.headers(headers_11)))
setUp(scn.inject(atOnceUsers(5))).protocols(httpProtocol)
}
我想从请求 8 cookie 中保存 X-XSRF-TOKEN
并在请求 9 header 中将其传递给服务器,如何使用 check()
和 saveAs()
API 在这种情况下执行多次负载测试?
我正在尝试使用 Gatling 执行负载测试,我需要从 cookie 中保存 X-XSRF-TOKEN
并在下一个请求 header 中传递它。这是我的场景:
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import scala.util.Random
class addvehicle extends Simulation {
object randomIntegerGenerator {
def randomInteger(range: Int) =
scala.util.Random.nextInt(range).toString
}
val feeder = Array(
Map( "Authorization" -> "Bearer XXXX" ),
Map( "Authorization" -> "Bearer YYYY" )
).random
val httpProtocol = http
.baseUrl("https://192.168.165.176:30479")
.inferHtmlResources()
.acceptHeader("application/json, text/plain, */*")
.acceptEncodingHeader("gzip, deflate")
.acceptLanguageHeader("IR")
.userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0")
val headers_8 = Map("X-XSRF-TOKEN" -> "f50b2810-ee6a-435b-87d6-5a40dd45bbb1")
val headers_9 = Map(
"Content-Type" -> "application/json",
"Origin" -> "https://192.168.165.176:30479"
val headers_10 = Map(
"Content-Type" -> "application/json",
"Origin" -> "https://192.168.165.176:30479"
val headers_11 = Map("X-XSRF-TOKEN" -> "050e6282-febd-4452-a68b-e88fbc8d3134")
val req = s"""{"entryDate":"2020-10-03","vehicleVersion":"vehicleVersionValue"]}"""
var randomSession = Iterator.continually(
Map(
"randsession" -> (req
.replace("vehicleVersionValue", randomIntegerGenerator.randomInteger(10))
)
))
val scn = scenario("addvehicle")
.feed(feeder)
.feed(randomSession)
.pause(1)
.exec(
http("request_8")
.get("/find/usable?category=Vehicle")
.header("Authorization","${Authorization}")
.headers(headers_8))
.pause(1)
.exec(getCookieValue(CookieKey("XSRF-TOKEN").saveAs("X-XSRF-TOKEN")))
.exec(
http("request_9")
.post("/api/universal/search")
.header("Authorization","${Authorization}")
.header("X-XSRF-TOKEN","${X-XSRF-TOKEN}")
.headers(headers_9)
.body(ElFileBody("C:/Users/test/Downloads/Gatling/user-files/resources/addvehicle/0009_request.json")))
.pause(1)
.exec(getCookieValue(CookieKey("XSRF-TOKEN").saveAs("X-XSRF-TOKEN1")))
.exec(
http("request_10")
.post("/api/vehicle/create")
.header("Authorization","${Authorization}")
.header("X-XSRF-TOKEN","${X-XSRF-TOKEN1}")
.headers(headers_10)
.body(StringBody("""${randsession}"""))
.resources(http("request_11")
.get("/api/bank/find")
.header("Authorization","${Authorization}")
.headers(headers_11),
http("request_12")
.get("/api/vehicle/find?page=0&size=6&sort=createdDate,DESC")
.header("Authorization","${Authorization}")
.headers(headers_11)))
setUp(scn.inject(atOnceUsers(5))).protocols(httpProtocol)
}
我想从请求 8 cookie 中保存 X-XSRF-TOKEN
并在请求 9 header 中将其传递给服务器,如何使用 check()
和 saveAs()
API 在这种情况下执行多次负载测试?