繁体   English   中英

如何使用一个gatling请求返回另一个请求 - Scala

[英]How to use return of one gatling request into another request - Scala

在下面的代码中,我在第一个Gatling请求中获取一个令牌,将其保存在名为auth的变量中。 但是,当我尝试在第二个请求中使用它时,它发送空字符串代替auth变量。 因此,出于某种原因,auth字符串直到在第二个请求中使用时才被更新。 任何人都可以建议任何解决方法,以便我可以将一个请求中返回的值用于另一个请求吗?

码:

  val headers_10 = Map("Content-Type" -> "application/x-www-form-urlencoded")
  var a= "qwerty91@gmail.com"
  var auth = ""
  val scn = scenario("Scenario Name") // A scenario is a chain of requests and pauses
    .exec(http("request_1") // Here's an example of a POST request
      .post("/token")
      .headers(headers_10)
      .formParam("email", a)
      .formParam("password", "password")
      .transformResponse { case response if response.isReceived =>
        new ResponseWrapper(response) {
        val a = response.body.string
        auth = "Basic " + Base64.getEncoder.encodeToString((a.substring(10,a.length - 2) + ":" + "junk").getBytes(StandardCharsets.UTF_8))
     }  
     })
     .pause(2)
     .exec(http("request_2")
       .get("/user")
       .header("Authorization",auth)
       .transformResponse { case response if response.isReceived =>
        new ResponseWrapper(response) {
        val a = response.body.string
     }
   })

您应该在会话中存储所需的值。 这样的东西会起作用,虽然你必须调整正则表达式和其他一些细节:

val headers_10 = Map("Content-Type" -> "application/x-www-form-urlencoded")
  var a= "qwerty91@gmail.com"
  var auth = ""
  val scn = scenario("Scenario Name") // A scenario is a chain of requests and pauses
    .exec(http("request_1") // Here's an example of a POST request
      .post("/token")
      .headers(headers_10)
      .formParam("email", a)
      .formParam("password", "password")
      .check(regex("token: (\\d+)").find.saveAs("auth")))
    .pause(2)
    .exec(http("request_2")
      .get("/user")
      .header("Authorization", "${auth}"))

这是关于“检查”的文档,您可以使用它来从响应中捕获值:

http://gatling.io/docs/2.2.2/http/http_check.html

这是关于gatling EL的文档,这是使用会话变量的最简单方法(这是上面最后一行中的“$ {auth}”语法):

http://gatling.io/docs/2.2.2/session/expression_el.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM