Knowledge
What a mock API is: a stateful fake API so the front end and tests need not wait for the back end
- A mock API is a stand-in API that returns sample data under the contract both sides agreed on, so the front end and the tests can run before the back end is done.
- A good mock is stateful (after a POST, a GET sees the new record) and can reproduce errors, empty data and latency, not only "pretty data".
- The examples use Localman: projects, resources, the five CRUD endpoints, conditional error rules and chaos.
What a mock API is and what it solves
A mock API is an HTTP service that plays the real back end: it accepts requests on the paths, methods and JSON formats both sides agreed on, and returns sample responses. The app calls it like the real API; once the back end is ready, only the base URL changes.
A mock solves three common problems:
- The front end does not wait for the back end. Both teams agree on the contract (resource names, fields, status codes) first, then work in parallel. How to set that contract is covered in Designing a REST API: naming resources, choosing HTTP methods and picking the right status codes.
- Reproducing hard-to-trigger cases. An empty list, a record without an image, an unusually long name, a wrong password at sign-in, a slow server or a 500: things a real back end rarely produces exactly when you need to test them.
- Learning HTTP. Beginners get a place to send requests and read responses (method, headers, body, status code) without setting up a server.
Many UI bugs are really contract bugs: the client sends the wrong field name, the API returns an array while the client expects an object, a failed sign-in has no proper status code, empty data breaks the layout. Mocking early surfaces them before integration.
Static mocks and stateful mocks
- Static mock: each endpoint returns a fixed piece of JSON. Enough to draw the UI, but POST a record and GET again and it is not there.
- Stateful mock: the mock keeps data like a small database. POST creates a record, PUT updates it, DELETE removes it, and GET reflects those changes. You need this for create, edit and delete flows and for end-to-end tests.
Hands-on with Localman
Localman is a stateful REST API mocking tool that runs on the web. The steps below follow its "Quick start" documentation.
1. Create a project
On the Projects page, create a project and, if you like, set an API Prefix such as /api/v1. Each project has a secret that sits right in the mock URL:
https://<secret>.localman.hoandat.com/api/v1/...
https://localman.hoandat.com/m/<secret>/api/v1/...
Use the second form when your network or tool cannot resolve the project's own subdomain. The secret works like a password: anyone with the URL can read and write the mock data, so never put real personal data in it.
2. Add a resource and generate data
A resource is a set of records with a plural name such as products; the name becomes the first segment of the route. The schema lists each record's fields, each one a basic type or a Faker method that generates names, emails or dates. Then pick how many records to generate (up to 100 at a time).
3. The five CRUD endpoints
Every resource comes with:
| Method | Path | Result |
|---|---|---|
GET |
/products |
200, array of records |
GET |
/products/12 |
200, one record |
POST |
/products |
201, the new record |
PUT / PATCH |
/products/12 |
200, the record after merging fields |
DELETE |
/products/12 |
200, the deleted record |
The body of POST, PUT and PATCH is a JSON object sent with Content-Type: application/json. A quick try with curl:
curl 'https://<secret>.localman.hoandat.com/api/v1/products?page=1&limit=10'
curl -X POST https://<secret>.localman.hoandat.com/api/v1/products \
-H 'Content-Type: application/json' \
-d '{"name":"Keyboard","price":450000,"inStock":false}'
List routes accept search, filtering by field (?inStock=false), sortBy with order=desc, page and limit (at most 100 records per page). What these parameters mean and the design choices behind them are in Pagination, filtering and sorting in a REST API: offset, cursor and the conventions to agree on. Nest reviews under products and you also get /products/3/reviews.
4. Do not test only pretty data
For a product list screen, cover: many products, exactly one, an empty list (delete every record or filter to nothing), a product without an image, a very long name, an out-of-stock product. Check that the empty state is clear, broken images have a fallback, and "load more" stops at the right time.
5. Rules: conditional errors
Each endpoint can have rules: when a request matches a condition, it returns a different status and body. A condition matches on a header, a query parameter, a JSONPath into the body, or the request order (the Nth request). Rules run top to bottom and the first match wins.
For example, with a sessions resource used as a sign-in endpoint, POST /sessions could have:
- body
$.emailis an empty string → 400, email missing; - body
$.passwordequalswrong-password→ 401; - the third request onwards (
nthgreater than 2) → 429, to test a "try again later" message.
The front end can then exercise both the redirect-on-success branch and the keep-the-form-and-show-an-error branch. How to shape error bodies so clients act on a code rather than the wording is discussed in Consistent errors, idempotency and REST API versioning: conventions that help an API last.
6. Chaos: slow and flaky
Chaos makes an endpoint slow (a random delay between a minimum and a maximum, up to 30 seconds) or flaky by ratio: an error with a status of your choice, a 429, or hanging until the timeout and then a 504. Use it to test spinners, retry logic and network error messages.
When the real back end is ready
- Write down the contract the mock serves (resources, fields, error codes) so both sides can compare.
- Switch the base URL from the mock to the real back end.
- Rerun every scenario you tried against the mock, error branches included; any difference is a place where the contract does not match.
- Rotate the secret or delete the mock project if its URL was shared widely.