Get auth token with one user from one url and use it with multiple users to test another url

Hi,

I’d like to use gatling for the following scenario:

  1. Get the jwt token from one url, for example, abc.com, in one scenario
  2. 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))));

There is an old question relating about this behavior, check it out:

Hi @amidavidw,

Welcome aboard!

You can check the solution proposed by @GeMi: Token Refresh - Java - Example .
Does that suit your use case?

Cheers!

Hi @trinp, @sbrevet,

Thanks a lot for your help.
I got it working :smile:

After checking both of the solutions recommended,
I noticed that the code snippet in my post is very similar to GeMi’s solution.

The fix is to replace

        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);

with

        ScenarioBuilder scn = scenario("Book Store Test")
                .exec(s -> s.set("jwtToken", jwt.get(0)))
                .exec(getAll)
                .exec(create);

What is the difference between these two .exe(...) ?

Should we always use .exec(s -> s.set(...)) before and
.exec(s -> { ... return s;}) after the execution of the chanbuilder?

Thanks again!

Hi @GeMi

Sorry, only two mentions allowed in one post.
Please check my reply above.

Thank you

From:

See below:

1 Like

I see. Thank you very much

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.