Which status to expect in typical scenarios
The expected status comes from the contract (OpenAPI, documentation) and is written before the first run, not adjusted to the server's answer. If the contract is silent, rely on RFC 9110 semantics and ask developers. Typically: creation 201 with Location, deletion 204, duplicate 409 or 422, wrong type 400, someone else's resource 404 or 403. Also check headers and unchanged state after a negative case.
API · Status codes
A user was created twice with the same email. Which status should you expect?
This is a typical scenario question. There is a risk of answering with what you see in Postman and calling it the expectation: the server returned 200, so that must be the intent. Then the test merely records behaviour instead of checking it. The expected status must be known before the run, and there is an order of sources for that. Below: where to take it from, how to turn it into a test, and a table of typical scenarios.
How to determine the expected status
Three sources, in order of priority. The contract (OpenAPI, documentation, team agreements) always comes first. If the contract is silent, take the semantics of RFC 9110: what the code means by the standard. If there is nothing there either, rely on established practice and record the assumption as a question to the developers. A mismatch is visible only if the expectation was written down in advance, so fix it before the first run.
An expectation before the run and fitting to the response
Ran it and wrote down what came back
The test is green under any behaviour, including 500 or 200 on a duplicate. It checks nothing.
An expectation from the contract before the run
A mismatch is a defect or an inaccuracy in the documentation. Both outcomes are useful, both must be recorded.
| Situation | Status |
|---|---|
| A resource was created | 201 and Location |
| Updated, a body is returned / not returned | 200 / 204 |
| Deleted | 204; a repeated delete is 404 or 204 by the contract |
| A long operation, the result comes later | 202 and Location of the job |
| A duplicate of a unique field, a state conflict | 409 (some APIs give 422 here) |
| Broken JSON, an invalid type | 400 |
| A syntactically correct body breaks a rule | 422 |
| Someone else's resource | 404 (does not reveal existence) or 403, as in the contract |
Someone else changed the resource (If-Match) | 412; if If-Match is required and missing, 428 |
| The body format is not supported | 415 |
The client asks for a format that does not exist (Accept) | 406 |
| The body is too large | 413 |
| The method is not allowed for the address | 405 and Allow |
| Too many requests | 429 and Retry-After |
| The server is unavailable, maintenance | 503 and Retry-After |
| A gateway got no response from the server | 502 (invalid response) or 504 (timeout) |
Why it is built this way. Each row has a reason, not just "that is customary". Conditional requests (If-Match) exist so that two clients do not overwrite each other's changes: 412 says the version is stale, 428 says the client sent no condition at all. 202 says the work was accepted but not done, and the client has to poll the job. 405 together with Allow hints which methods are permitted, and 429 and 503 with Retry-After say when to retry.
Example: a duplicate email
The scenario from the question: create a user twice with the same email. Suppose the contract says: the first is 201, the second is 409 with a description of the conflict.
POST /v1/users HTTP/1.1
Content-Type: application/json
{ "email": "olena@example.com", "name": "Olena" }
HTTP/1.1 409 Conflict
Content-Type: application/problem+json
{ "title": "Email already registered", "status": 409 }
Before: the test reproduced what it saw, expect(second.status()).toBe(200), because that is how the server answered. But 200 means the server "created" a duplicate or silently overwrote an existing record. After: the expectation 409 comes from the contract, and the test fails, exposing the mismatch. The same logic works for conditional requests:
import { test, expect } from '@playwright/test';
test('PUT with a stale If-Match gives 412', async ({ request }) => {
const url = '/v1/orders/17'; // 412 is written from the contract, before the run
const etag = (await request.get(url)).headers()['etag'];
await request.put(url, { headers: { 'If-Match': etag }, data: { qty: 2 } });
const res = await request.put(url, {
headers: { 'If-Match': etag }, // the resource has changed since
data: { qty: 3 },
});
expect(res.status()).toBe(412);
});
The 412 test has three steps: get the version (ETag), change the resource, and repeat the request with the old version. The expected 412 is written from the contract before you have seen the response. For 405 and 429 the headers are checked too:
HTTP/1.1 405 Method Not Allowed
Allow: GET, POST
HTTP/1.1 429 Too Many Requests
Retry-After: 30
Allow is mandatory here (RFC 9110), Retry-After is expected in rate-limited responses. A test that checks only the code misses a missing header the client relies on.
Limits and alternatives
- The table is typical, not mandatory. For a duplicate 409 and 422 are equally legitimate. What is not acceptable is an API answering differently on neighbouring endpoints.
- A repeated delete (DELETE is idempotent by semantics) may give 204 or 404: both fit the idea that the final state is the same. What matters is that it is written in the contract.
- Long operations are sometimes easier to run synchronously, but then gateway timeouts give 504. For 202 you also test polling the job and the final status.
- The body is also part of the expectation: a 4xx with an empty body makes the client's life harder, and a 5xx on a client error is always a defect.
Common mistakes
- Writing the expectation after the run. The test becomes a snapshot of behaviour, not a check.
- Not checking that the resource was not created. A 409 on a duplicate while the database still got a second record: the status is right, the state is corrupted. After a negative case, look at the state.
- Ignoring accompanying headers. A 405 without
Allow, a 429 withoutRetry-After, a 201 withoutLocationbreak clients even though the code is right. - One "universal" code for all errors. A 400 for everything, from bad JSON to a duplicate, deprives the client of the chance to react differently.
Check yourself
Scenario: a repeated DELETE of the same resource. The test expects 404, the API returns 204. A bug or not?
Answer
Not necessarily. Both responses are compatible with idempotency: after the first delete the resource state is the same. The contract decides. If it says 404 and the API gives 204, it is a mismatch to fix or to document. If the contract is silent, the question goes to the developers.
A PUT without an If-Match header where the server requires a conditional request. Which status, and what is this code for?
Answer
428 Precondition Required. The server demands a condition to prevent a "lost update", when two clients overwrite each other's changes. If the condition is present but the version is stale, that is 412.
Location, deletion 204, invalid data 400 or 422, a duplicate email 409, someone else's resource 404 or 403. Middle/Senior: I take the expected status from the contract and write it down before the run, not fit it to the response. If the contract is silent I rely on RFC 9110 and raise a question. Besides the code I check the headers (Location, Allow, Retry-After) and the state after a negative case. For a duplicate email the right answer is 409 or 422 by the contract, not 500 and not 201.