Modifying session in transformWithSession in Java

Gatling version: 3.13
Gatling flavor: java kotlin scala javascript typescript
Gatling build tool: maven gradle sbt bundle npm

I made sure I’ve update my Gatling version to the latest release
I read the guidelines and how to ask a question topics.
I provided a SSCCE (or at least, all information to help the community understand my topic)
I copied output I observe, and explain what I think should be.

Hello Gatling Community,
I would like to ask if it is legal and supposed to “work-as-desired” to modify the session within the transformWithSession method?

check((header("foo").transformWithSession((h, s) -> s.set("header_foo", h)))

The above effectively sets the key “header_foo” in the session, but is it a proper use of the API or more of a coincidence?

Regards,
Tomasz G.

No, what you’re doing doesn’t work. Session is immutable.
What you’re simply doing here is creating a new Session instance that gets discarded right away as your check doesn’t do anything with it.
You can’t transform the Session with transformWithSession whose purpose is to transform what’s extracted from the check.

1 Like

I am sorry, I simplified the example to make it more compact and I did not test it and… it was deprived of its vital content that way.

check(header("foo").transformWithSession((h, s) -> {
  Map<String, String> headers = s.getMap("headers");
  headers.put("foo", h);
  return s.set("headers", headers);
}))

This now becomes clear why it “works” :). The map inside the session gets updated with the additional value. The updated and returned session has nothing to do with that update and just gets discarded (one could return an empty string with the same effect).

The above is therefore not an example for a proper API usage and more of a result of some confusion on our end.

Greetings,
Tomasz G.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.