Blog

Testing Token Expiry Without Waiting for a Token to Expire

September 6, 2026Xerat023 min read
Share
Testing Token Expiry Without Waiting for a Token to Expire

Access tokens expire on purpose — usually somewhere between 15 minutes and a few hours for a typical OAuth or JWT-based setup — specifically so that a leaked token has a limited window of usefulness. That's a good security property and a genuinely annoying one to test, because "wait for the token to actually expire" is a real option that also means your test suite now takes as long as the token's lifetime, which nobody actually does.

What's supposed to happen when a token expires

The typical flow: an access token expires, the client makes a request, gets a 401, and — rather than surfacing that to the user as a failure — silently uses a longer-lived refresh token to get a new access token, then retries the original request. Done correctly, a user never notices their token expired at all; the app just quietly refreshes and moves on. Done incorrectly, a user gets logged out unexpectedly, or the app gets stuck in a retry loop, or (worse) it keeps using the expired token and the user sees a confusing wall of failed requests with no clear explanation.

That's a meaningful amount of logic — "notice the 401, pause other in-flight requests, refresh, retry, resume" — that either works or doesn't, and the only way to know is to actually watch it happen against a request that fails specifically because of expiry, not some other reason.

Why waiting for a real token to expire is a bad test strategy

Beyond just being slow, waiting for real expiry doesn't even test the thing reliably: you have no control over exactly when the 401 happens relative to other requests in flight, so you can't reliably exercise "two requests are in flight when the token expires, does only one refresh happen or do you race and refresh twice" — a real and common bug in refresh-token logic, where a naive implementation kicks off a fresh refresh call for every request that happens to 401 around the same time, when what should happen is one refresh, with every waiting request reusing its result.

What a controllable version of this looks like

The useful setup is a mock endpoint that returns 401 on command — not because a real token genuinely expired, but because you told it to, so you can trigger the exact scenario you want to verify:

  • A single expired-token 401, confirming the refresh-and-retry path works at all.
  • Two or more requests hitting 401 at close to the same moment, confirming your refresh logic coalesces into a single refresh call rather than firing one per failed request.
  • A refresh call that itself fails (refresh token also invalid or expired), confirming the app actually logs the user out and shows a real sign-in prompt, rather than looping forever trying to refresh a refresh token that will never succeed.

Conditional matching makes the first two straightforward — return 401 unconditionally for a route to force the always-expired case, or key it off a header your test harness controls to switch a specific request into "treat this token as expired" on demand, without touching a real auth provider or waiting on a real clock.

Getting refresh logic right matters more than it might seem, because it's exactly the kind of code that's invisible when it works and directly responsible for a wave of "randomly logged out" complaints when it doesn't — and it's also exactly the kind of code most teams write once, rarely revisit, and therefore rarely re-test after the one time it was first built.

X

Xerat02

Building MockBase.