替换 foreach 变量中的字符
Replace characters in foreach variable
代码如下:
def readEntityMultipleTimes(entityName: String, pathPrefix: String = "") = {
val plural = entityName + "s"
exec(http(s"Geting all $plural")
.get(pathPrefix + plural)
.check(status is 200)
.check(jsonPath("$[*].id").findAll.saveAs("entityIds"))
).exec(s => {
if (logLevel >= 2) println("\nids:\n" + s("entityIds"))
s
})
.pause(interval millis)
.foreach("${entityIds}", "entityId") {
repeat(readEntityNumber) {
exec(http(s"Getting one $entityName")
.get(pathPrefix + plural + "/${entityId}")
.check(status is 200)
)
}
}
}
问题是 entityId
可能包含 space 并且它使 HTTP GET 请求失败。我需要将 space 替换为 %20
。
我试过加特林 EL ${entityId.replaceAll(\" \", \"%20\")}"
或 ${java.net.URLEncoder.encode(entityId)}
我想建议的方法是从会话中获取 entityId
并在 Scala 中执行这些操作,但是这个变量是为每次循环迭代动态创建的,所以我不确定将 "session lambda"(会话 => ...)
Gatling EL 语法有限,您不能在其中放置任何 Scala 代码。
你确实要传递一个函数。
.get(session => pathPrefix + plural + URLEncoder.encode(session("entityId").as[String])))
代码如下:
def readEntityMultipleTimes(entityName: String, pathPrefix: String = "") = {
val plural = entityName + "s"
exec(http(s"Geting all $plural")
.get(pathPrefix + plural)
.check(status is 200)
.check(jsonPath("$[*].id").findAll.saveAs("entityIds"))
).exec(s => {
if (logLevel >= 2) println("\nids:\n" + s("entityIds"))
s
})
.pause(interval millis)
.foreach("${entityIds}", "entityId") {
repeat(readEntityNumber) {
exec(http(s"Getting one $entityName")
.get(pathPrefix + plural + "/${entityId}")
.check(status is 200)
)
}
}
}
问题是 entityId
可能包含 space 并且它使 HTTP GET 请求失败。我需要将 space 替换为 %20
。
我试过加特林 EL ${entityId.replaceAll(\" \", \"%20\")}"
或 ${java.net.URLEncoder.encode(entityId)}
我想建议的方法是从会话中获取 entityId
并在 Scala 中执行这些操作,但是这个变量是为每次循环迭代动态创建的,所以我不确定将 "session lambda"(会话 => ...)
Gatling EL 语法有限,您不能在其中放置任何 Scala 代码。
你确实要传递一个函数。
.get(session => pathPrefix + plural + URLEncoder.encode(session("entityId").as[String])))