SaveAs works for only one user

val login = feed(loginFeeder)
.exec(http(“login”)
.post("/login")
.formParam(“üsername”,"${username}")
.formParam(“password”,"${password}")

#loginFeeder is a csv with usernames and password

val createProduct =

exec(http(“createProduct”)
.post("/task/create")
.header(“X-XSRF-TOKEN”,getCookievalue(CookieKey(“XSRF_TOKEN”))
.body()
.check(jsonpath("$.productId").saveAs(“product”)

.exec(http(“getProductDetails”)
.get("/getproductDetails" + “${product}”)

val scenario1 = scenario(“createProduct”)
.exec(login)
.exec(createProduct)

setUp(scenario1.inject(rampUsers(10) during (5 seconds))).protocols(httpProtocol)

This works fine when i inject only one user. if i inject more than 1 the first one passes and for the rest it says, No Attribute named ‘Product’ is defined.

Wrong getCookievalue usage. getCookieValue is a dedicated action that extracts a cookie from the CookieJar and copy the value in the Session.

https://github.com/gatling/gatling/blob/master/gatling-http/src/test/scala/io/gatling/http/compile/HttpCompileTest.scala#L292

Thanks for the quick response.

I have changed it to

object Helper{
  def setXsrfHeader(session:Session): Validation[String] = {
    getCookie("XSRF-TOKEN", session).map(c => URLDecoder.decode(c, "UTF-8")) match {
      case Some(value) => Success(value)
      case None => Failure("Unable to find XSRF-TOKEN cookie")
    }
  }

  def getCookie(name:String, session:Session): Option[String] = {
    val cookieJar = session("gatling.http.cookies").as[CookieJar]
    cookieJar.get(Uri.create(baseUrl)).find(cookie => cookie.getName == name).map(_.getValue)
  }
}

http("POST")
.post("/myurl")
   .header("X-XSRF-TOKEN", Helper.setXsrfHeader)

It still gives the same error.

Its taking the same xsrf token for multiple users

Lower logging level in logback.xml.

Note: you should stick to getCookievalue instead of riding on internals such as CookieJar.

val login = feed(loginFeeder)
                   .exec(http("login")
                   .post("/login")
                   .formParam("üsername","${username}")
                   .formParam("password","${password}")

.exec(getcookieValue(Cookiekey("XSRF-TOKEN").withDomain("some.com").saveAs("Token")
#loginFeeder is a csv with usernames and password

val createProduct =

exec(http("createProduct")
.post("/task/create")
.header("X-XSRF-TOKEN","$token")
.body()
.check(jsonpath("$.productId").saveAs("product")

.exec(http("getProductDetails")
.get("/getproductDetails" + "${product}")

val scenario1 = scenario("createProduct")
                        .exec(login)
                        .exec(createProduct)

setUp(scenario1.inject(rampUsers(10) during (5 seconds))).protocols(httpProtocol)

Tried this and it works for 1 user. If i inject more says could not verify the csrf token provided,session not found.

Several errors:

  • “$token” is not proper Gatling EL => “${token}”

  • saveAs(“Token”) vs “${token}” => case issue

REALLY Sorry, those were typos while posting,

They are correctly provided in the test, hence it works with 1 user.

.saveAs("token") and used as "${token}"

Thanks to Gatling’s architecture, there’s just no way several virtual users would share the same Session data (as long as you actually don’t use global mutable references in your own code).
You should consider a bug in your application or in your test.
As I said, you should enable debug logging and check what happens on the wire.