Test API caching headers: Cache-Control, max-age and Vary
Caching bugs come in two kinds. The harmless-looking kind: a price list cached for a day after the price changed. The dangerous kind: a CDN caches GET /me and the next visitor sees someone else's profile.
Both come down to a few response headers, and both are easy to test.
Sort your endpoints
| Kind | Examples | Expected Cache-Control |
|---|---|---|
| Public | /products, /countries, /openapi.json | public, max-age=300 (or whatever you decided) |
| Private | /me, /orders, /invoices/{id} | private, max-age=0 or no-store |
| Never cache | /auth/token, /payments, one-time links | no-store |
Put them in separate folders of the collection. The folder is where the caching rule lives, so you set it once.
Assertions per folder
On the Public folder's Tests tab:
| Source | Path | Operator | Expected |
|---|---|---|---|
| Header | Cache-Control | contains | public |
| Header | Cache-Control | contains | max-age= |
On the Never cache folder:
| Source | Path | Operator | Expected |
|---|---|---|---|
| Header | Cache-Control | contains | no-store |
Every request in a folder inherits its assertions, including requests added later. A new endpoint dropped into Never cache is checked from its first run.
Check the number, not just the word
A max-age that's too long is a bug too. A folder-level post-response script can hold the rule:
const cc = pm.response.headers.get('Cache-Control') || '';
const m = /max-age=(\d+)/.exec(cc);
pm.test('public responses are cached between 1 and 60 minutes', () => {
pm.expect(m, 'max-age is set').to.be.ok;
pm.expect(Number(m[1])).to.be.within(60, 3600);
});Private never means public
On the Private folder:
const cc = (pm.response.headers.get('Cache-Control') || '').toLowerCase();
pm.test('per-user data is not publicly cacheable', () => {
pm.expect(cc).to.not.include('public');
pm.expect(cc).to.match(/private|no-store|no-cache/);
});
pm.test('no shared-cache lifetime on per-user data', () => {
pm.expect(cc).to.not.match(/s-maxage=\d+/);
});A missing Cache-Control on a private endpoint isn't automatically safe: some CDNs apply a default TTL to anything without one. Treat "missing" as a failure here.
Vary
If the same URL returns different bodies depending on a request header, caches must know:
- Localized responses depend on
Accept-Language. - Responses that differ for signed-in users depend on
AuthorizationorCookie. - Compressed responses depend on
Accept-Encoding(your server usually handles this).
pm.test('localized response varies by language', () => {
pm.expect((pm.response.headers.get('Vary') || '').toLowerCase()).to.include('accept-language');
});Errors shouldn't be cached for long
A 500 or 404 cached for an hour turns a one-minute outage into a one-hour outage. Send a request that you know fails (an id that doesn't exist on a public endpoint) and check:
pm.test('errors are not cached for long', () => {
const m = /max-age=(\d+)/.exec(pm.response.headers.get('Cache-Control') || '');
pm.expect(m ? Number(m[1]) : 0).to.be.at.most(60);
});Conditional requests
max-age decides how long a copy is fresh. After that, a client can ask "has it changed?" with If-None-Match and get a cheap 304. That part is covered in testing ETags and conditional requests.
Test through the CDN too
Headers your app sets can be changed by the CDN or gateway in front of it. Run the same folder against the public hostname, not only against the service directly:
requestarium run-collection "Catalog API" --folder "Caching" --env Production --junit caching.xml