Gatling - 运行 JAVA-每个虚拟用户编码一次
Gatling - running JAVA-code once per Virtual User
有没有办法 运行 每个用户一次这个“JWT-generator/http(“ConnectToken”)”?现在是 运行 一次,然后每次都使用相同的 JWT。
import java.io.File
import java.util.Properties
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import config.Config._
import util._
import headers.Headers._
object GetTokenRequest extends JwtGeneratorRSA{
val get_token = {
val feeder = csv("data/TestDataTest.csv")
val current = new File(".").getCanonicalPath
System.out.println("Current dir:" + current)
val currentDir = System.getProperty("user.dir")
System.out.println("Current dir using System:" + currentDir)
val props = new Properties
props.put("issuer","cfa9c8fb-8dd6-4d7c-b142-786c3d774f64");
props.put("audience",app_url+"/connect/token");
props.put("resource","testRes");
props.put("consumer_org","testOrg");
props.put("scope","openid profile helseid://scopes/identity/pid");
props.put("token.endpoint",app_url + "/connect/token");
props.put("keystore.file",current+"/src/test/resources/data/cfa9c8fb-8dd6-4d7c-b142-786c3d774f64.p12");
props.put("keystore.password","AYiPf1mZYOY60XV0tCuPIFk9UaGiOg1K");
props.put("keystore.alias","");
props.put("keystore.alias.password","");
// Generate Json Web Token
val jwt = makeJwt(props)
println("J W T : ")
println(jwt)
http("ConnectToken")
.post(app_url + "/connect/token")
.headers(headers_0)
.formParam("grant_type", "${grant_type}")
.formParam("client_assertion", jwt)
.formParam("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer")
.formParam("client_id", "${client_id}")
.formParam("scope", "${scope}")
.check(regex("access_token\":\"(.*?)\"").saveAs("access_token"))
}
}
执行是这样的:
.exec(GetTokenRequest.get_token)
我希望每个用户都运行 JWT 生成器。我猜问题出在这部分:
http("ConnectToken")
.post(app_url + "/connect/token")
.headers(headers_0)
.formParam("grant_type", "${grant_type}")
.formParam("client_assertion", jwt)
...
我看到这里有几个问题。问题之一是您在一种方法中混合了不同的逻辑和操作。你需要把它分解成小块。
现在让我们回到问题本身。为了为每个用户加载令牌,只需将其放在馈线中即可。你甚至在你的代码中有一个例子:
"${grant_type}"
"${client_id}"
现在就去做
"${jwt}"
有没有办法 运行 每个用户一次这个“JWT-generator/http(“ConnectToken”)”?现在是 运行 一次,然后每次都使用相同的 JWT。
import java.io.File
import java.util.Properties
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import config.Config._
import util._
import headers.Headers._
object GetTokenRequest extends JwtGeneratorRSA{
val get_token = {
val feeder = csv("data/TestDataTest.csv")
val current = new File(".").getCanonicalPath
System.out.println("Current dir:" + current)
val currentDir = System.getProperty("user.dir")
System.out.println("Current dir using System:" + currentDir)
val props = new Properties
props.put("issuer","cfa9c8fb-8dd6-4d7c-b142-786c3d774f64");
props.put("audience",app_url+"/connect/token");
props.put("resource","testRes");
props.put("consumer_org","testOrg");
props.put("scope","openid profile helseid://scopes/identity/pid");
props.put("token.endpoint",app_url + "/connect/token");
props.put("keystore.file",current+"/src/test/resources/data/cfa9c8fb-8dd6-4d7c-b142-786c3d774f64.p12");
props.put("keystore.password","AYiPf1mZYOY60XV0tCuPIFk9UaGiOg1K");
props.put("keystore.alias","");
props.put("keystore.alias.password","");
// Generate Json Web Token
val jwt = makeJwt(props)
println("J W T : ")
println(jwt)
http("ConnectToken")
.post(app_url + "/connect/token")
.headers(headers_0)
.formParam("grant_type", "${grant_type}")
.formParam("client_assertion", jwt)
.formParam("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer")
.formParam("client_id", "${client_id}")
.formParam("scope", "${scope}")
.check(regex("access_token\":\"(.*?)\"").saveAs("access_token"))
}
}
执行是这样的:
.exec(GetTokenRequest.get_token)
我希望每个用户都运行 JWT 生成器。我猜问题出在这部分:
http("ConnectToken") .post(app_url + "/connect/token") .headers(headers_0) .formParam("grant_type", "${grant_type}") .formParam("client_assertion", jwt) ...
我看到这里有几个问题。问题之一是您在一种方法中混合了不同的逻辑和操作。你需要把它分解成小块。
现在让我们回到问题本身。为了为每个用户加载令牌,只需将其放在馈线中即可。你甚至在你的代码中有一个例子:
"${grant_type}"
"${client_id}"
现在就去做
"${jwt}"