Blog

Mock server or proxy mock: which one to use for testing

A tester needs one error while the rest of the app keeps working with the test environment. A developer needs an endpoint the backend doesn’t have. Both get a mock response, but in different ways: a proxy mock or a mock server. The wrong choice means rework: the build gets reconfigured for another address, and CI tests hit the fact that a mock server doesn’t run without a window.

Two ways to respond in place of the server

A proxy mock. The app calls its real API, for example api.example.com, and Bench responds in place of the server to the requests that have a mock. All other requests go to the server as usual.

A mock server. A separate test API on your computer with its own port. The app calls it directly, at its address, and no proxy is needed.

What mattersProxy mockMock server
Where it respondsIn the proxy, in place of the real serverOn its own port, as a standalone API
API address in the appStays the sameChanges to the mock server’s address
What the app needsGo through the proxyCall the mock server’s address
No response definedThe request goes to the real server404 with an explanation
Response setsCases, switched in one clickEndpoints enabled and disabled, including by folder
Headless, in CIThe bench-mocks programNo: runs while Bench is open

Choosing in three questions

  • The real API doesn’t exist or is unavailable, and you can change the address in the build: a mock server.
  • The real API works, and only a few endpoints need to respond differently: a proxy mock.
  • Automated tests on a build machine need the responses: a proxy mock and the bench-mocks program.

No backend yet

A screen waiting for an endpoint can’t be tested. A mock server gives you that endpoint right away. You can build it from scratch, from a template of a typical app or from recorded traffic, with the “Create mock server from group” button in the “Endpoints” tree.

  • The server starts on the first free port starting at 9091 and accepts both HTTP and HTTPS on it.
  • Next-page links and image URLs are built with {{baseUrl}} and point to the mock server itself, wherever it’s called from.
  • “Network access” opens the server to a phone on the same network. The Android emulator reaches the computer at 10.0.2.2.
  • The “Export API to note” button turns the server into a description for the backend: endpoints, status codes, headers, sample responses.

Reproduce an error

A 500 from the real server arrives when nobody expects it and doesn’t arrive when you need to test it. A proxy mock is the better fit here: the app stays on the test environment, and only one response changes.

  1. In the “Traffic” section, select an exchange and click “Create mock”. The mock gets the method, host, path and the server’s response.
  2. Change the status code to 500, 503 or 429 and save.
  3. Enable the mock with its switch: a mock made from an exchange starts disabled so it doesn’t respond before you’ve checked it.

In the traffic, a mock’s response has a blue cube in the first column, and the tooltip names the mock. Several failures at once go into a case, a named set of responses on top of the mocks. Switching cases takes effect with the next request, without restarting the proxy.

On a mock server, an error is a separate endpoint with the same method and path. Disable the main one and enable the error endpoint.

Slow network

Both a mock and a mock server endpoint have a “Delay” field, up to 60,000 ms. That’s enough to see the loading indicator, test the client timeout and a second tap on the button while the first request is still in flight.

“Delay” holds back the whole response, and does so on every request. 3G speed, 500 and 503 errors, dropped connections and truncated bodies come from chaos. In the proxy, it applies to mock responses too: Bench treats a mock like a server. A mock server has its own chaos profile and its own “Chaos” switch, so one server runs steadily while another next to it fails.

Neither a mock nor a mock server has a “first call fails, second succeeds” queue. Chaos breaks responses with a set probability, but the run is repeatable: the third request to a rule gets the same disruption again. How to set up the rules is covered on the Bench chaos page.

When there’s no matching response

Here the two behave in opposite ways. A proxy mock lets a request with no matching mock through to the server: the app doesn’t notice a difference. A mock server responds 404 and names the reason: endpoint_not_found if there’s no endpoint, and variant_not_found with a list of variants if no condition matched.

On the mock server’s “Request log” tab, such a request is marked “no endpoint”. The “Create endpoint” button next to it opens a draft with the same method and path.

Automated tests and CI

A build machine has no window. For it, the console program bench-mocks ships alongside the app: the same proxy with mocks and cases, headless. It writes nothing to the workspace, so one workspace from git serves both the developer and the build.

Run with a case
bench-mocks cases list --workspace ./api-space
bench-mocks serve --workspace ./api-space --case "Empty order list"

Once the proxy is ready, the program prints one line of JSON: the proxy address, the control API address and the session token. The test points the app at the proxy and switches cases through the API, effective with the next request:

Control API
curl -s -X PUT -H "Authorization: Bearer $TOKEN" \
  -d '{"name":"Delivery slot taken"}' http://$CONTROL/v1/active-case
curl -s -H "Authorization: Bearer $TOKEN" http://$CONTROL/v1/report
curl -s -X POST -H "Authorization: Bearer $TOKEN" http://$CONTROL/v1/stop
  • The Android emulator uses 10.0.2.2, the iOS Simulator uses 127.0.0.1. You don’t need to open the proxy to the network for them.
  • The report lists the mock responses that fired and the request counts in a stable order, so you can compare reports from two runs.
  • A typo in a case name doesn’t turn into a mysterious test: the program exits with code 5 and names the closest case.
  • The token is a session secret. Don’t print the whole ready line to the build log.

bench-mocks works only with proxy mocks and cases. Mock servers, breakpoints and monitoring aren’t available to it: a mock server responds while Bench and its workspace are open. For automated tests on a build machine, choose proxy mocks.

What goes into git

Mocks, cases and mock servers are files in the mocks, cases and mock-servers folders of the workspace, with response bodies next to them. They show up in code review, and a teammate gets the same responses with git pull. Which case is active and which servers are running is stored only on your computer.

Picked one approach and had to switch to the other? Write to beta@microkoi.com about the task where it happened. That’s how we’ll see where the line between them isn’t obvious.

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