首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从cookie中保存XSRF令牌,并在Gatling中的下一个请求头中将其传递给服务器

如何从cookie中保存XSRF令牌,并在Gatling中的下一个请求头中将其传递给服务器
EN

Stack Overflow用户
提问于 2020-10-19 16:34:24
回答 1查看 224关注 0票数 1

我正在尝试使用Gatling执行负载测试,并且我需要从cookie中保存X-XSRF-TOKEN并将其传递到下一个请求头中。这是我的场景:

代码语言:javascript
复制
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)
}

我想保存Request8Cookie中的X-XSRF-TOKEN并在Request9头部中传递给服务器,如何在此场景下使用check()saveAs()接口进行多次负载测试?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-19 20:18:17

使用headerheaderRegex check解析Set-Cookie标头,或使用getCookieValue action获取Cookie值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64423791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档