2016-06-13 59 views
3

在下面的代碼中,我在第一個gatling請求中獲取一個令牌,並將其保存在名爲auth的變量中。但是,當我嘗試在第二個請求中使用它時,它將發送空字符串來代替auth變量。因此,出於某種原因,auth字符串不會被更新,直到它在第二個請求中被使用。任何人都可以提出任何解決方法,以便我可以將一個請求中返回的值用於另一個請求中?如何使用一個gatling請求返回到另一個請求 - Scala

TIA :)

代碼:

val headers_10 = Map("Content-Type" -> "application/x-www-form-urlencoded") 
    var a= "[email protected]" 
    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 
    } 
    }) 

回答

2

,可以儲存您的會話所需的值。這樣的事情會的工作,但你必須調整正則表達式,也許其他一些細節:

val headers_10 = Map("Content-Type" -> "application/x-www-form-urlencoded") 
    var a= "[email protected]" 
    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

這裏是關於加特林EL,它是使用會話變量的最簡單的方法的文檔(這是「$ {授權}」語法中的最後一行以上):

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

相關問題