Scala 中的 Gatling 如何从重定向获取 url

Mat*_*zak 6 scala gatling

我在 Scala 中使用 gatling ver 2.3.0。发送请求后是否有可能从重定向到变量获取 url?例如,我请求 192.168.1.30:8080/ 并且此链接将我重定向到 192.168.1.30:8080/token/123,我可以获得 /token/123 吗?我尝试使用此代码但发生错误 header.find.exists,除了在 Fiddler 中什么也没找到,我看到了这个标题

 val httpConf = http
        .baseURL("http://192.168.1.30:8080")
 .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
            .acceptEncodingHeader("gzip, deflate")
            .acceptLanguageHeader("en-US,en;q=0.5")
            .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")

val scn = scenario("SCENARIO2")
.exec(http("open")
  .get("/")
  .check(header("Location").saveAs("url")))
.exec(session => {
  val urlN = session.get("url").asOption[String]
  print(urlN.getOrElse("nothing"))
  session
})
Run Code Online (Sandbox Code Playgroud)

Mat*_*zak 6

我知道重定向有什么问题,这是我的问题的答案:1)我应该将 httpConf .disableFollowRedirect 和 .check(status.is(302)) 添加到场景中

val httpConf = http
    .baseURL("192.168.1.30:8080") // Here is the root for all relative URLs
   .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") // Here are the common headers
    .acceptEncodingHeader("gzip, deflate")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0")
    .disableFollowRedirect

val scn = scenario("SCENARIO2")
    .exec(http("open")
      .get("/")
      .check(status.is(302))
      .check(header("Location").saveAs("url")))
    .exec(session => {
      val urlN = session.get("url").asOption[String]
      print(urlN.getOrElse("nothing"))
      session
    })
Run Code Online (Sandbox Code Playgroud)