Hi All,
I have been evaluating Gatling for a while and I’m impressed with the simplicity and robustness of the tool. I look forward to using Gatling for my workflow pretty soon. I’m thinking about load testing our Restful API which uses JSON.
I have multiple Gatling simulations file to support distinct use cases and I see myself making authentication request for every scenario which is required when for multiple users. For example, lets say I have three scenarios CreateUser, UpdateUser and GetUser. To consume each of these service I need to first authenticate with the API and use the authentication token returned in the header thereafter. Here is the snippet of the code that will help explain the case better.
`
class CreateUser extends Simulation {
val scn = scenario(“CreateUserScenario”)
.exec(http(“Authentication”)
.post(https://myawesomeapi.com/auth)
.headers(headers1)
.body(StringBody("""{{"user": "xyz"}, {"pass":"secret"}""")).asJSON
.check(status.is(201))
.check(jsonPath("$.token").find.saveAs("mytoken"))
)
.pause(1)
.exec(http("CreateUser")
.post("[https://myawesomeapi.com/createuser](https://myawesomeapi.com/createuser)")
.header("token", "${mytoken}")
.body(StringBody("""{{"user": "xyz"}, {"pass":"secret"}, {"email" : "[user@e](mailto:user@email.com )mail.com"}""")).asJSON
.check(status.is(200))
.check(jsonPath("true").find.saveAs("success"))
)
setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))
}
`
`
class UpdateUser extends Simulation {
val scn = scenario(“UpdateUserScenario”)
.exec(http(“Authentication”)
.post(https://myawesomeapi.com/auth)
.headers(headers1)
.body(StringBody("""{{“user”: “xyz”}, {“pass”:“secret”}""")).asJSON
.check(status.is(201))
.check(jsonPath("$.token").find.saveAs(“mytoken”))
)
.pause(1)
.exec(http(“UpdateUser”)
.post(“https://myawesomeapi.com/updateuser/123”)
.header(“token”, “${mytoken}”)
.body(StringBody("""{{“user”: “xyz”}, {“pass”:“newsecret”}, {“email” : “user@email.com”}""")).asJSON
.check(status.is(200))
.check(jsonPath(“true”).find.saveAs(“success”))
)
setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))
}
`
You can see that I have to authenticate before I consume anything else. I’m thinking that there should be a better way to do this and wrap the Authentication portion of the code into a shared package and import/utilize that in the actual scenario. Something like this?
`
import Authentication._
class CreateUser extends Simulation {
val scn = scenario(“CreateUserScenario”)
.exec(DoAuthentication(“some input value here”)) //extract token returned to reuse below
)
.pause(1)
.exec(http(“CreateUser”)
.post(“https://myawesomeapi.com/createuser”)
.header(“token”, “${mytoken}”)
.body(StringBody("""{{“user”: “xyz”}, {“pass”:“secret”}, {“email” : “user@xyzemail.mail”}""")).asJSON
.check(status.is(200))
.check(jsonPath(“true”).find.saveAs(“success”))
)
setUp(scn.inject(atOnceUsers(1)).protocols(httpConf))
}
`
I am new to Scala so apologize for any errors and if this is a simple thing. How do I go about doing this? Is there an example that I can follow? I have searched the forums and haven’t found something similar to this. I plan to use this module not only for authentication but for anything that is common across multiple scripts.
Thank you in advance!