Get session variable in scala func

Hey community! I have a bunch of requests like:


val headers1 = Map(
"SomeHeader1" -> "${SomeHeader1}",
"SomeHeader2" -> "${SomeHeader2}",
"SomeHeader3" -> "${SomeHeader3}")

val headers2 = Map(
"SomeHeader4" -> "${SomeHeader4}",
"SomeHeader5" -> "${SomeHeader5}",
"SomeHeader6" -> "${SomeHeader6}")

val req1 = http("req")
.get("/some/path")
.headers(headers1)

val req2 = http("req")
.get("/some/path")
.headers(headers2)

req1 and req2 are the same except headers, so I want to have a common request which will sent either headers1 or headers2 in .headers section depending on some session variable, which I will define on scenario level.

val commonReq = http("req")
.get("/some/path")
.headers(hFunction())

Is it possible to get a session variable into a function like def hFunction(): Map[String, String] = {}, which will set appropriate header depending on some logic?

Any help would be appreciated.

Also, I tried to use session api, but can’t understand how to use code like session => session(“header”).asString not in exec section

понедельник, 22 ноября 2021 г. в 17:15:31 UTC+3, Niko Kovatch:

headers (plural) is one of the rare places where you can’t pass a function yet.

Thank you for your answer Stephane. Is there any elegant workaround, what do you think?

вторник, 23 ноября 2021 г. в 00:33:58 UTC+3, Stéphane Landelle:

There’s no way to use a function to pass the headers’ Map, sorry.

What you can do though is share the common request pieces:

val headers1 = Map(
“SomeHeader1” → “${SomeHeader1}”,
“SomeHeader2” → “${SomeHeader2}”,
“SomeHeader3” → “${SomeHeader3}”)

val headers2 = Map(
“SomeHeader4” → “${SomeHeader4}”,
“SomeHeader5” → “${SomeHeader5}”,
“SomeHeader6” → “${SomeHeader6}”)

val base = http(“req”)
.get("/some/path")

val req1 = base
.headers(headers1)

val req2 = base
.headers(headers2)

It sounds reasonable, thank you!

вторник, 23 ноября 2021 г. в 10:51:24 UTC+3, Stéphane Landelle: