Only 1 cookie sent to server - even when multiple cookies are set in session

Gatling version: 2.1.7

The server application that I am testing needs 2 cookies to validate the authenticity of logged in user. I do get the 2 cookies when I log-in and then I initiate another request by setting the header values on Cookies; however on the server I always get only 1 cookie, the “SecurityTokenURL”.

Is there something that I am missing or doing wrong?

`
package com.xxx

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

class TestSimulation extends Simulation {

val httpProtocol = http
.baseURL(“http://xxxx-server/”)
.disableCaching
.inferHtmlResources(BlackList("""..js""", “”"..css""", “”"..gif""", “”"..jpeg""", “”"..jpg""", “”"..ico""", “”"..woff""", “”"..(t|o)tf""", “”"..png""", “”"..html"""), WhiteList())

val KEY_CENTRIC_REQUEST_PREVENT_CACHE = “request.preventCache”;

val CONNECTION_HEADER_VALUE = “keep-alive”;

val commonHeaders = Map(
HttpHeaderNames.Accept → HttpHeaderValues.ApplicationJson,
HttpHeaderNames.CacheControl → HttpHeaderValues.NoCache,
HttpHeaderNames.Connection → CONNECTION_HEADER_VALUE)

val randomGenerator = new scala.util.Random(99999999)

val scn = scenario(“Login”)
.exec(
http(“login”)
.post("/csi-requesthandler/RequestHandler")
.headers(commonHeaders)
.header(HttpHeaderNames.ContentType, HttpHeaderValues.MultipartFormData)
.queryParam(KEY_CENTRIC_REQUEST_PREVENT_CACHE, randomGenerator.nextInt())
.formParamMap(Map(“LoginID” → “xxx”,
“Password” → “xxx”,
“Module” → “xxx”,
“Operation” → “SimpleLogin”,
“OutputJSON” → “1”))
.check(
headerRegex(HttpHeaderNames.SetCookie, “SecurityTokenURL=[\w:/]+”)
.transform(header => extractAndTransformHeader(header))
.saveAs(“securityTokenAuthHeader”))
.check(
headerRegex(HttpHeaderNames.SetCookie, “JSESSIONID=[\w:/]+”)
.transform(header => extractAndTransformHeader(header))
.saveAs(“jsessionIdAuthHeader”))
.check(regex(""“Status:“Successful”,”"")))

val getCurrentSessionScn = scenario(“GetCurrentSession”)
.exec(session => {
flushSessionCookies
val securityTokenArray = session(“securityTokenAuthHeader”).as[Array[String]]
println(“securityTokenArray: " + securityTokenArray(0) + “:” + securityTokenArray(1))
addCookie(Cookie(securityTokenArray(0), securityTokenArray(1)).withPath(”/"))

val sessionIdArray = session(“jsessionIdAuthHeader”).as[Array[String]]
println(“sessionIdArray: " + sessionIdArray(0) + “:” + sessionIdArray(1))
addCookie(Cookie(sessionIdArray(0), sessionIdArray(1)).withPath(”/csi-requesthandler"))

session

})
.exec(http(“getcurrentsession”)
.get("/csi-requesthandler/RequestHandler")
.headers(commonHeaders)
.queryParamMap(Map(KEY_CENTRIC_REQUEST_PREVENT_CACHE → randomGenerator.nextInt(),
“Module” → “xxx”,
“Operation” → “GetCurrentSession”,
“OutputJSON” → “1”))
.check(regex(""“Status:“Successful”,”"")
.transform(body => extractAndTransformHeader(body))))

val allScenarios = scn.exec(getCurrentSessionScn)

setUp(
allScenarios.inject(atOnceUsers(2))).protocols(httpProtocol)

def extractAndTransformHeader(originalHeader: String): Array[String] = {
originalHeader.split("=")
}

}
`

You can’t use Gatling DSL components inside functions.
Those are merely builders.

Thanks for the pointer. For those who are interested, this is how I achieved it

`
val getCurrentSessionScn = scenario(“GetCurrentSession”)
.exec(session => {
val securityTokenArray = session(“securityTokenAuthHeader”).as[Array[String]]
val sessionIdArray = session(“jsessionIdAuthHeader”).as[Array[String]]

session.set(“SecurityTokenURLValue”,securityTokenArray(1)).set(“JSESSIONIDValue”, sessionIdArray(1))
})
.exec(
addCookie(Cookie(“SecurityTokenURL”, “${SecurityTokenURLValue}”))
)
.exec(
addCookie(Cookie(“JSESSIONID”, “${JSESSIONIDValue}”))
)
.exec(http(“getcurrentsession”)
.get("/csi-requesthandler/RequestHandler")
.headers(commonHeaders)
.queryParamMap(Map(KEY_CENTRIC_REQUEST_PREVENT_CACHE → randomGenerator.nextInt(),
“Module” → “xxx”,
“Operation” → “GetCurrentSession”,
“OutputJSON” → “1”))
.check(regex(""“Status:“Successful”,”""))
)
`

Thanks,

Anil

Too much boilerplate. Gatling EL can access array elements by index: http://gatling.io/docs/2.1.7/session/expression_el.html#expression-language