So I have many gatling rest call functions that look more or less like:
val restCall = {
exec(http("RestCall")
.post("/restCall")
.body(ElFileBody("json/body.json")).asJson
.check(saveResponseToSession(status,bodyString,header))
)
.exec(session => {validateResponse(session)})
.pause(Config.minDelayValue seconds, Config.maxDelayValue seconds)
}
.check(saveResponseToSession(status,bodyString,header))
<-- Is saving the body, status and header as session variables
.exec(session => {validateResponse(session)})
<-- is validating the status, body and header according to my custom needs.
.pause(Config.minDelayValue seconds, Config.maxDelayValue seconds)
<-- a simulation level pause that is used on all my transactions
These three functions are executed on all my API calls, what it means to me is hundreds of code duplication lines...
I am looking for a way to create/overide the .exec chain function and which will include these 3 functions at the end of each call.
So for example the above example will look like:
val restCall = {
customExec(http("RestCall")
.post("/restCall")
.body(ElFileBody("json/body.json")).asJson)
But will also include:
.check(saveResponseToSession(status,bodyString,header))
)
.exec(session => {validateResponse(session)})
.pause(Config.minDelayValue seconds, Config.maxDelayValue seconds)
}
as I shown on the first example.
You could just have a function that takes a ChainBuilder to run and then runs the validate and pause steps after.
val restCall = {
exec(http("RestCall")
.post("/restCall")
.body(ElFileBody("json/body.json")).asJson
.check(saveResponseToSession(status,bodyString,header))
)
}
def validateAndWait(chain: ChainBuilder) =
exec(chain)
.exec(session => {validateResponse(session)})
.pause(Config.minDelayValue seconds, Config.maxDelayValue seconds)
def scn = scenario("action with validation").exec(validateAndWait(restCall))