How to refresh JWT token

Gatling version: 3.11.5
Gatling flavor: java kotlin scala javascript typescript
Gatling build tool: maven gradle sbt bundle npm

Hi,

I have a question about a simple simulation with Gatling.

Before a request will be send to a server an JWT token must be fetched and before five minutes the token should be refreshed.

    private final ScenarioBuilder scenario = scenario("Fetch order")
            .exec(
                    http("Authenticate")
                            .post(AUTHENTICATION_URL)
                            .asFormUrlEncoded()
                            .formParam("username", "username")
                            .formParam("password", "password")
                            .check(status().is(OK.code()))
                            .check(jsonPath("$.token").saveAs("token"))
            )
            .feed(feeder)
            .tryMax(3).on(
                    exec(
                            http("Fetch order")
                                    .get("#{auftragId}")
                                    .header(AUTHORIZATION, "Bearer #{token}")
                                    .header(ACCEPT, "application/json")
                    )
            );    private final ScenarioBuilder scenario = scenario("Fetch order")
            .exec(
                    http("Authenticate")
                            .post(AUTHENTICATION_URL)
                            .asFormUrlEncoded()
                            .formParam("username", "username")
                            .formParam("password", "password")
                            .check(status().is(OK.code()))
                            .check(jsonPath("$.token").saveAs("token"))
            )
            .feed(feeder)
            .tryMax(3).on(
                    exec(
                            http("Fetch order")
                                    .get("#{auftragId}")
                                    .header(AUTHORIZATION, "Bearer #{token}")
                                    .header(ACCEPT, "application/json")
                    )
            );

Now I spend my time to find out how to refresh the token efficently. But I did not found a solution.

Some sources I read was:

But at the end I didn’t get it.

    private final HttpProtocolBuilder httpProtocol = http
            .baseUrl(BASE_URL);
    {
        this.setUp(scenario.injectOpen(atOnceUsers(1))).protocols(httpProtocol);
    }

How to get a Gatling simulation to run where I have to create/refresh a JWT token and query a server for some data?

Thanks in advance,
Markus

Hi @markus

What did you implement?

I remember reviewing @GeMi’s solution and it was completely working.

Take care:

  • @GeMi’s solution is about a sharing token for all users
  • @GeMi’s solution contains an injection profile with different scenarios.

You may want to check if the time has come to refresh the token at every step in your scenario.
In that case, you will have to code an helper that surround a step with your verification (a comparison of time versus a value in the session) then perform refresh before the actual call to your step.

Does that help?

Cheers!

1 Like

Hi @sbrevet,

I let the example of @GeMi run on my machine and it works.

But where can I inject my code:


                  exec(
                            http("Fetch order")
                                    .get("#{auftragId}")
                                    .header(AUTHORIZATION, "Bearer #{token}")
                                    .header(ACCEPT, "application/json")
                    )

I see a scenario for creating the token (scnForSetFirstToken), I see the scnForRefreshToken stuff and how the token will be abort (scnForRefreshTokenAbort)

    {
        setUp(scnForSetFirstToken.injectOpen(atOnceUsers(1))
                .andThen(
                        scnForRefreshToken.injectOpen(atOnceUsers(1)),
                        scn.injectOpen(constantUsersPerSec(10).during(30))
                                .andThen(scnForRefreshTokenAbort.injectOpen(atOnceUsers(1)))
                )
        ).protocols(httpProtocol);
    }

But now I see where the scenario runs “scn”. I will rewrite it to my purpose and hope it will work.

I can rely on the fact that the scnForRefreshToken is working on the background.
In my understanding there are two scenarios are running to the same time?

Thanks a lot. I should got it.

Cheers,
Markus

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