implementing scala code in the simulation script to generate HMAC and using ELFileBody

Hi,

I tried to do this:

`

package com.site

import scala.concurrent.duration._

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._

class checkXXXSimulation extends Simulation {

 val httpProtocol = http
  .baseURL("http://stage-xxx.xxx.com")
  .inferHtmlResources()
  .acceptEncodingHeader("gzip,deflate")
  .contentTypeHeader("text/xml;charset=UTF-8")
  .userAgentHeader("Apache-HttpClient/4.1.1 (java 1.5)")

 val headers_0 = Map("SOAPAction" -> """"http://xxx.cardservices.xxx.com/Creditxxx2"""")

  val md = java.security.MessageDigest.getInstance("SHA-1")
  val str = " “7080001237555” + ”1104” + ”7080001237555” + ”2015-02-04 12:23:55” + ”xxx” + ${MemberId) + ”2” +”hJSaxxx5M9” "
  val ha = new sun.misc.BASE64Encoder().encode(md.digest(str.getBytes))

 val scn = scenario("checkxxxSimulation")

  .feed(csv("data/memberid.csv").random)

  .exec(http("request_0")
   .post("/xxxCardservicesxxxApi/ValueCodeService.asmx")
   .headers(headers_0)
   .body(ELFileBody("checkxxx_request.txt"))
   .check(status.is(200)))

 setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}

`

