Module 2. Advanced testing · Lesson 12 of 41 · 14 min
API testing in depth: status codes, schemas, idempotency
What to check in every request, contracts and JSON Schema, authorization, pagination and idempotency.
Why APIs are tested separately from the UI
API tests are 10–100 times faster than UI tests, break less when the layout changes and point precisely at the defect. Most business logic lives on the back end — and that's where it's most efficient to test it.
Checklist for one endpoint
| What | Example check |
|---|---|
| Status | 201 on create, 200 on read, 204 on delete |
| Schema | All required fields present, correct types, nothing extra |
| Values | Order total = price × quantity − discount |
| Validation | Empty, negative, very long, wrong type → 400 with a clear error |
| Authentication | No token → 401, expired token → 401 |
| Authorization | Someone else's resource → 403 or 404, never 200 |
| Idempotency | Repeating a request with the same key doesn't create a duplicate |
| Timing | p95 within the requirement |
JSON Schema — the contract in code
A schema describes what the response body must look like. Validating against it catches a broken contract for every field at once:
{
"type": "object",
"required": ["id", "status", "total"],
"properties": {
"id": { "type": "integer" },
"status": { "enum": ["pending", "paid", "cancelled"] },
"total": { "type": "number", "minimum": 0 }
}
}
Idempotency and retries
Networks are unreliable: a client may send a request twice. By the standard, GET, PUT and DELETE are idempotent — repeating them doesn't change the result. POST isn't, so payment APIs (Stripe is the classic example) use an Idempotency-Key header. Test: send the same POST twice with the same key → one order.
Pagination and filters
- First, last and empty page; page size 0, 1, the maximum and above the maximum.
- Stable sorting: an item never shows up on two pages.
- Filters combine correctly; special characters in search don't break the request.
API-level security
The most common vulnerability is IDOR: change /orders/9917 to /orders/9916 and you see someone else's order. Check every endpoint that takes an ID as a different user. More in the security lesson.
Test data for your requests comes from the test data generator; HTTP status code basics are in the web and API lesson.
Key takeaways
- For each endpoint, check status, schema, values, validation, authentication and authorization.
- JSON Schema catches a broken contract for every field at once.
- POST isn't idempotent — payments need an Idempotency-Key and a retry test.
Check yourself
1. A request for someone else's order returns 200 with the data. What is it?
2. Which method is not idempotent by the standard?
3. What does JSON Schema validate?