Test ETags and conditional requests: 304 Not Modified and 412 Precondition Failed
An ETag is a fingerprint of a resource's current version. Clients send it back in two ways: If-None-Match on a read ("only send it if it changed") and If-Match on a write ("only apply this if nobody changed it since I read it"). The first saves bandwidth. The second prevents two people from silently overwriting each other's edits. Both are easy to half-implement, so they're worth a test.
1. Read and keep the ETag
GET {{baseUrl}}/articles/{{articleId}}const etag = pm.response.headers.get('ETag');
pm.test('has an ETag', () => pm.expect(etag).to.be.a('string').and.not.empty);
pm.environment.set('etag', etag);ETags are quoted strings, like "a1b2c3", or weak ones, like W/"a1b2c3". Send them back exactly as received, quotes included.
2. Read again: 304
GET {{baseUrl}}/articles/{{articleId}}
If-None-Match: {{etag}}pm.test('not modified', () => pm.response.to.have.status(304));
pm.test('no body', () => pm.expect(pm.response.text()).to.equal(''));
pm.test('same ETag', () => pm.expect(pm.response.headers.get('ETag')).to.equal(pm.environment.get('etag')));A server that answers 200 here works, but wastes every client's bandwidth. One that answers 304 with a body breaks the HTTP spec, and some clients.
3. Update with the right version
PATCH {{baseUrl}}/articles/{{articleId}}
If-Match: {{etag}}
Content-Type: application/json
{ "title": "Updated title" }pm.test('updated', () => pm.expect(pm.response.code).to.be.oneOf([200, 204]));
const next = pm.response.headers.get('ETag');
pm.test('new ETag', () => pm.expect(next).to.not.equal(pm.environment.get('etag')));
pm.environment.set('oldEtag', pm.environment.get('etag'));
pm.environment.set('etag', next);4. Update with a stale version: 412
This is the one that matters. Someone else updated the article; your copy is stale:
PATCH {{baseUrl}}/articles/{{articleId}}
If-Match: {{oldEtag}}
Content-Type: application/json
{ "title": "My stale edit" }pm.test('stale write refused', () => pm.response.to.have.status(412));Then read the article and check the title is still Updated title. A 412 that wrote anyway is worse than no check at all.
5. Edge cases worth one request each
| Request | Expected |
|---|---|
If-None-Match: * on a GET of an existing resource | 304 |
If-Match: * on a PUT to a resource that doesn't exist | 412 |
If-None-Match: * on a PUT that creates | 201; again → 412 |
A write with no If-Match, when your API requires it | 428 Precondition Required |
Two different representations (Accept: application/json vs text/csv) | different ETags |
Don't forget Last-Modified
Some APIs use Last-Modified and If-Modified-Since / If-Unmodified-Since instead. The tests are the same with dates. Its one-second precision means two quick updates can share a timestamp, which is why ETags are preferred for writes.