What to check when testing an endpoint
An endpoint is checked in layers from the cheapest to the most expensive: status and time, headers, body, negative requests, security and system state. The first three layers cover one successful request, while negative requests, security and state get separate tests with their own data. The body is compared with the whole shape rather than one field, to catch an extra sensitive field such as a password hash.
API · Checklist
The test for an endpoint is green. How many layers of the response did it actually check?
A test for GET /v1/accounts/42 checks that the status is 200 and that a balance field exists. It has been green for a month. Then it turns out that the response had a passwordHash field all along, and the endpoint answered in three seconds under load. All of it was visible, nobody was looking: the check stopped at the first layer.
This lesson gives a short frame: which layers to check in every endpoint, in what order, and which of them belong to a single request and which to the whole system. The next lessons open these layers up in detail.
How it is built: the layers of a check
A request passes through several layers, and each of them can break independently of the others. That is why a check is best built from the bottom up: from the cheapest (did a response arrive at all) to the most expensive (did the system state change correctly).
The layers of checking one endpoint
Why this way. The status and the time can be checked without parsing the body, headers without parsing the schema, so these checks are cheap and go first: if the status is already wrong, checking the schema is pointless. Negative requests, security and state need separate requests and prepared data, so they are written as separate tests instead of being appended to one "happy" scenario.
Each layer answers its own question. Status: did the server understand what happened. Headers: does the client know how to read the response. Body: is the data what the contract (Swagger/OpenAPI) promises. Negative requests: does the server withstand garbage. Security: does a user see only their own data. State: is the system after the request what it was supposed to become.
Example: from "status and one field" to all layers
Before: expect(res.status()).toBe(200) and expect(body.balance).toBeDefined(). A test like that lets this response through:
{
"id": 42,
"owner": "Olena",
"balance": 1200,
"currency": "UAH",
"passwordHash": "$2b$12$..."
}
That is a leak of a sensitive field: a password hash must never be in the API at all. After: the test compares the body with the exact shape of the response, checks a header and a coarse time threshold.
test('GET account: every layer of the response', async ({ request }) => {
const started = Date.now();
const res = await request.get('/v1/accounts/42');
const ms = Date.now() - started;
expect(res.status()).toBe(200);
expect(res.headers()['content-type']).toContain('application/json');
expect(await res.json()).toEqual({
id: 42,
owner: expect.any(String),
balance: expect.any(Number),
currency: 'UAH',
}); // toEqual fails on an extra field such as passwordHash
expect(ms).toBeLessThan(1000); // a coarse threshold, not a benchmark
});
toEqual with a full object catches an extra field, a missing one and a type change, while toBeDefined on one field catches only the last. The time check here is deliberately coarse: in a single run it filters out obvious failures (seconds instead of milliseconds), and real measurements belong to a load test.
What to check depending on the method
A state-changing request (POST, PUT, PATCH, DELETE)
Besides the response layers I check layer 6: a repeated GET, related objects, side effects. Covered in the lesson "Beyond the response: side effects and system state".
A read (GET)
The state must not change: layer 6 turns into a check that the request changed nothing, plus cache headers.
Limits and alternatives
- Not every layer is needed in every test. The base set per endpoint is one "happy path" test with layers 1-3 and separate tests for layers 4-6. A test that checks everything at once fails with an unclear reason.
- Checking the body against a schema (OpenAPI, JSON Schema) can be automated for all endpoints together, while value and state checks stay hand-written.
- There are test generators from a contract (for example, Schemathesis) that cover layers 3-4 in bulk. They do not know business rules, so layers 5-6 stay with a person.
- If there is no documentation the frame still works: expectations come from the behaviour, and differences are clarified with the developer.
Common mistakes
- Stopping at the status. Defects in headers, types and extra fields go unnoticed, and the client fails in production.
- Checking one field instead of the shape. An extra sensitive field is not caught by any "field exists" check.
- Writing only positive tests. Negative requests find most validation defects, and without them layer 4 is empty.
- Not looking at the state after a write. A 201 only proves that the server said so.
- A strict time threshold on a shared environment. The test becomes flaky from network noise, so the threshold gets a margin and exact measurements go to load tests.
Check yourself
An endpoint returns 200, the Content-Type is right, the needed fields are there. Why is that not enough to call it tested?
Answer
Only layers 1-3 were checked, for one successful request. It is unknown what happens with bad data (layer 4), whether a user sees someone else's data (layer 5) and whether the state changed correctly (layer 6). Also the body may have contained extra sensitive fields if the check went field by field instead of covering the whole object.
Why are negative requests, security and state written as separate tests instead of being added to one?
Answer
Each needs its own data and its own request, and a failure of one must not hide the others. One big test stops at the first failure and does not show the rest. Also, the name of a separate test says at once what broke.