I am trying to create a test that first loads a list of authenticated users into the Session.
Will something like this approach actually work?
iimport io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
object Authentication {
case class Client(key: String, secret: String, accessToken: String)
val authUrl = "https://api.example.com/oauth2/token"
val authRequestHeaders = Map(
"content-type" -> "application/x-www-form-urlencoded",
"accept" -> """*/*""",
"user-agent" -> """Gatling/3.6.1""",
"accept-encoding" -> """gzip, deflate, br"""
)
val httpProtocol = http.baseUrl(authUrl)
.headers(authRequestHeaders)
val clientFeeder = csv("data/env/clients.csv").eager
val clientList = List.empty[Client]
def authenticate() = {
foreach(clientFeeder.readRecords, "client") {
exec { session =>
http("auth")
.post(authUrl)
.formParamMap(Map(
"client_id" -> "#{client.clientId}",
"client_secret" -> "#{client.clientSecret}",
"grant_type" -> "client_credentials"
))
.check(jsonPath("$.access_token").saveAs("accessToken"))
val client = Client("#{client.clientId}", "#{client.clientSecret}", "#{accessToken}")
val newClientList = clientList :+ client
val newSession = session.set("clientList", newClientList)
session
}
}
}
}
Example clients.csv file:
clientId,clientSecret
abc123, xyz=
def456, abc=
ghi789, def=