testing - 从 Gatling 中的编码响应中提取数据
问题描述
我在从一个响应中提取数据时遇到问题,我在 Fiddler 中对其进行了检查,并收到消息表明此响应已编码: https ://i.postimg.cc/G3VfMtxh/2019-10-15-12-31-41- Progress-Telerik-Fiddler-Web-Debugger.png
当我将此内容复制到https://jsonpath.com/时,我看到 https://i.postimg.cc/rpYP4wh4/2019-10-14-22-41-19-jsonpath-online-evaluator.png
所以我尝试使用正则表达式,我在这里检查了它https://regex101.com/ https://i.postimg.cc/DZ6m6RX1/2019-10-14-22-58-57-Online-regex-tester-and -调试器-PHP-PCRE.png
它可以在在线编辑器中使用,但是脚本存在问题。
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import java.util.Base64
import io.gatling.http.response._
import java.nio.charset.StandardCharsets.UTF_8
class login2 extends Simulation {
val httpProtocol = http
.baseUrl("myapiaddress")
.inferHtmlResources()
.userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36")
.proxy(Proxy("localhost", 8888).httpsPort(8888))
val headers_0 = Map(
"Accept" -> "application/json, text/plain, */*",
"Origin" -> "mysiteaddress",
"Sec-Fetch-Mode" -> "cors")
val headers_1 = Map(
"Access-Control-Request-Headers" -> "authorization",
"Access-Control-Request-Method" -> "GET",
"Origin" -> "mysiteaddress",
"Sec-Fetch-Mode" -> "cors")
val headers_2 = Map(
"Accept" -> "application/json, text/plain, */*",
"Origin" -> "mysiteaddress",
"Sec-Fetch-Mode" -> "cors",
"authorization" -> "Bearer ${authToken}")
val headers_3 = Map("Sec-Fetch-Mode" -> "no-cors")
val headers_6 = Map(
"Origin" -> "mysiteaddress",
"Sec-Fetch-Mode" -> "cors",
"content-type" -> "application/x-www-form-urlencoded; charset=UTF-8")
val uri1 = "api/signalr"
val scn = scenario("login2")
.exec(http("request_0")
.post("/api/oauth/token")
.headers(headers_0)
.formParam("username", "yelari@malboxe.com")
.formParam("password", "Zaq1@wsx")
.formParam("grant_type", "password")
.check(jsonPath("$..access_token").exists.saveAs("authToken"))
.resources(http("request_1")
.options("/api/account")
.headers(headers_1),
http("request_2")
.get("/api/account")
.headers(headers_2),
http("request_3")
.get("/UploadedFiles/03765fee-ede8-4689-9a4c-13dd2ada18a4.png")
.headers(headers_3),
http("request_4")
.options("/api/conversations/")
.headers(headers_1),
http("request_5")
.options("/api/notifications")
.headers(headers_1),
http("request_6")
.get(uri1 + "/negotiate?clientProtocol=1.5&Authorization=Bearer%20${authToken}&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D")
//.check(jsonPath("$.ConnectionToken").exists.saveAs("MyConnectionToken"))
//.check(regex("""a-zA-Z0-9=/+{152}""").findAll.saveAs("MyConnectionToken"))
.headers(headers_6),
http("request_7")
.get("/api/notifications")
.headers(headers_2),
http("request_8")
.get("/api/conversations/")
.headers(headers_2),
http("request_9")
.get("/UploadedFiles/5899f868-8473-4dfc-a39c-f13a94c47b3a.jpeg")
.headers(headers_3),
http("request_10")
.get("/UploadedFiles/26ac4d69-8a63-4575-bec4-849d5a5e194a.png")
.headers(headers_3),
http("request_11")
.get(uri1 + "/start?transport=serverSentEvents&clientProtocol=1.5&Authorization=Bearer%20${authToken}&connectionToken=${MyConnectionToken}&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D")
.headers(headers_6)))
val printSesssionVar = scenario("print session var")
.exec{session => println(session("printSesssionVar").as[String])
session}
setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}
但是在这两种情况下我都会出错
> request_11: Failed to build request: No attribute named 'MyCon 1 (50.00%)
nectionToken' is defined
> status.find.in(200,201,202,203,204,205,206,207,208,209,304), f 1 (50.00%)
ound 404
我试图在 request_6 上提取它并在 request_11 上使用它。
是否可以处理此请求?在其他方面,我毫无问题地提取不记名令牌。
解决方案
MyConnectionToken
您已经在上面注释掉了这两个提取,但我想您已经知道了?
.exec { session =>
尝试通过将块放在同一请求链中并将其打印出来,即:
// ... other code above
http("request_11")
.get(uri1 + "/start?transport=serverSentEvents&clientProtocol=1.5&Authorization=Bearer%20${authToken}&connectionToken=${MyConnectionToken}&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D")
.headers(headers_6)))
.exec{session => println(session("printSesssionVar").as[String])
session}
因为您声明val printSesssionVar = scenario
会话中保存的变量没有被继承。
推荐阅读
- angular - 无法在打字稿中设置未定义的属性(已定义)
- python-3.x - Python Pydantic 双基模型
- scala - Scala:路径相关类型上的模式匹配
- php - Vue CLI+Laravel Api 请求/响应 400 错误请求
- visual-studio-code - ctrl-e 在 Visual Studio 代码中的两个文件之间切换
- php - 在“支付订单”页面 Woocommerce 上保存账单和运输字段数据
- kotlin - KOTLIN:如何为函数中的泛型类型参数分配默认值?
- octave - 增量搜索方法脚本错误
- mysql - MySQL随着时间的推移价格变化最大
- c# - 使用 PlayerPrefs 浮动使数组的某个元素可交互,同时所有 +1 元素保持锁定