Azure Load Testing Series - Accessing bearer token in Jmeter using JSON extractor
Series - azureloadtesting
Contents
About this blog
Hey folks!, In this blog we will see how to pass bearer token dynamically while running the test plans.
Mock API
In order to cover the scenarios, I have used a mock API that exposes token endpoint and a dummy GET endpoint.
Extract token within a thread group using JSON Extractor
Configuring GET endpoint with HTTP Header manager
- Create ThreadGroup and Add HTTP request , ThreadGroups->Add->Sampler->HTTP Request
- Create HTTP Request for GET endpoint : https://httpbin.org/anything/foo
-
Above GET endpoint requires an bearer token to authorize the call.
-
We can add the headers by adding config element under the HTTP Request option menu
- In the HTTP header manager add authorization key value as below
|
|
- ${auth_token} - We need to somehow get this bearer token dynamically to make sure we send the valid authorization header along with the GET endpoint
Configuring Token endpoint
- Now we need to follow the same steps of adding HTTP request and HTTP header manager as we did before for the POST endpoint : https://virtserver.swaggerhub.com/helen/oauth-token-mock/1.0.0/token
- Add below key vaule in the Headers manager
|
|
- Request body we will send along with the POST call are
- grant_type : password
- username : Steve Smith
- password : p$ssw0rd
- client_id : test
- client_secret : 99ae8af9-cf8a-4cb3-89b2-9a42b97762b2
- Add listener view result from HTTP request -> Add -> Listener -> View result tree
- Now click on Run for the Get access token endpoint and you can see the bearer token generated in the Response body
Create JSON extractor
- In order to make use of this token generated in other HTTP requests create a JSON extractor from HTTP Request->Add->Post Processors->JSON Extractor
- Provide “Names of created variables” and JSON Path expression as below
|
|
- Now we can pass on “auth_token” variable in another HTTP request within the same Thread Group
Request Headers
- Now we can see that the bearer token in the request headers
Next steps
In our next blog we will see how to pass access token dynamically between different thread groups
Join us
Please go ahead and join our discord channel (https://discord.gg/8Cs82yNS) to give some valuable feedbacks