码迷,mamicode.com
首页 > 其他好文 > 详细

Gatling - 用 session 实现关联 传递 token 值

时间:2018-01-17 00:52:39      阅读:1079      评论:0      收藏:0      [点我收藏+]

标签:cdb   相关   ext   ber   添加   $$   获得   roo   sig   

项目中的某个接口必须先登录后调用,但是 header 中的Authorization 需要在登录返回的token中添加一个字串,所以需要先获得 token 并修改后传递给该接口的请求。

虽然这是常见的关联的问题,但是由于刚开始研究gatling, 苦于中文相关文档有限,看了官方文档,也是有些茫然,尝试了不同的方法后,终于看到请求成功了。

package advisorApp

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._

class application extends Simulation {

val httpConf = http
.baseURL("https://xxxx.xxxx") // Here is the root for all relative URLs
.acceptHeader("application/json, text/plain, */*") // Here are the common headers
.acceptLanguageHeader("zh-CN,zh;q=0.9,en;q=0.8")
.acceptEncodingHeader("gzip, deflate, br")
.userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36")

val headers_10 = Map("Content-Type" -> "application/json") // Note the headers specific to a given request

val scn = scenario("SigningAdvisor")
.exec(http("login")
.post("/advisor-auth/api/auth/xxxx/_actions/loginOrRegisterUser") //test data
.headers(headers_10)
.body(StringBody("""{ "phone": "177xxxxxxxx","verificationCode":"7777"}""")).asJSON
.check(status.is(200))
.check(jsonPath("$..userId").saveAs("userId"))
.check(jsonPath("$..token").exists)
.check(jsonPath("$..token").saveAs("token"))
)

// 取出 token 值并修改
.exec(session => {
val token = session("token").as[String]
val token2 = "Bearer " + token
session.set("token",token2)
})

// 打印 session 验证正确性
.exec { session => println(session)
session
}

.exec(http("advisorSigning")
.post("/bms-advisor/api/advisors")
.headers(headers_10)
.header("Authorization","${token}")
.body(StringBody("""{"userId": "5a5d9d1f30c49e0008c1c913",
"advisorPhone": "xxxxxx", //  test data
"emailAddress": "Q@123.com",
"advisorName": "xxxx", //test data
"idCardNumber": "xxxxxxx", //test data
"nation": "Han",
"highestDegree": "doctor",
"politicalStatus": "ptgm",
"provinceCompanyId": "59f422cdb2b14f0008a1166d"}""")).asJSON)


setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))
}

打印出的session 结果:

Session(SigningAdvisor,1,Map(gatling.http.cache.dns -> io.gatling.http.resolver.ShuffleJdkNameResolver@54fba1c8, userId -> 5a5daee230c49e0008c1c915, token -> Bearer eyJhbGciOiJIUzI1NiJ9.eyJwcmluY2lwYWwiOiI1YTVkYWVlMjMwYzQ5ZTAwMDhjMWM5MTUiLCJkYXRhIjoiMzI5MDQyNDQtZTUwZC00ZmExLWIyMWYtY2MxNjM3ZTZlOGI3IiwiZXhwIjoxNTE2Njk2MDc3fQ.yJsKzVk1wkT4j6Ygdua4jpoxq1V3zzXZ8hDuk3EI4Pw),1516091277053,151,OK,List(),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda$409/115086468@687d0e8a)

Gatling - 用 session 实现关联 传递 token 值

标签:cdb   相关   ext   ber   添加   $$   获得   roo   sig   

原文地址:https://www.cnblogs.com/yuanchunli/p/8297609.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!