Notes
Request and response
Every HTTP call is a request and a response. The request has a method, here GET, a path, headers like the host, and sometimes a body. The response has a status line, here 200 OK, headers like the content type, and a body. Ask ShopRung for product one and the body is JSON: the Clean Code Notebook, in Books, twelve fifty, forty in stock. An API test sends requests like this and checks every part of the response.
GET /api/products/1 HTTP/1.1
Host: localhost:8080
HTTP/1.1 200 OK
Content-Type: application/json
{"id":1,"sku":"BK-101",
"name":"Clean Code Notebook",
"category":"Books",
"priceCents":1250,"price":
"$12.50","stock":40,"inStock":true} Methods
The method says what the client wants. GET reads, like listing products. POST creates something or triggers an action, like adding to the cart or placing an order. PATCH changes part of something, like a cart line's quantity. PUT replaces a whole resource; ShopRung does not use it. DELETE removes, like a cart line. Use a method the API does not allow, like DELETE on a product, and ShopRung answers four hundred and five, method not allowed.
GET /api/products read
POST /api/cart/items create
or act
PATCH /api/cart/items/1 change
part
PUT (replace whole) not used
DELETE /api/cart/items/1 remove
wrong method → 405
DELETE /api/products/1 Safe and idempotent
Two properties matter to testers. A safe method does not change data: GET must never alter anything. An idempotent call gives the same result whether you make it once or twice: setting a cart line to two with PATCH, twice, still leaves two. POST usually is not idempotent: adding four notebooks twice gives eight. Test it: call twice and compare. A GET that changes data, or a payment POST that charges twice on a retry, are serious bugs.
- safe: does not change data GET (and HEAD, OPTIONS)
- idempotent: doing it twice = doing it once GET, PUT, DELETE, and PATCH quantity=2 here
- NOT idempotent: POST add 4 notebooks twice → 8 in the cart
- test: call twice, compare
Status codes
Status codes come in families. Two hundreds mean success. Three hundreds mean redirect. Four hundreds mean the client did something wrong: a bad request, not logged in, not allowed, not found. Five hundreds mean the server failed. A useful rule: when your test sends bad input, expect a four hundred something with a clear message. If the server answers five hundred, that is almost always a bug, because bad input should never crash a server.
- 2xx success 200 OK, 201 Created, 204 No Content
- 3xx redirect (301, 302, 304)
- 4xx the CLIENT is wrong 400, 401, 403, 404, 405, 409, 422, 429
- 5xx the SERVER failed 500, 502, 503, 504
- a 5xx in a test = usually a bug
Codes in ShopRung
ShopRung uses each code precisely, and tests should check that. Two hundred and one when an order is created. Four hundred for a quantity of zero or a body that is not JSON. Four oh one with no login token. Four oh three when a customer tries to ship an order. Four oh four for a product that does not exist. Four oh five for a method the path does not support. Four oh nine for conflicts, like asking for more stock than exists, or paying an order twice.
201 POST /api/orders (placed)
400 quantity 0: invalid_quantity
400 body "not json": bad_request
401 no token: unauthorized
403 customer ships: forbidden
404 /api/products/999
405 DELETE /api/products/1
409 add 4 of 3 left:
out_of_stock
409 pay a PAID order Headers
Headers carry information about the message. On requests, content type says the body is JSON, authorization carries the login token, and accept says which format you want back. On responses, check the content type, caching headers, the location of a newly created resource where an API provides it, and security headers. And check what should not be there: no internal server versions, stack traces or secrets leaking in headers or bodies.
- request Content-Type: application/json Authorization: Bearer <token> Accept: application/json
- response Content-Type: application/json Cache-Control, Location (201) security headers
- test: right type, no secrets
REST
REST is a style for designing HTTP APIs. Resources are nouns at URLs: orders, and order seven. Methods are the verbs: GET order seven. Calls are stateless: each request carries its own authentication, so the server does not remember you between calls. Data travels as JSON representations. Actions that are not simple create, read, update or delete become sub resources, like posting to an order's cancel. Query strings filter lists, like question mark q equals mug.
- resources are nouns at URLs /api/orders, /api/orders/7
- methods are the verbs GET /api/orders/7
- stateless: each call carries its own auth (the token)
- JSON representations
- actions that do not fit CRUD: POST /api/orders/7/cancel
- query strings filter: ?q=mug
What to check
What should every API test check? The exact status code. The body: the right fields, types and values, and for errors, both the error code and the message. Key headers. The side effects: after placing an order, read it back, check the stock went down, or query the database. That nothing else changed. The response time against an agreed limit. And the overall shape, with a JSON schema, which has its own episode in this part.
- status code (exact)
- body: fields, types, values error code AND message
- headers: content type
- side effects: GET the order back, check stock, the DB
- nothing else changed
- time: under the agreed limit
- schema: shape of the JSON
Negative tests
Most API bugs hide in requests the developer did not expect, because the UI never sends them. Try a missing field and an extra field. A string where a number belongs. An empty body and invalid JSON. Huge and negative values. A missing or broken token. Someone else's order id. The wrong method. The same request twice. Each should get a four hundred something with a clear message, never a five hundred and never silently wrong data.
- missing field, extra field
- wrong type: "4" instead of 4
- empty body, invalid JSON
- huge values, negative values
- missing or broken token
- someone else's id (ORD-7)
- wrong method, wrong path
- same request twice
- expect 4xx + clear message
Common mistakes
API testing habits to drop. Checking only that the status is two hundred. Not checking error codes and messages. Never reading the data back to confirm a change. Testing only the happy path. Testing through the browser what the API could test in a fraction of the time. Ignoring headers. And trusting a two hundred status whose body actually says error, which some badly designed APIs do. Always check the body too.
- checking only "status 200"
- not checking error messages
- never reading data back
- only happy-path requests
- testing through the UI what the API could test faster
- ignoring headers
- trusting 200 with an error inside the body
Say it like this
In the interview: I check the exact status code, the body's fields, values and schema, error codes and messages for bad input, key headers, side effects by reading the data back, and response time. Beyond the happy path, I test authentication and authorisation, idempotency by repeating calls, and negative inputs, expecting a four hundred something with a clear message, never a five hundred. Then name the codes you know: two oh one, four oh one versus four oh three, four oh nine.
- "The exact status code, the body's fields and values and its schema, error codes and messages for bad input, key
- headers, side effects by reading data back, and response time. I also test
- auth, idempotency and negative inputs, expecting
- 4xx, never 5xx."