How to get that pesky session data and call a http function inside exec

I am trying to call a custom function to generate a map of headers, then use that map to pass to the http.headers() function. However, while attempting this in the body of an exec method, the test runs, but doesn’t execute the actual GET call.

Perhaps there is a better what to do this altogether. Thanks in advance for any help.

`

class RecordedSimulation extends Simulation {

val httpProtocol = http
.inferHtmlResources()
.acceptEncodingHeader(“gzip, identity”)
.connection(“Keep-Alive, TE”)
.contentTypeHeader(“application/json”)
.userAgentHeader(“Gatling”)

val director = “http://google.com

var feeder = Iterator.continually(Map(“deviceId” → CreateDeviceId()))

val scn = scenario(“RecordedSimulation”)
.feed(feeder)
.exec(session =>
{
val headers = CreateHeader(“test”, session(“deviceId”).as[String])
http(“Director”)
.get(director)
.headers(headers)
.check(status.is(200))
.check(jsonPath("$.serverUrl").saveAs(“serverUrl”))
session
})

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

`

CreateHeader is from an imported class ( if it matters ):

`

def CreateHeader(time : String, deviceId : String): Map[String, String] = {

val headers = Map(
“TE” → “identity”,
“deviceId” → deviceId,
“time” → time,
“token” → GetTokenHeaderValue(time, deviceId))
return headers;
}

`

Test results: ( i have tracing enabled in logback config )

GATLING_HOME is set to /usr/local/gatling/2.1.4
Simulation xxx.RecordedSimulation started…
Creds: 4833b584-3098-4486-868f-d8a49e8b50e5:test

How about capturing headers in session function first and then calling get. I haven’t done anything like this before but based on my limited understanding it should work, I hope.

`
val scn = scenario(“RecordedSimulation”)
.feed(feeder)
.exec{session =>
val headers = CreateHeader(“test”, session(“deviceId”).as[String])
session.set(“headers”, headers)
}
.exec(http(“Director”)
.get(director)
.headers("${headers}")
.check(status.is(200))
.check(jsonPath("$.serverUrl").saveAs(“serverUrl”))
)

`

Good suggestion, which led me to a solution. Unfortunately, headers() is not one of the functions that accepts gatling’s EL. Fortunately, header() does, and I was able to do this:

`

val scn = scenario(“RecordedSimulation”)

.feed(feeder)
.exec(http(“Director”)
.get(director)
.check(status.is(200))
.check(jsonPath("$.serverUrl").saveAs(“serverUrl”)))
.pause(1) // 10
.exec(session => SetHeaderValuesInSession(session))
.exec(http(“Register”)
.get(“https://${serverUrl}/xxx/xxx/xxx”)
.header(“time”,"${time}")
.header(“deviceId”,"${deviceId}")
.header(“token”,"${token}")
.check(status.is(200))
.check(jsonPath("$.simsId").saveAs(“simsId”)))
.exec(session => {
println(session)
session
})

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

def SetHeaderValuesInSession(session : Session): Session = {
val time = GetTime()
val deviceId = session(“deviceId”).as[String]
val token = GetTokenHeaderValue(time, deviceId)
session.set(“time”,time).set(“token”, token)
}

`

Thanks again!

-Ryan