Setting httpProtocol based on the results of an HTTP response

493 views
Skip to first unread message

Daniel Gempesaw

unread,
Feb 27, 2015, 5:51:07 PM2/27/15
to gat...@googlegroups.com
Hey, thanks for such a cool tool! I'm new to Gatling (and a year or so away from scala :/ ) so perhaps I am thinking about my test setup (or everything) incorrectly.

I'd like to use `httpProtocol` to configure my requests. But, I don't know the values that I want to put in the httpProtocol before the test runs, so I'm trying to figure out how to... lazily set up the httpProtocol, or... mutate the httpProtocol, or to set up a new httpProtocol... or something, after sending my first request. The first request comes back with the data I need to properly define the httpProtocol.

Basically, my first request is for an `access_token` as well as a user UUID. Following that, there will be 90 - 100 requests or so using the UUID in the url and requiring the `access_token` in the authorization header. I've found out how to use `saveAs` to put the access token and uuid into the `session`, but I don't know how to tell gatling to use them in the subsequent requests other than explicitly putting a header and url in every request. Here's a short version of what I'm trying to do...

```
val scn = scenario("load tests")

.exec(http("first request with important stuff in the response")
  .post(authentication_url)
  .check(jsonPath("$.user_id").exists.saveAs("user_id"))
  .check(jsonPath("$.access_token").exists.saveAs("access_token"))
)

.exec( (session: Session) => {
  // this is nonsense
  val httpProtocol = http
    .baseUrl("/.../${user_id}/...")
    .authorizationHeader("auth ${access_token}")
})

.exec(http("90 more of these exist")
  .get(needs_the_user_id + "something else different per request")
  .header("Authorization", needs_the_access_token)
  .check(bodyString.is(RawFileBody("expected_response.txt"))))

// repeat variations of the above request with gets, posts, puts, etc...
...

setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
```

Hm, a thought while typing this up, perhaps I could define custom functions for `get`, `post`, and `put` that configure everything properly... that seems reasonable, I guess. Well, if there's any way to do this with httpProtocol, any guidance would be appreciated! Thanks!

Daniel Gempesaw

unread,
Mar 2, 2015, 9:46:56 AM3/2/15
to gat...@googlegroups.com

fwiw, defining custom functions that returned an io.gatling.http.request.builder.HttpRequestBuilder made this more or less a non-issue. :) basically

def get_rat ( page: String ) : io.gatling.http.request.builder.HttpRequestBuilder = {
  http("Get  " + nameFromPage( page ))
    .get("/uses/the/${account_id}/")
    .header("Authorization", "uses the ${access_token}")
}

Stéphane LANDELLE

unread,
Mar 3, 2015, 5:39:51 AM3/3/15
to gat...@googlegroups.com
Hi,

For the record, your former approach can't work:
  • HttpProcol is immutable
  • HttpProtocol is resolved when building the Simulation, i.e BEFORE running it
And yes, a helper method is the best way. Add some implicit conversions and you're king of the world. I'm considering making HttpRequestBuilder a case class to ease such use cases.

Cheers,

Stéphane Landelle
Lead developer


--
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+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Arrowwood

unread,
Mar 3, 2015, 4:15:56 PM3/3/15
to gat...@googlegroups.com
Since I'm guilty of doing this kind of stuff, I would support anything that makes this kind of magic easier to do.  :)

Stéphane LANDELLE

unread,
Mar 4, 2015, 2:10:38 AM3/4/15
to gat...@googlegroups.com

Stéphane Landelle
Lead developer


Reply all
Reply to author
Forward
0 new messages