Blog

How to test timeouts and server errors in your app

On staging, the server answers in 200 ms and always correctly. On a mobile network, the same request takes twenty seconds or ends with a 503. What the user sees at that moment is usually learned from reviews. Mock responses in Microkoi Bench show it in advance, with no changes to the server or the app.

Why staging won’t show this

  • Staging is barely loaded and doesn’t slow down when a test needs it to.
  • Asking the backend team to return a 503 from one endpoint for one test run takes a day of messages.
  • Throttling the network on the phone slows down every request at once, not the one you need.

You need a specific request to get the response you defined: slow, with an error, with the right header. In Bench, that’s a mock response, or mock. The proxy that carries the phone’s traffic returns it, and the request never reaches the server.

A mock from a real response

  1. Route the phone’s traffic to Bench and keep the api.example.com host in the “Recording” window: mocks answer only for hosts that are recorded.
  2. Open the task list in the app. The GET /v1/tasks exchange appears in “Traffic”.
  3. In the exchange panel, click “Create mock”. The method, host and path come from the request; the status code, headers and body come from the server’s response.
  4. The mock is created turned off. Check the response and turn the mock on with the toggle in the editor header.

Now Bench answers for the task list. In “Traffic”, such an exchange has a blue mock cube in the first column, so you always see where the response came from.

A slow response and a timeout

Find the request timeout in the app’s code, for example 15 seconds. Set the mock’s “Delay” field above it, to 20000 ms, and open the list.

A common finding: the spinner turns for fifteen seconds, then “No tasks yet” appears. The app takes a timeout for an empty response. For a user on the subway, it looks as if their tasks are gone.

A delay below the timeout, for example 8000 ms, tests something else: whether the spinner stays visible the whole time and what happens on pull-to-refresh. Two GET /v1/tasks rows in “Traffic” half a second apart mean the app sent a second request without waiting for the first.

Error codes

At some point, the server returns each of these responses to every user. Write down what should happen in the app for each one:

ResponseWhat to set in the mockWhat to expect from the app
500A JSON body with an error fieldA message and “Retry”, cached data stays in place
503Retry-After: 30A retry no earlier than 30 seconds later
401No bodyA token refresh and a retry without signing out
429Retry-After: 5A pause before the retry

To avoid editing one mock back and forth, group the responses into cases: “Server 500”, “Maintenance 503”, “Token expired”. A case overrides the status code, headers and body of the base mock. Switch the case, and the proxy answers the new way immediately, without a restart. The delay stays as set in the base mock.

The response in the “Maintenance 503” case
HTTP/1.1 503 Service Unavailable
Retry-After: 30
Content-Type: application/json

{"error": "maintenance"}

Retries are visible in the traffic

Each attempt by the app is a separate row in “Traffic” with the mock cube, and the “Started” column shows when it went out. You don’t need logs or a debugger for this.

The app’s attempts on a 503 response
Method  Path        Status  Source  Started
GET     /v1/tasks   503     Mock    09:41:07.120
GET     /v1/tasks   503     Mock    09:41:07.310
GET     /v1/tasks   503     Mock    09:41:07.495
GET     /v1/tasks   503     Mock    09:41:07.702

Four requests in under a second: the app ignores Retry-After and retries right away. For one phone, that’s a trifle. For a server under maintenance that all users hit again at once, it’s extra load at the worst possible moment.

An error first, then success

A mock answers every request the same way. To check that the app gets back to the list after a failure, you need a breakpoint.

  1. Turn the mock off: a breakpoint stops the server’s response, but not the mock’s.
  2. In the GET /v1/tasks exchange panel, click “Breakpoint”. It applies to both the request and the response.
  3. Open the list in the app. Let the request through with “Continue unchanged”, and on the response click “Cancel request”: the app gets a 502.
  4. Tap “Retry” in the app and let the request and response through with “Continue unchanged”.

The error message should disappear and the list should come back. While the exchange is paused at the breakpoint, the app waits for the response, but no longer than 10 minutes: after that, the exchange continues on its own.

A check before every release

Mocks and cases are files in the workspace folder and go to git with it. Before a release, the check takes ten minutes: turn on the mock, go through the cases one by one, compare with the table. The same cases also run without the window, in continuous integration.

When the check is over, “Disable all mocks inside” on the api.example.com host sends the app back to the real server.

Run these responses on your app with the beta and write to beta@microkoi.com: what you found and which server response was missing. We read every email.

Try it on your own project

The beta is free and needs no sign-up. Download it, pick your project folder and start the proxy — your first requests will show up within minutes.

Version 0.9.6 · macOS, Windows and Linux · no sign-up