Testing checklist by method: what to check for GET, POST, PUT, PATCH, DELETE
For each HTTP method check the response (code, headers, body) and the server state afterwards, read with a separate GET. POST: 201, Location, the resource visible via GET, validation, a repeat. PUT: full replacement. PATCH: only the sent field changes. DELETE: a follow-up GET returns 404 and neighbours stay intact. An unsupported method gives 405 with Allow, and GET changes nothing.
API · Methods
What to check for POST /orders so the interview answer is more than "status 201"?
In an interview they ask: "what will you check for POST /orders?". A weak answer: "status 201". A strong one starts from the method: what it guarantees, what must change on the server, what must not. This article gives such a grid for five methods and shows how it turns into automated tests.
How it works
A method defines two kinds of expectations. The first is the response: code, headers, body. The second is the server state: what changed or did not change after the request. Both must be checked, because a response without a state change means "200, but nothing is saved", and a state change without a proper response means the client never learns the result. The state is checked with a separate request, usually GET.
| Method | What to check |
|---|---|
| GET | Does not change state (a repeat gives the same). 200 for an existing id, 404 for a missing one. Filters and pagination. An empty collection gives 200 and []. Someone else's resource is unavailable. |
| POST | 201 and Location (or a body with an id). The created resource is visible through GET. Validation of required fields and types. A duplicate of a unique field. A repeated POST creates a second resource unless there is an idempotency key. |
| PUT | Full replace: a field that was not sent is reset. A repeat gives the same state. A missing resource is either created (201) or gives 404 by the contract. |
| PATCH | Only the sent field changes, the rest stays. null versus an absent field. An empty body {}. A read-only field (id, createdAt) is not changed. |
| DELETE | Success is usually 204. The next GET gives 404. A repeat gives 404 or 204 by the contract. Related data is handled by the rule. Someone else's resource is not deleted. |
The table is complemented by a "base grid" of cases common to every endpoint: the happy path; missing authentication (401); someone else's resource (403 or 404 by policy); an invalid body (400 or 422); an unknown id (404); a repeat of the same request.
Example: from the grid to tests
It is convenient to keep the grid as a table instead of copying a test. A new case costs one row:
const cases = [
{ name: 'GET existing', method: 'get', path: '/v1/orders/17', status: 200 },
{ name: 'GET unknown id', method: 'get', path: '/v1/orders/999999', status: 404 },
{ name: 'DELETE collection', method: 'delete', path: '/v1/orders', status: 405 },
{ name: 'PATCH unknown id', method: 'patch', path: '/v1/orders/999999', status: 404 },
] as const;
for (const c of cases) {
test(c.name, async ({ request }) => {
const res = await request[c.method](c.path, { data: c.method === 'patch' ? {} : undefined });
expect(res.status()).toBe(c.status);
if (c.status === 405) expect(res.headers()['allow']).toBeTruthy();
});
}
For DELETE the consequences matter more than the code. The test checks three things: the resource is gone, a repeat does not break, neighbours are not harmed:
test('DELETE: gone, repeatable, no collateral damage', async ({ request }) => {
const { id } = await (await request.post('/v1/orders', { data: { sku: 'A1', qty: 1 } })).json();
const neighbour = await (await request.post('/v1/orders', { data: { sku: 'B2', qty: 1 } })).json();
expect((await request.delete('/v1/orders/' + id)).status()).toBe(204);
expect((await request.get('/v1/orders/' + id)).status()).toBe(404);
// the repeat may be 204 or 404 by contract, but never 500 and never a second effect
expect([204, 404]).toContain((await request.delete('/v1/orders/' + id)).status());
// a neighbour created by the test is untouched
expect((await request.get('/v1/orders/' + neighbour.id)).status()).toBe(200);
});
The repeat allows 204 or 404 because contracts differ, but never 500 and never a second side effect. "The neighbouring resource is intact" catches a dangerous bug where a DELETE without an id filter removes too much.
404 or 405 on DELETE of a collection
404 Not Found
Misleading: the /orders collection exists, the method is simply not allowed for it.
405 Method Not Allowed
Describes the problem exactly. The Allow header lists the permitted methods, for example GET, POST.
Limits and particulars
- Method safety. Only POST, PUT, PATCH, DELETE change state. A GET that changes something (
GET /orders/17/cancel) is a defect: a prefetch, a crawler or a cache will call it. The check is simple: GET twice and compare the state. - PUT to a missing resource. The API may create it (201) or answer 404. Both are legitimate, but must be documented and the same across endpoints.
- Someone else's resource. Answering 403 or 404 is a policy decision: 404 does not reveal that the resource exists. A test compares with the contract.
- Races. Two simultaneous PATCHes on one resource and a check that there is no lost update (
If-Match, a version).
Common mistakes
- Checking only the code. A POST test with 201 but no GET after creation misses "created, but not saved".
- Not checking a repeat. Duplicates and lost updates show up exactly on a repeated request.
- Skipping 405. A server that answers 500 to an unknown method has a bug that one test line catches.
- Taking expectations from your head. The code and behaviour of each method are checked against the service contract.
Check yourself
For PATCH /users/7 with the body {} the server returned 200. What to check?
Answer
Whether the state changed (GET before and after): an empty change changes nothing. The contract alternative is 400 or 422 as "nothing to change". The main thing: no 500 and no fields reset.
Why is it not enough to check 201 after POST?
Answer
Because 201 is only the server's claim that the resource was created, not proof. Read Location or the id, do a GET and make sure the resource exists with the fields that were sent, not merely with the right status.
Which status to expect when calling DELETE /orders without an id?
Answer
405 Method Not Allowed with an Allow header if deleting the whole collection is not supported. 404 is imprecise and 500 is a defect.
Location, that the order is visible through GET, validation of required fields, a duplicate and access without a token. Middle/Senior: I start from the method: what must change in the state and what must not. For POST I add a repeated request and an idempotency key, for PUT a full replace, for PATCH the difference between null and an absent field, for DELETE a repeat, the effect on related data and that neighbours are intact. An unsupported method must give 405 with Allow, and a GET must not change data.