Course outline

API Testing / Basic: trustworthy API observations / Lesson 7

Connect API responses to resource state

Verify status, response fields and state effects for valid and invalid synthetic requests.

About 4 min readContent reviewed
On this page

Lesson goals

  • Define a response and state contract.
  • Derive independent valid and invalid cases.
  • Inspect side effects without exposing sensitive data.

Define a complete response promise

An API response is more than a status code: a contract can specify headers, body structure and effects on a resource. Begin with the documented behavior for a synthetic enrollment request and agree what should happen for a valid and an invalid input. Do not invent a universal error status for every API. Record the service revision, contract version and isolated environment. The aim is to connect each observation to the specific promise it can support.

Prepare independent synthetic cases

Create synthetic users and records owned by the test, and calculate expected values independently from the agreed rule. For a valid request, define the response and exactly one intended record. For an invalid request, define the documented error and unchanged state. The first table summarizes the fictional lab contract; it is not a real execution result. If the business rule or error format is unresolved, ask its owner rather than copying the implementation’s output into the expected result.

text Synthetic API contract
Case | Status | State
valid request | documented success | one synthetic record
invalid request | documented error | unchanged

The table states lab expectations and is not a real response or executable request.

Observe status, body and state

Inspect the documented status, relevant headers and response fields, then use a safe read-back to check the resulting state. A success message can coexist with a failed write, and an error response can coexist with an unintended partial write. The second table separates the error representation from the state question. Avoid recording authentication tokens or real personal data in evidence. An assertion should explain which contractual promise failed and include a safe synthetic request identifier.

text Synthetic API contract
Observation | Question
error response | is its shape documented?
state after error | was anything changed?

The table states lab expectations and is not a real response or executable request.

Compare success and failure paths

Run the valid and invalid cases against fresh isolated data so one does not inherit the other’s state. Confirm the permitted request creates the intended record and the rejected request leaves no unintended record or queued side effect under the contract. If a request times out, distinguish an unknown outcome from a confirmed server failure. Preserve the first failing response and state observation; a later successful retry does not explain what happened to the first request.

Record the scope of evidence

After a repair, rerun both cases with the same contract version and inspect the selected response and state evidence. Record any dependency that was simulated and which external behavior was not exercised. A small pair of cases is useful but not exhaustive: other field boundaries, roles and concurrent requests may need separate investigation. Passing results support only the identified artifact, environment and examples. Review the assertions when the contract or persistence behavior changes.

Common errors and fixes

Asserting only a success status

Why it happens
The promised write may not have persisted.
How to fix it
Read back the relevant synthetic resource.
Prevent it next time
Include state evidence in the acceptance case.

Assuming an error response means no side effect

Why it happens
A partial or background write may still occur.
How to fix it
Inspect the protected state after rejection.
Prevent it next time
Add negative side-effect assertions.

Copying the actual result as the expected value

Why it happens
The implementation becomes its own oracle.
How to fix it
Derive expectations from the agreed rule.
Prevent it next time
Review expected results before execution.

2 practice exercises

1. The API returns an error but creates a synthetic record. What failed?

Show worked solution

The documented no-effect expectation failed even though the response was an error. Preserve the response and state evidence.

2. A success response arrives but read-back finds no record. What can be concluded?

Show worked solution

The promised persistence was not observed for that case. Investigate the write and observation path without assuming the cause.

Key takeaways

  • Check the response and promised state together.
  • Derive expectations independently and state the tested scope.

Official references