Testing rate limits and 429 responses
A rate limit you've never tested is a guess. Maybe it kicks in at 100 requests a minute, maybe at 10,000, maybe it was turned off in the last config change. And when it does kick in, clients need a clear 429 Too Many Requests with a hint about when to retry, not a 500 or a hanging connection.
Here's how to test both sides: that the limit works, and that your tests don't trip over it.
What a good 429 looks like
HTTP/1.1 429 Too Many Requests
Retry-After: 30
RateLimit-Limit: 100
RateLimit-Remaining: 0
RateLimit-Reset: 30
Content-Type: application/json
{"error": "rate_limited", "message": "Too many requests. Try again in 30 seconds."}- Status
429, not403or503. Clients and proxies treat these differently. Retry-After, in seconds or as a date, so clients know how long to wait.RateLimit-Limit,RateLimit-RemainingandRateLimit-Reset(or the olderX-RateLimit-*names) on every response, so clients can slow down before they hit the wall.- A JSON body that says what happened, in the same error format as the rest of your API.
Test the headers on a normal response
Start with the easy part. On any request that's rate-limited, check the headers are there and make sense:
pm.test('rate limit headers are present', () => {
pm.response.to.have.header('RateLimit-Limit');
pm.response.to.have.header('RateLimit-Remaining');
});
pm.test('remaining is within the limit', () => {
const limit = Number(pm.response.headers.get('RateLimit-Limit'));
const remaining = Number(pm.response.headers.get('RateLimit-Remaining'));
pm.expect(remaining).to.be.within(0, limit - 1);
});Trigger the limit
To see a 429, you have to send more requests than the limit allows. Do it against a test setup with a low limit: a staging environment with the limit set to, say, 10 per minute, or an API key on a low tier.
Put one request in a folder called Rate limit, with this post-response script:
const code = pm.response.code;
pm.test('either allowed or rate limited, nothing else', () => {
pm.expect(code).to.be.oneOf([200, 429]);
});
if (code === 429) {
pm.test('429 says when to retry', () => {
pm.response.to.have.header('Retry-After');
pm.expect(Number(pm.response.headers.get('Retry-After'))).to.be.above(0);
});
pm.test('429 has an error body', () => {
pm.expect(pm.response.json().error).to.equal('rate_limited');
});
}Then run it more times than the limit:
requestarium run-collection "Orders API" --folder "Rate limit" --env RateLimitTest --iterations 15Read the results: the first ten should be 200 and the rest 429. If all fifteen are 200, the limit isn't working.
Test recovery
A limit that never lifts is a bug too. After the run above, wait for the window to pass and check that requests are allowed again. In CI, that's a second command after a pause:
requestarium run-collection "Orders API" --folder "Rate limit" --env RateLimitTest --iterations 15
sleep 60
requestarium run "Get orders" --env RateLimitTestThe last command exits non-zero if its tests fail, so a limit that doesn't reset fails the job.
Keep normal runs under the limit
The opposite problem is more common: your regular test suite starts failing with 429 once it grows, or when two CI jobs run at once. Three fixes, from simplest:
- Slow down.
--delay 200waits 200 ms between requests. - Use a test key with a higher limit for CI, if your API or provider offers one.
- Don't retry
429blindly. The CLI's--retriesonly retries502,503and504(and requests that couldn't be sent), on purpose: a429usually means the test setup is wrong, and retrying hides it.
requestarium run-collection "Orders API" --env Staging --delay 200Per-user and per-key limits
If limits are per user or per API key, test that one user's traffic doesn't block another. Run the rate limit folder with user A's token until it's limited, then send one request with user B's token in a second environment. User B should get 200.