如何将在saveAs(“ access_token”)中生成的access_token发送给Gatlin Soap Request的ElFileBody

时间:2019-05-10 16:36:27

标签: gatling scala-gatling gatling-jsonpath

我有此代码,可以在我希望将auth_token传递给soap请求正文的地方加特林。 Soap请求主体写在txt文件中,而该txt文件在ElFilebody中调用

我需要发送的是在saveAs(“ access_token”)中生成的access_token,然后将其发送到ElFileBody(“ Soap_request_CancelEvent.txt”)

Content of Soap_request_CancelEvent.txt is below
<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns="http://www.example.com/Service/2013-03">
<soapenv:Header/>
<soapenv:Body>
  <ns:CancelEvent>
     <!--Optional:-->
     <ns:request>
        <ns:AuthToken>${access_token}</ns:AuthToken>
        <ns:SanctionNumber>?</ns:SanctionNumber>
     </ns:request>
   </ns:CancelEvent>
 </soapenv:Body>
</soapenv:Envelope>

Gatling控制台中的错误是

 Session(Test Cancel Event Soap Request,23,Map(caee65d1-d380-48e9-8566- 
 9d8efb16b5fa -> 0, 6014555f-5487-44bc-9384-864dece96fe0 -> 
 5),1557435685640,-125,KO,List(ExitOnCompleteLoopBlock(6014555f-5487-44bc- 
 9384-864dece96fe0), ExitOnCompleteLoopBlock(caee65d1-d380-48e9-8566-  
 6b5fa)),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda$37 
 2/1791589252@2be8b240)
14:01:30.520 [ERROR] i.g.h.a.s.HttpRequestAction - 'httpRequest-17' failed 
to execute: No attribute named 'access_token' is defined

代码在

下方
 package simulations

import scala.io.Source

 class ERBSJson extends BaseSimulation  {


 val httpProtocol = http
 .baseURL("http://services.online.local:4059/Business/")

 val uri03 = "https://api.platform.com"



   )

 val header_cancel = Map(
 "POST" -> "http://services.online.local:4059/Business/ HTTP/1.1",
 "Accept-Encoding" -> "gzip,deflate",
 "Content-Type" -> "text/xml;charset=UTF-8",
 "SOAPAction" -> "http://www.example.org/Service/2013- 
 03/IEventReporterEndpoint/CancelEvent",
 "Content-Length" -> "429",
 "Host" -> "r20services.onlinegaming.local:4059",
 "Connection" -> "Keep-Alive",
 "User-Agent" -> "Apache-HttpClient/4.1.1 (java 1.5)"
 )

 val headers_10 = Map("Content-Type" -> "application/json","Authorization" 
 -> "${token_type} + ${access_token}" )



val source: String = Source.fromFile("C:/Gatling2/resources/data/input- 
ERBS.json").getLines.mkString
def userCount: Int  = JsonPath.parse(source).read("$.[0].user")
def testDuration: Int  = JsonPath.parse(source).read("$.[0].testDuration")
def rampDuration: Int  = JsonPath.parse(source).read("$.[0].rampDuration")



before {
  println(s"Running test with ${userCount} users")
  println(s"Ramping users over ${rampDuration} seconds")
   println(s"Total Test duration: ${testDuration} seconds")
  }


def CancelEvent()={
   repeat(990000000){


  exec(flushHttpCache)
  exec(flushHttpCache)
    .exec(http("Cancel Event")
      .post("EventReporterV2")
      .headers(header_cancel)
      .body(ElFileBody("Soap_request_CancelEvent.txt"))
      .check(status.in(200,201)))//checkforaspecificstatus
    .exec{session=>println(session);session}//parameterfortheorgIdgoeshere
    .pause(1)

    }
         }



 val scenario1 = scenario("Test Cancel Event Soap Request ")
 .exec(http("Event-Reservations-Web-Image-Login")
  .get("https://api.origin.cloud/dev/event-reservations- 
 /loading.dfbfd678.svg")
  .headers(headers_1)
  .resources(http("Http Header Token Authentication Url")
    .options(uri03 + "/auth/oauth/token")
    .headers(headers_7),
    http("Token Generation Url For Post")
      .post(uri03 + "/auth/oauth/token")
      .headers(headers_8)
      .formParam("grant_type", "password")
      .formParam("username", "zyz@abc.com")
      .formParam("password", "fJC2RuVmHB")


  .basicAuth("ikrwugh3883gh","NbnEEqmDLSfno315o87ghFGYr3jybtzbi76sr")
      .check(jsonPath("$.access_token").exists.saveAs("access_token"))
      .check(jsonPath("$.token_type").exists.saveAs("token_type"))





  ))
.forever() { // add in the forever() method - users now loop forever

  exec(CancelEvent())
}




 setUp(

   scenario1.inject(
   nothingFor(5 seconds),
  rampUsers(userCount) over ( rampDuration ))
  .protocols(httpProtocol))


.maxDuration(testDuration)

}

1 个答案:

答案 0 :(得分:0)

您可以尝试删除“ .exists”命令吗?还是要保存就没必要