What OAuth tokens are and why NeoLoad needs to handle them
OAuth tokens are credentials that allow NeoLoad to access protected APIs on behalf of a user or application. When you test an API that requires OAuth authentication, NeoLoad must obtain a token, store it, and send it with each request. Without proper token handling, your load tests will fail because the server will reject requests that lack valid credentials.
NeoLoad handles OAuth in two main ways: by extracting tokens from responses and reusing them across test iterations, or by requesting new tokens when old ones expire. The method you choose depends on whether your API uses short-lived tokens that refresh frequently or longer-lived tokens that persist across multiple requests.
Key Takeaways
- OAuth tokens must be extracted from the server response after login and stored as NeoLoad variables so they can be reused in subsequent requests.
- Use the Extract from Response feature to capture tokens automatically, then reference them in request headers using the variable syntax ${variable_name}.
- Token expiration requires you to either request a new token before it expires or build refresh logic into your test script to handle expired tokens gracefully.
- Different OAuth flows (authorization code, client credentials, implicit) require different extraction and storage approaches in NeoLoad.
- Test your token handling in a small pilot run before scaling to full load, because token limits or rate-limiting on the auth server can become a bottleneck.
Extracting tokens from OAuth responses
After your test sends a login or token request to the OAuth server, the response contains the token you need. NeoLoad's Extract from Response feature captures this token and stores it as a variable. Open the request that receives the token response, select the response body or headers, and define an extraction rule that pulls out the token value.
Most OAuth responses return tokens in JSON format, so you will use a JSON path or regular expression to locate the token. For example, if the response is {"access_token": "abc123xyz"}, your extraction rule targets the access_token field. Once extracted, NeoLoad stores the value in a variable — typically named something like oauth_token or access_token — that you can reference in later requests.
After extraction, verify the variable contains the token by checking NeoLoad's variable list or by adding a simple request that echoes the variable back. This step catches extraction errors early, before you run a full load test and waste time debugging why requests are failing.
Sending tokens in request headers
Once you have extracted the token into a variable, you must include it in the Authorization header of every API request that requires it. In NeoLoad, open each protected request and add a header named Authorization with the value Bearer ${oauth_token} (or whatever your variable is named). The ${} syntax tells NeoLoad to substitute the actual token value at runtime.
Some APIs use different header names or formats — for example, X-API-Token: ${oauth_token} or Authorization: Token ${oauth_token}. Check your API documentation to confirm the exact header name and format before building your test. Sending the token in the wrong header or with the wrong prefix will cause the server to reject the request as unauthorized.
If your test includes multiple requests to different endpoints, you can add the same Authorization header to each one. NeoLoad will substitute the token variable in every request, so you do not need to manually update each header if the token changes.
Handling token expiration and refresh
Short-lived OAuth tokens expire after a set time — often 15 minutes to an hour. If your load test runs longer than the token lifetime, requests will start failing with 401 Unauthorized errors. You have two options: request a new token before the old one expires, or detect expired tokens and refresh them on demand.
The simpler approach is to request a new token at regular intervals. For example, if tokens last 30 minutes, add a token request to your test script every 25 minutes. This keeps a fresh token in your variable throughout the test. The downside is that you are making extra requests to the auth server, which can become a bottleneck under heavy load.
The more sophisticated approach uses If/Else logic in NeoLoad to detect when a request fails with a 401 status code, then automatically request a new token and retry the failed request. This method uses fewer token requests but requires more complex script logic. Set up a conditional block that checks the response status, extracts a new token if needed, and repeats the request.
Configuring different OAuth flows in NeoLoad
OAuth has several flows, and each requires a slightly different setup in NeoLoad. The Authorization Code flow (most common for web apps) requires you to simulate a user login: send credentials to the auth server, extract the authorization code from the redirect, exchange the code for a token, and then use that token. This involves multiple requests in sequence, so your test script must follow the exact order.
The Client Credentials flow (common for server-to-server APIs) is simpler: send your client ID and secret directly to the token endpoint, extract the token from the response, and use it. This flow has no user login step, so your test script is shorter and faster to set up.
The Implicit flow (older, less common) returns the token directly in the redirect URL instead of in a response body. You must extract the token from the URL rather than from JSON, which requires a different extraction rule. If your API uses Implicit flow, check whether it has migrated to a newer flow — many have, because Implicit is considered less secure.
Testing token handling before running load tests
Before you scale your test to hundreds or thousands of users, run a small pilot with just a few virtual users to verify that token extraction and reuse work correctly. Watch the test output and check that each request includes a valid Authorization header and that responses do not contain 401 errors.
Pay attention to how many token requests the auth server receives. If you are requesting a new token for every virtual user, you may overwhelm the auth server before you even start testing your actual API. Some organizations rate-limit token requests or have a maximum number of active tokens per client. Running a pilot reveals these limits early, so you can adjust your test strategy — for example, by having multiple virtual users share a single token, or by spacing out token requests.
Also test what happens when a token expires mid-test. Intentionally let a token expire and verify that your refresh logic (if you have it) kicks in correctly. If you are not using refresh logic, confirm that the test fails gracefully rather than hanging or producing confusing error messages.
Common mistakes and how to avoid them
A frequent mistake is forgetting to extract the token at all. If you manually copy a token from a login response and hardcode it into your test script, the token will expire after a few minutes and all requests will fail. Always use NeoLoad's extraction feature to capture tokens automatically.
Another common error is using the wrong variable name or syntax. If you extract a token into a variable called my_token but reference it as ${oauth_token} in your headers, NeoLoad will send the literal string ${oauth_token} instead of the actual token value. Double-check that variable names match exactly, including capitalization.
A third mistake is not accounting for token expiration in long-running tests. A test that works fine for 5 minutes may fail after 30 minutes when tokens expire. Always include a token refresh strategy if your test will run longer than your token lifetime.
Finally, do not assume all virtual users can share a single token. Some APIs allow only one active token per client at a time, so if you try to use the same token across 100 virtual users, the server may invalidate it or reject duplicate requests. Test with a realistic number of virtual users to catch this issue.
Frequently Asked Questions
Can I use the same OAuth token for all virtual users in my load test?
It depends on your API. Some APIs allow one token to be used by multiple clients simultaneously, while others invalidate a token if it is used from different IP addresses or in parallel requests. Test with a small number of virtual users first to see whether the auth server accepts concurrent requests with the same token. If it does not, you will need to request separate tokens for each user or for groups of users.
What should I do if the OAuth server rate-limits token requests?
If the auth server rejects token requests with a 429 Too Many Requests error, you are requesting tokens too frequently. Increase the time between token requests, or have multiple virtual users share a single token if your API allows it. You can also contact the API provider to request a higher rate limit for load testing, or ask whether they have a separate staging environment with higher limits.
How do I extract a token from a response header instead of the response body?
In NeoLoad, create an extraction rule and select Extract from Response Headers instead of the response body. Then specify the header name (for example, X-Auth-Token) and NeoLoad will pull the token from that header. The rest of the process is the same: store it in a variable and reference it in subsequent requests.
What if my OAuth token is in a nested JSON structure?
Use a JSON path expression to navigate the nested structure. For example, if the response is {"data": {"access_token": "abc123"}}, your extraction rule should use the path data.access_token or $.data.access_token depending on NeoLoad's syntax. Consult NeoLoad's documentation for the exact JSON path format it supports.
Can NeoLoad handle OAuth 2.0 and OpenID Connect?
NeoLoad can handle OAuth 2.0 flows through manual extraction and variable substitution. OpenID Connect, which builds on OAuth 2.0, works the same way — extract the ID token or access token from the response and use it in subsequent requests. The main difference is that OpenID Connect responses may include additional claims in the token, but NeoLoad treats it as a standard token extraction.