Извини, Стефан, но я работал над другими подходами. Это мой код, который работает :)
import io.gatling.core.Predef._
import io.gatling.http.Predef._
class Next extends Simulation {
val httpProtocol = http
.baseUrl("someBaseURL")
.inferHtmlResources()
.userAgentHeader(
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36"
)
.proxy(Proxy("localhost", 8080))
val jsonFileFeederAdmins = jsonFile(
"/home/lukasz/Gattling/Skeleton/user-files/resources/data/adminLoginTemplate.json"
).circular
val jsonFileFeederJourneySteps = jsonFile(
"/home/lukasz/Gattling/Skeleton/user-files/resources/data/userJourneySteps.json"
).random
val jsonadminJourneyStepsSwagger = jsonFile(
"/home/lukasz/Gattling/Skeleton/user-files/resources/data/adminJourneyStepsSwagger.json"
).circular
val loginHeadres = Map(
"accept" -> "application/json",
"accept-encoding" -> "gzip, deflate, br",
"accept-language" -> "en-US,en;q=0.9,pl;q=0.8",
"access-control-expose-headers" -> "x-total-count",
"content-type" -> "application/json; charset=utf-8",
"sec-fetch-dest" -> "empty",
"sec-fetch-mode" -> "cors",
"sec-fetch-site" -> "same-origin"
)
var token = ""
def getToken() = {
repeat(1) {
feed(jsonFileFeederAdmins)
.exec(
http("Login admin and get token")
.post("/auth/login")
.headers(loginHeadres)
.body(
StringBody(
"""{
"userName": "${userName}",
"password": "${password}"
}"""
)
)
.check(
header("set-cookie")
.saveAs("token")
)
)
}.exec(session => {
token = session("token").as[String].slice(9, 380)
println("Token: Bearer " + token)
session
})
}
def adminJourney() = {
exec(_.set("token", token)) // Set it here
.repeat(5) {
feed(jsonadminJourneyStepsSwagger)
.exec(
http("${stepName}")
.get(
"${requestUrl}"
)
.header(
"authorization",
"Bearer " + "${token}"
)
.check(status.is(200))
)
}
}
val scn = scenario("Awesome journey")
.exec(getToken())
.exec(adminJourney())
.pause(10)
setUp(
scn.inject(rampUsers(3) during (5 seconds))
).protocols(httpProtocol)
}