Reputation: 11
Can someone help? I'm saving response header with Bearer token value Then I'm putting it to the new request header How to convert 'id_token' to the proper format and paste to request?
Graphic: https://i.sstatic.net/7sN2f.jpg
The response from login request:
Content-Type: application/json; charset=utf-8
Connection: close
date: Wed, 17 Jun 2020 06:29:34 GMT
x-powered-by: Express
access-control-allow-origin: https://admin-test-demo-se.skeleton.sh
vary: Origin
access-control-allow-credentials: true
content-length: 41
set-cookie: id_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoie1wiSWRcIjo3OCxcIkZpcnN0TmFtZVwiOm51bGwsXCJMYXN0TmFtZVwiOm51bGwsXCJVc2VyTmFtZVwiOlwiR2F0bGluZyAxXCJ9IiwidW5pcXVlX25hbWUiOiJHYXRsaW5nIDEiLCJhdXRobWV0aG9kIjoiYXV0aGVudGljYXRpb24vZW1wbG95ZWUiLCJyb2xlIjoiQWRtaW4iLCJuYmYiOjE1OTIzNzUzNzQsImV4cCI6MTU5NDk2NzM3NCwiaWF0IjoxNTkyMzc1Mzc0fQ.UPrQNogh7PR4NqLSEBsBeNCnlCPA8ATZ6ttQZTcOO7A; Max-Age=172800; Path=/; Expires=Fri, 19 Jun 2020 06:29:34 GMT; HttpOnly
etag: W/"29-NA9GNaOnOGzxyvP55ORm4/m40kU"
access-control-expose-headers: x-total-count
x-vercel-cache: MISS
x-vercel-trace: bru1
server: Vercel
x-vercel-id: bru1::dub1::67695-1592375373291-bee974116ff3
strict-transport-security: max-age=63072000
cache-control: s-maxage=0
{"status":"ok","currentAuthority":"user"}
Code:
.feed(jsonFileFeederAdmins)
.feed(jsonFileFeederJourneySteps)
.exec(
http("request_0")
.post("/auth/login")
.headers(headers_0)
.body(
StringBody(
"""{
"userName": "${userName}",
"password": "${password}"
}"""
)
)
.check(header("set-cookie"))
.check(regex("""id_token=([^;]+)""").find.saveAs("id_token"))
// .saveAs("cookie")
.resources(
http("request_1")
.get("${requestUrl}")
.header(
"authorization",
"${id_token}"
),
http("request_2")
.get("${requestUrl}")
.header(
"authorization",
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoie1wiSWRcIjo3OCxcIkZpcnN0TmFtZVwiOm51bGwsXCJMYXN0TmFtZVwiOm51bGwsXCJVc2VyTmFtZVwiOlwiR2F0bGluZyAxXCJ9IiwidW5pcXVlX25hbWUiOiJHYXRsaW5nIDEiLCJhdXRobWV0aG9kIjoiYXV0aGVudGljYXRpb24vZW1wbG95ZWUiLCJyb2xlIjoiQWRtaW4iLCJuYmYiOjE1OTIzMTAxNjksImV4cCI6MTU5NDkwMjE2OSwiaWF0IjoxNTkyMzEwMTY5fQ.Qwrsd0BFU-nX2oetz3E3J3cWmCqWgMzE_ia3ThUIw5Q")
Upvotes: 0
Views: 1401
Reputation: 11
Sorry Stéphane but I was working on other approaches. This is my code that is working :)
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)
}
Upvotes: 1
Reputation: 6608
regex
is applied on the response body. You want to use use headerRegex to apply a regex on a response header.
Upvotes: 0