Mock server from traffic: a copy of your real API in a minute
A mock written from memory resembles the API but doesn’t match it. It has no null where the server sends one, no field added last week, and the images live at the wrong addresses. The app passes on such a mock and fails on the real server. Microkoi Bench builds a mock server from recorded traffic: every endpoint responds with what the server actually returned.
Why a mock from memory drifts from the API
A response typed by hand reflects what the developer remembers about the API. The real response is longer: optional fields, empty arrays, null instead of a string, dates in another format. That’s exactly where parsing breaks.
The test environment doesn’t help. Its data changes every day: listings get removed, test accounts get wiped. The screen that showed an error yesterday doesn’t show it today. You need a copy of the API that responds the same way every time.
Record the flow once
Start the Bench proxy and go through the flow you need in the app. For example, in a real estate app: sign-in with an SMS code, the listing feed, a listing with photos. The “Endpoints” tree in the “Traffic” section sorts the requests by host and path segment.
Build a server from a group
- In the “Endpoints” tree, hover over a node or a group, for example
api.example.com/.v1 - Click “Create mock server from group”. The “Mock servers” section opens with the new server.
- Check the bar above the server: it lists the addresses split into variants, and any warnings.
Need requests from several hosts? Click the checkmark button in the tree header, select hosts, groups or individual methods, and click “Mock server”.
POST /v1/auth step=phone 200
POST /v1/auth step=code 200
GET /v1/listings 200
GET /v1/listings/42 200
GET /v1/photos/42-1.jpg 200 fileWhat Bench takes from the recording
- Each method and path pair becomes an endpoint with the last recorded response: status code, headers, body.
- Photos and other binary responses carry over as files, exactly as they arrived.
- Endpoints are sorted into folders by path segment. The name is the last segment, and for an ID, the last two together: listings/42.
- Technical headers for length, compression, connection and Date aren’t carried over.
- Endpoints are created enabled, the server is created stopped.
One address, different responses
Signing in with an SMS code takes two POST /v1/ requests: first with step=phone, then with step=code. One address, different responses. A mock with a single response would break the second step.
Bench looks at up to 100 recent requests to an address and looks for a parameter that explains the difference in responses. Here it’s step. You get two endpoints with conditions, and the bar above the server shows “auth: 2 variants by step”.
Timestamps, tokens, signatures and request IDs are never used to split variants. The SMS code itself is only in the second request, so any code works on replay.
If the app sends a value that wasn’t in the recording, the server responds 404 with the variant_not_found code and a list of variants. Need a response to that request too? Add an endpoint with the same path and no conditions.
Links point to the mock server
API responses often contain the server’s full address: the next-page link, photo URLs, the Location header. Bench replaces the original host’s address with a {{baseUrl}} placeholder, and the running server puts in the address it was called at.
| Where the request comes from | What gets put in |
|---|---|
| This computer | http://localhost:9091 |
| Android emulator | http://10.0.2.2:9091 |
| A phone over HTTPS | https://192.168.1.15:9091 |
Addresses of other hosts, such as a content delivery network, stay as they were. If photos come from there, the app loads them from the real host.
Connect the app
Turn on the “Running” switch. The server listens on the first free port starting at 9091 and accepts both HTTP and HTTPS on it. Use its address as the API base URL in the debug build.
curl http://127.0.0.1:9091/v1/listingsThe phone and the emulator need “Network access” in the server’s address block. For HTTPS, the device has to trust the Bench root certificate, the same one the proxy uses.
Before you hand it to the team
A recording is real responses, so it may contain tokens and personal data. Replace them in the endpoint bodies before you put the server in git. While the server runs with network access, any device on that network sees its responses.
If a response was saved incompletely or wasn’t there at all, the bar above the server starts with “Check the endpoint responses:” and names the address. Fill in that endpoint by hand.
What you get
A copy of the API that responds the same way today, next week and on any computer. The server is a folder of files in the workspace: a teammate gets it with git, and a change to a response shows up in the diff.
The app asked for something that wasn’t in the recording? The “Request log” tab shows the request marked “no endpoint”, and the “Create endpoint” button next to it opens a draft with the same method and path.
Build a server from your own traffic in the beta and write to beta@microkoi.com about responses that didn’t carry over the way you expected. We read every email.