I have the following code that makes a simple POST request. How it should work? Before I have made a request, I generate a testing value "orderName1"
and save it to the session. Next, I am using the function "create"
to make the request and using the generated value in a function. And it works fine! My request is successful. As result, I have an order with a dynamically generated name "Name_68hKl90G"
. But I want to save the id
, returned from the server into the session. I want to use "Name_68hKl90G"
as a key
into the session.
How it should work (session):
orderName1 -> Name_68hKl90G
Name_68hKl90G -> 63d80b25cb6c2a7e7e845e40
How it actually work (session):
orderName1 -> Name_68hKl90G
${orderName1} -> 63d80b25cb6c2a7e7e845e40
Code:
val generateStr = (symbols: Integer) => Random.alphanumeric.take(symbols).mkString
val prepareData = exec(
session => {
val newSession = session
.set("orderName1", "Name_" + generateStr(8))
newSession
}
)
def create(name: String) = exec(
http("Create order")
.post("/shop/order")
.body(StringBody(s"""{"name":"${name}"}""".stripMargin)).asJson
.check(status.is(201))
.check(jsonPath("$.id").saveAs(s"${name}"))
)
val userScn = scenario("Scenario")
.exec(
prepareData,
create("${orderName1}")
)
setUp(
userScn.inject(rampUsers(rampUpUserCount) during (rampUpDuration seconds)),
).protocols(httpProtocol)
It is no need to use session value as a key for other value. You can satisfy all needs by following code:
val generateStr = (symbols: Integer) => Random.alphanumeric.take(symbols).mkString
val prepareData = exec(
session => {
val newSession = session
.set("orderName1", "Name_" + generateStr(8))
newSession
}
)
def create(name: String) = exec(
http("Create order")
.post("/shop/order")
.body(StringBody(session => s"""{"name":"${session(name).as[String]}"}""".stripMargin)).asJson
.check(status.is(201))
.check(jsonPath("$.id").saveAs(s"${name}_id"))
)
def rename(name: String, newName: String) = exec(
http("Update order name")
.patch(session => s"/shop/order/${session(name + "_id").as[String]}")
.body(StringBody(session => s"""{"name":"${newName}"}""".stripMargin)).asJson
.check(status.is(200))
)
val userScn = scenario("Scenario")
.exec(
prepareData,
create("orderName1"),
rename("orderName1", "New name")
)
setUp(
userScn.inject(rampUsers(rampUpUserCount) during (rampUpDuration seconds)),
).protocols(httpProtocol)