Search code examples
scalaurljsessionidgatling

Gatling record JSessionID from URL


I'm the Sysadmin for a small company and I'm trying to set up a first test with gatling for one of our Webapps. I know a bit of C and Java Syntax as well as Regexes, but no Scala whatsoever.

The app I'm trying to test has the jsessionid (including a jvmRoute) in the URL, not set in a cookie. According to what Stéphane Landelle wrote here, Gatling is supposed to automagically record the jsessionid per user session and replay it, but that appears to only work when the jsessionid is set as a Cookie.

I deleted the actual recorded jsessionid from the URLs in the test case, reasoning that it would not be valid on any future attempts. When i run the test, the Appserver generates a new jsessionid, which is then not included in any future calls.

Because of this, i'm trying to scrape the jsessionid from the initial redirect and include it in any future URL. There is a Location Header in the first response that looks like this:

Location    https://URL/welcome.do;jsessionid=F97250BDC1576B5766CEFA56645EA3F4.node1

The code currently looks like this:

    .exec(http("Open Page")
      .get("""/?code=abcdef""")
      .headers(headers_0)
 // Test extract jsessionid from URL
       .check(headerRegex("Location", "jsessionid=(.*)")).saveAs("jsess")

    .exec(http("welcome.do")
      .post("""/welcome.do;jsessionid=${jsess}""")

...and it doesn't compile.

12:15:14.198 [ERROR] i.g.a.ZincCompiler$ - FirstTest.scala:53: value saveAs is not a member of io.gatling.http.request.builder.HttpRequestBuilder
12:15:14.199 [ERROR] i.g.a.ZincCompiler$ -          .check(headerRegex("Location", "jsessionid=(.*)")).saveAs("jsess")
12:15:14.200 [ERROR] i.g.a.ZincCompiler$ -                                                             ^
12:15:14.261 [ERROR] i.g.a.ZincCompiler$ - one error found

If i move one closing bracket to the end:

  .check(headerRegex("Location", "jsessionid=(.*)").saveAs("jsess"))

it compiles but does not do what's desired:

---- Errors --------------------------------------------------------------------
> No attribute named 'jsess' is defined                              11 (78.57%)
> status.in(200,304,201,202,203,204,205,206,207,208,209), but ac      2 (14.29%)
tually found 404
> headerRegex((Location,jsessionid=(.*))).exists, found nothing       1 ( 7.14%)
================================================================================

So, how do i record the jsessionid in order to reuse it? Or am I doing the completely wrong thing here? Any help is appreciated.


Solution

  • What happens is that if Gatling follows redirect (default), checks will be applied on the landing request. In order to grab your jsessionid path parameter, you can either disable follow redirect in HttpProtocol, or use a currentLocationRegex check.

    Then, is this jsessionid path parameter only there for the first page, or on all of them? Usually, it's only there on the first page, until the servlet container realizes that your browser supports cookies and it can use them for session tracking instead.

    Are you reaaaaally sure that you want your app to suppport disabled cookies (I mean, we're in 2014, why not disabled javascript too...). If not, you can probably disable path parameter session tracking. Check this thread for how it can be done on Tomcat.

    If so, yes, you'll have to add this path parameter to every url. You'll probably want to define a method for that, such as:

    def appendJSessionId(pathWithoutQuery: String) =
      pathWithoutQuery + ";jsessionid=${jsessionid}"