Testing OAuth 2.0, JWTs and token expiry

Most auth testing stops at "a valid token works, an invalid one does not", which leaves the interesting half untested: the token that was valid when the request started and expired before it finished. The APIs below issue genuinely signed, genuinely short-lived tokens, so refresh and expiry are things that actually happen.

OAuth and JWT testing — request and response sequence Sequence diagram for OAuth and JWT testing: POST /auth/v1/oauth/authorize answers 200 OK. POST /auth/v1/oauth/token answers 400 Bad Request. GET /auth/v1/oauth/protected-resource answers 401 Unauthorized. POST /friends/v1/characters/bulk-delete answers 403 Forbidden. Your client Fun API Playground POST /auth/v1/oauth/authorize 200 OK Step 1: request an authorization code POST /auth/v1/oauth/token 400 Bad Request Step 2: exchange for a token (client_credentials / author… GET /auth/v1/oauth/protected-resource 401 Unauthorized A resource protected by a token from this flow (not the s… POST /friends/v1/characters/bulk-delete 403 Forbidden Bulk-delete characters by id (partial success supported)
Real endpoints from the catalog — every request above is served, and every status code shown is one that endpoint actually returns.

How to practise it

  1. Complete the client-credentials flow and call a protected endpoint with the token you were issued.
  2. Wait for a short-lived token to expire and confirm your client refreshes transparently instead of failing the user's request.
  3. Exchange a refresh token, then attempt to reuse the old one and read the rejection.
  4. Run the authorization-code flow with PKCE, then retry with the wrong code_verifier.
  5. Drive the device-code flow end to end — the one that matters for CLIs and TVs, and the one nobody tests.
  6. Send a well-formed but wrongly-signed JWT and confirm it is refused as firmly as a missing one.

Test 401 vs 403 with Python

This example uses the Friends API role-based demo tokens, which are separate from the OAuth tokens issued by Auth Playground. Install requests and run it with a fresh sandbox.

from uuid import uuid4
import requests

url = 'https://funapi.dev/api/friends/v1/characters/999999'
sandbox = {'X-Sandbox-ID': str(uuid4())}

missing = requests.delete(url, headers=sandbox, timeout=10)
viewer = requests.delete(url, headers={**sandbox, 'Authorization': 'Bearer qa-viewer-token'}, timeout=10)
admin = requests.delete(url, headers={**sandbox, 'Authorization': 'Bearer qa-admin-token'}, timeout=10)

assert missing.status_code == 401
assert viewer.status_code == 403
assert admin.status_code == 404
print('401 missing token, 403 forbidden role, 404 authorized but absent')

The target is deliberately absent. A missing token is unauthenticated; the viewer token is authenticated but cannot delete; the admin may delete but this resource does not exist.

APIs to try it on

Status codes involved

Other techniques

Getting started guide · All 55 mock REST APIs

Last updated