Exctract data from encoded response in Gatling

跟風遠走 提交于 2019-12-11 14:38:01

问题


I have a problem with extracting data from one response, I check it in Fiddler and I get message that this response is encoded: https://i.postimg.cc/G3VfMtxh/2019-10-15-12-31-41-Progress-Telerik-Fiddler-Web-Debugger.png

When I copy this content to https://jsonpath.com/ I see https://i.postimg.cc/rpYP4wh4/2019-10-14-22-41-19-jsonpath-online-evaluator.png

So I tried to use regex, I checked it here https://regex101.com/ https://i.postimg.cc/DZ6m6RX1/2019-10-14-22-58-57-Online-regex-tester-and-debugger-PHP-PCRE.png

And it works in online editor, but there is a problem with script.


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)
}


But in both cases I get error

> 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

I tried to extract it on request_6 and use it on request_11.

Is it possible to handle this request? On other I extract bearer token without problems.


回答1:


You have both of the extractions for MyConnectionToken commented out above, but I guess you already know that?

Try and print it out by putting the .exec { session => block within the same request chain, i.e.:

// ... 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}

Because you are declaring val printSesssionVar = scenario the saved variable from the session isn't being carried over.



来源:https://stackoverflow.com/questions/58393026/exctract-data-from-encoded-response-in-gatling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!