Hi,
I’d like to use gatling for the following scenario:
- Get the jwt token from one url, for example, abc.com, in one scenario
- Use it in another scenario to test another url, for example def.com, in another scenario
How to do that?
I tried as the code snippet below, but it does not work.
Got the error:
'Get all books' failed to execute: No attribute named 'jwtToken' is defined
List<String> jwt = new ArrayList<>(1);
// Http Configuration
HttpProtocolBuilder bookStore = http
.baseUrl("https://abc.com/api")
.acceptHeader("application/json").contentTypeHeader("application/json");
HttpProtocolBuilder auth = http
.baseUrl("https://def.com")
.acceptHeader("application/json").contentTypeHeader("application/x-www-form-urlencoded");
// HTTP CALLS
ChainBuilder authenticate = exec(http("Authenticate")
.post("/oauth/token")
.formParam("client_id", "abc")
.formParam("client_secret", "def")
.formParam("grant_type", "client_credentials")
.formParam("request_type", "token")
.check(jmesPath("token").saveAs("jwtToken"))
.check(status().is(200)))
.exec(session -> {
jwt.add(session.getString("jwtToken"));
System.out.println("JWT: " + jwt);
return session;
});
ChainBuilder getAll = exec(
http("Get all books")
.get("/books")
.header("Authorization", "Bearer #{jwtToken}"));
ChainBuilder create = exec(http("Create book").post("/books")
.header("Authorization", "Bearer #{jwtToken}")
.body(ElFileBody("book.json")).asJson()
.check(status().is(201)));
ScenarioBuilder scnAuth = scenario("Auth")
.exec(authenticate);
ScenarioBuilder scn = scenario("Book Store Test")
.exec(s -> {
System.out.println("JWT to use: " + jwt.get(0));
s.set("jwtToken", jwt.get(0));
return s;
})
.exec(getAll)
.exec(create);
setUp(scnAuth.injectOpen(atOnceUsers(1)).protocols(auth).andThen(List.of(
scn.injectOpen(nothingFor(5), rampUsers(20).during(30))
.protocols(bookStore))));