Hi,
I'm trying to learn Gatling, so sorry if this is the wrong forum. I've tried searching for this and couldn't find any inside information.
Currently I'm working on a Gatling suite for an API that I'm working on, and this API is used by first acquiring an access token that is part of a session response; and with the access token subsequently being added as a header for each and every request.
I could manually add the header for each and every request that I make to the API, however this seems quite redundant.
Is there a recommended approach for adding a session token to each and every http call BUT the one that returns the token in the first place?
Best,
Henrik
def remove = {
exec(http("history-remove")
.delete("/v1/history/remove")
.header("X-Access-Token", "${access_token")
)
} If I recall correctly I could do it with something like the above, once saved into my session, however repeating this line for each and every endpoint is certainly feasible, it just feels a bit redundant. Best, Henrik
package object models {
implicit def apiHttp(requestName: Expression[String]):HttpHelper = new HttpHelper(requestName)
}
class HttpHelper(requestName: Expression[String]) extends Http(requestName: Expression[String]) {
private final val SESSION_TOKEN = "X-Session-Token"
override def get(url: Expression[String]) = httpRequest("GET", url).header(SESSION_TOKEN, "${session_token}")
override def get(uri: Uri) = httpRequest("GET", Right(uri)).header(SESSION_TOKEN, "${session_token}")
override def put(url: Expression[String]) = httpRequest("PUT", url).header(SESSION_TOKEN, "${session_token}")
override def post(url: Expression[String]) = httpRequest("POST", url).header(SESSION_TOKEN, "${session_token}")
override def patch(url: Expression[String]) = httpRequest("PATCH", url).header(SESSION_TOKEN, "${session_token}")
override def head(url: Expression[String]) = httpRequest("HEAD", url).header(SESSION_TOKEN, "${session_token}")
override def delete(url: Expression[String]) = httpRequest("DELETE", url).header(SESSION_TOKEN, "${session_token}")
override def options(url: Expression[String]) = httpRequest("OPTIONS", url).header(SESSION_TOKEN, "${session_token}")
}
My initial request, that is to take place at least once during a virtual user session saves the session_token into the session.
val items = {
exec(apiHttp("history-items")
.get("/v1/history/items)
)
}
exec(http("sessions-get")
.post("/v1/sessions")
.body(StringBody(Json.stringify(jsonBody)))
.check(
status.is(201),
jsonPath("$.session_token").saveAs("session_token")
)
)