Optional queryParam in Gatling

Viewed 475

In my Gatling test I want to use queryParams but some have to be optional. Empty values gives errors so that is not an option. Is there a way to make a queryParam optional?

2 Answers

Let's suppose that you have some variable in the session (let's say foo) and based on the value of that variable, you determine whether or not a query parameter is required.

You can achieve this using queryParamMap() and providing an arbitrary Expression[Map[String, Any]] i.e. a Session => Validation[Map[String, Any]] function.

http("Getting issues")
  .get("https://github.com/gatling/gatling/issues")
  .queryParamMap(session =>
    session("foo").validate[String].map {
      case "a" => Map("milestone" -> "1", "state" -> "open")
      case "b" => Map("milestone" -> "1", "state" -> "open", "author" -> "jenkins")
    })

Seek more information in official documentation.

My answer based on answer by Camilo Silva. Perhaps my solution will be useful to community:

.queryParamMap(session =>
      session("experiment").validate[String].map {
        case value if value.nonEmpty => Map("experiment" -> value)
        case "" => Map()
      })
Related