2016-06-13 8 views
3

次のコードでトークンを取得し、authという変数に保存しています。しかし、2番目のリクエストでそれを使用しようとすると、auth変数の代わりに空の文字列が送信されます。何らかの理由で、auth文字列が2番目のリクエストで使用されるまで更新されていません。他の要求に1つの要求で返された値を使用できるように誰でも回避策を提案できますか?次のコードで1つのリクエストを別のリクエストに返す方法 - 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、上のドキュメントです(これは上記の最後の行で、「$ {AUTH}」構文):

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

関連する問題