Two problems:

  1. I want to ‘feed’ my definition with a variable from a .csv file called ‘member’, however I cannot put the .feed infront of the scala code (val str = " “70…) How Can I feed a scala implementation with a .csv file?

  2. When I have a request body (checkxxx_request.txt) with content (not complete):

      <!--Optional:-->
            <pos:Footer>
               <!--Optional:-->
               <api:MAC>${ha}</api:MAC>
            </pos:Footer>
         </pos:wrapper>
      </pos:CreditXXX2>
   </soapenv:Body>
</soapenv:Envelope>

I get the error:

No attribute named ‘ha’ is defined

Why?

You have to write a session fn and add the variable to the session. See session api.

Stefan

session fn?

Session function

Eg:
exec{session =>

val ha = new sun.misc.BASE64Encoder().encode(md.digest(str.getBytes))
session.set("ha",ha)
}

Ok, then I have this:

`

package com.xxx

import io.gatling.core.Predef._
import io.gatling.http.Predef._

class checkxxxSimulation extends Simulation {

 val httpProtocol = http
  .baseURL("http://xxx.xxx.com")
  .inferHtmlResources()
  .acceptEncodingHeader("gzip,deflate")
  .contentTypeHeader("text/xml;charset=UTF-8")
  .userAgentHeader("Apache-HttpClient/4.1.1 (java 1.5)")

 val scn = scenario("checkxxxSimulation")

    .feed(csv("data/members.csv").random)

    .exec{session =>
    val md = java.security.MessageDigest.getInstance("SHA-1")
    val str = " “7080001237xxx” + ”1108” + ”7080001237xxx” + ”2015-02-04 12:23:55” + ”xxx” + ${member) + ”2” +”hJSaxxx” "
    val ha = new sun.misc.BASE64Encoder().encode(md.digest(str.getBytes))
    http("checkxxx")
      .post("/xxx/xxx.asmx")
      .headers(Map("SOAPAction" -> """"http://xxx.xxx.xxx.com/Checkxxx""""))
      .body(ELFileBody("RecordedSimulation_0000_request.txt"))
    session.set("ha",ha)
      }
 setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}

`

Further I have the request body:

`

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:tns="http://xxx.xxx.xxx.com/" xmlns:s1="http://xxx.xxx.xxx.Com/">
   <soap:Body>
      <tns:CheckEVC2 xmlns="http://xxx.xxx.xxx.com/">
         <tns:wrapper>
            <tns:CheckxxxRequest>
               <tns:ValueCodeID>${member)</tns:ValueCodeID>
               <tns:ValueCodeType>2</tns:ValueCodeType>
            </tns:CheckxxxRequest>
            <tns:Footer xsi:type="s1:Footer">
               <s1:MAC>${ha)</s1:MAC>
            </tns:Footer>
            <tns:Header xsi:type="s1:Header">
               <s1:AcquirerId>company</s1:AcquirerId>
               <s1:AgreementID>7080001237xxx</s1:AgreementID>
               <s1:CashRegisterID/>
               <s1:ClientIP/>
               <s1:MerchantConceptID>1108</s1:MerchantConceptID>
               <s1:MerchantID>708000123xxx</s1:MerchantID>
               <s1:TransmissionTime>2015-02-04 12:23:55</s1:TransmissionTime>
            </tns:Header>
         </tns:wrapper>
      </tns:Checkxxx>
   </soap:Body>
</soap:Envelope>

`

and a .csv file looking like this:

member
32014142
32019332
.
.
.

Running this gives me the error:

Maybe like this (if the ‘session’ is accessible between my two .exec’s ? Are they?

`

val scn = scenario("checkxxxSimulation")

   .feed(csv("data/members.csv").random)

   .exec{session =>
   val md = java.security.MessageDigest.getInstance("SHA-1")
   val str = " “7080001237xxx” + ”1108” + ”7080001237xxx” + ”2015-02-04 12:23:55” + ”xxx” + ${medlem) + ”2” +”hJSaGExxx” "
   val ha = new sun.misc.BASE64Encoder().encode(md.digest(str.getBytes))
   session.set("ha", ha)}

   .exec(
   http("checkxxx")
     .post("/xxx/xxx.asmx")
     .headers(Map("SOAPAction" -> "http://xxx.xxx.xxx.com/Checkxxx"))
     .body(ELFileBody("checkxxx_request.txt")))

setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)

`

You can not put your post request inside the session fn. Session fn is usually used for setting up attributes, which are available for the rest of the iteration.

Here is the link that describes it. http://gatling.io/docs/2.1.4/general/scenario.html#structure-elements

No… I found out what you are saying:

This works:

`

val scn = scenario("checkxxxSimulation")

   .feed(csv("data/members.csv").random)

   .exec{session =>
   val md = java.security.MessageDigest.getInstance("SHA-1")
   val str = " “7080001237xxx” + ”1104” + ”7080001237xxx” + ”2015-02-04 12:23:55” + ”xxx” + ${medlem) + ”2” + ”hJSaGxxx” "
   val ha = new sun.misc.BASE64Encoder().encode(md.digest(str.getBytes))
   val mac = ha.substring(0, ha.length() / 2 )
   session.set("mac", mac)
   }

   .exec(session => {
   println((session("mac").as[String]))
   session})

   .exec(
   http("checkxxx")
     .post("/xxx/xxx.asmx")
     .headers(Map("SOAPAction" -> "http://xxx.xxx.xxx.com/xxx"))
     .body(ELFileBody("checkxx_request.txt"))
     .check(status.is(200)))

`

The problem now is that the actual decoding does not work (or the string concatenate?).

`

val ha = new sun.misc.BASE64Encoder().encode(md.digest(str.getBytes))

`

Any tips on what might be wrong with the decoding?

Cheers!

you shouldn’t use sun packages: http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html
Use this: http://docs.oracle.com/javase/8/docs/api/java/util/Base64.Encoder.html
But that is probably not the issue. It’s probably all the quotes. Read this: http://www.tutorialspoint.com/scala/scala_strings.htm

HTH

Stefan

Ok this works:

val str = "7080001237543" + "1105" + "7080001237543" + "2015-02-04 12:23:55" + "site" + "0032014143" + "2" + "hJSbGEx5M7"

Can I replace the number “0032014143” with a variable ${member}inside the concatenating? I need to parameterize it for each request.