Reading Session Variables to create an ActionBuilder

477 views
Skip to first unread message

Jason Penney

unread,
Aug 17, 2016, 12:42:33 PM8/17/16
to Gatling User Group
Hi, I'm using Gatling 2.2.2, and I'm trying to do something just slightly unusual with a Gatling session.  I hope this is an easy one for you.

I have a security library that augments JWT, that creates an arbitrary list of HTTP headers that I need to augment my HTTP requests.  Since it is a "rolling JWT" authentication scheme, I need to generate these headers dynamically as the test is running.  I have discovered and confirmed that I can use the exec method to dynamically call this library.  (I also ensure that caching is being performed, so don't worry, the cost of invoking this library has been suitably minimized.)

I have also confirmed that the Gatling session's Map is wonderfully type agnostic, defined as a Map[String, Any].  The rub comes when I want to actually read that list of headers back out of the map to use in my scenario.  The version of exec that declares the session as an accessible argument is expected to return a Session as its result, whereas the version of exec that actually allows me to inject an HTTP request into the scenario does not have direct access to the Session.

I even looked at trying to extend StringBody or its cousins to support a Map[String, String], but I quickly got lost, and I'm not certain if the EL would allow me to correctly extract and retype the Map[String, String] to use in my .headers method.

So, my question is simply this: what's the best and recommended practice for extracting this session variable to use when creating my HTTP request?  Example code below.  Thanks in advance.

-----------

package gatling


import io.gatling.core.Predef._

import io.gatling.http.Predef._


object SessionQuestion {

        val scn = {

                scenario("SetHeaders")

                          .exec(session => {

                                        // In Real Life, this list of headers comes from a security library:

                                        val headerMap = Map[String, String]("header1" -> "headerValue1", "header2" -> "headerValue2")

                                        session.set("headers", headerMap)

                                })

                  .exec({

                                // Question is here: How do I read the current session to create the action?

                                val headerMap: Map[String, String] = null // session.get("headers").asInstanceOf[Map[String, String]]

                                http("SessionQuestionRequest")

                                        .post("/comments")

                                        .headers(headerMap)

                                        .body(StringBody("some_body"))

                                        .asJSON

                                        .check(

                                                status is 204

                                        )

                        })

        }

    }

}


class SessionQuestion extends Simulation {

        val httpProtocol = http

                .baseURL("http://computer-database.gatling.io")

                .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")

                .acceptEncodingHeader("gzip, deflate")

                .acceptLanguageHeader("en-US,en;q=0.5")

                .userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Firefox/45.0")

//        .disableFollowRedirect


        setUp(SessionQuestion.scn.inject(atOnceUsers(10))).protocols(httpProtocol)

}

Stéphane LANDELLE

unread,
Aug 17, 2016, 2:46:56 PM8/17/16
to gat...@googlegroups.com
Looks like a SignatureCalculator to me

Stéphane Landelle
GatlingCorp CEO


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alan Phillips

unread,
Aug 19, 2016, 2:33:48 PM8/19/16
to Gatling User Group
Jason,

In my simulation, after a user logs in, I had to grab the HTTP authorization header into a variable for use in later operations. Here is the example code that worked for me:


Not sure if this directly applies to what you are doing but thought I'd add this note.

Cheers,
Alan
Reply all
Reply to author
Forward
0 new messages