jeudi 29 septembre 2016

How to check if Gatling's session variable exists outside of exec block?

My case is quite simple. I have a REST call defined in Scala class - BackendApi:

def eventPathTreeCall(request: EventPathTreeRequestBuilder): HttpRequestBuilder = {
    val url = request.build()
    http(url).get(url).headers(httpGetHeaders())
  }

This call is used by two scenarios - one simulating loading a webpage, and other just testing REST calls. Depending on which scenario is making the call, I need different request headers. For loading web page scenario I need to pass X-CSRF-TOKEN, but I do not want to send it when executing second scenario. What I wanted to achive, is to have an if statement, checking if csrf-token variable is defined in Gatling's session. My first thought was to use Gatling EL:

def httpGetHeaders(): Map[String, String] = {
 val map = collection.mutable.Map(
  "Etag" -> "",
  "X-Forwarded-For" -> "127.0.0.1",
  "X-Origin-Id" -> "3",
  "X-Requested-With" -> "XMLHttpRequest",
  "Pragma" -> "no-cache",
  "Cache-Control" -> "no-cache",
  "Connection" -> "keep-alive")
 if (!"${csrf-token.isUndefined()}".toBoolean)) map.put("X-CSRF-Token", "${csrf-token}")
 map.toMap
}

Unfortunately it does not work, I get an exception about parsing boolean.

Caused by: java.lang.IllegalArgumentException: For input string: "${csrf-token.isUndefined()}"

So it is clear that Gatling's expression is not evaluated. When I have created a var with headers in the same class, and tried to use expressions in it - they were populated correctly:

val getHeaders = Map(
"Etag" -> "",
"X-CSRF-Token" -> "${csrf-token}",
"X-Forwarded-For" -> "127.0.0.1",
"X-Origin-Id" -> "3",
"X-Requested-With" -> "XMLHttpRequest",
"Pragma" -> "no-cache",
"Cache-Control" -> "no-cache",
"Connection" -> "keep-alive")

If my idea is wrong, please tell me how to use session variable value outside of gatling exec blocks, and base test's logic on these values in Scala code.

Aucun commentaire:

Enregistrer un commentaire