问题描述
我在Scala中使用Gatling Ver 2.3.0.发送请求后,是否有可能将网址从重定向到变量?例如我请求192.168.1.30:8080/,此链接将我重定向到192.168.1.30:8080/token/123,我可以获取/token/123吗?我尝试使用此代码,但出现错误header.find.exists,什么都没有发现,但是在Fiddler中,我看到了此头文件
I using gatling ver 2.3.0 in scala. Is it possible after send request get a url from redirect to variable? e.g. I request for 192.168.1.30:8080/ and this link redirect me to 192.168.1.30:8080/token/123, can I get /token/123? I tried with this code but occurs error header.find.exists, found nothing but in Fiddler I see this header
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
})
推荐答案
我知道重定向存在什么问题,这是我的问题的答案:1)我应该在场景中添加httpdis .disableFollowRedirect和.check(status.is(302))
I know what is wrong with redirect this is answer on my question:1) I should add to httpConf .disableFollowRedirect and .check(status.is(302)) to scenario
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
})
这篇关于在scala中加特林如何从重定向中获取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!