QA Interview Hub · Knowledge base · Quizzes · Українська

Networking › Networking in QA work

Intercepting and mocking requests, proxy tools

In a browser the response is replaced by page.route, while for a mobile or desktop client a proxy sits between it and the server. A route handler calls fulfill, continue or abort: abort models a network failure, and fulfill with 503 is a server response, so both are tested. HTTPS through a proxy needs a trusted root certificate, and certificate pinning blocks it. Mocks do not replace a few end-to-end tests.

Networking · Testing

You need to check how a mobile app behaves when the backend returns 500, and you cannot change the backend. What do you do?

A real backend rarely breaks on demand. To check a 500, a slow response or a lost connection, the response is replaced between the client and the server. For a browser the test itself does it, for a mobile app or a desktop client a proxy is needed. This lesson covers both approaches, the difference between "cut" and "answer with an error", and why proxies and HTTPS do not get along without a certificate.

How it works and why. page.route(url, handler) (or context.route for all pages of a context) puts a handler on requests that match a pattern. The handler decides one of three things: route.fulfill() returns its own response, route.continue() passes the request on (changing headers or body if needed), route.abort(code) cuts it with a network error. route.fetch() performs the real request so you can change the response and return it via fulfill. Abort codes include connectionrefused, connectionreset, timedout, namenotresolved, internetdisconnected, failed and others.

// a network failure: fetch ends with a TypeError, Network shows failed
await page.route('**/api/orders', (route) => route.abort('connectionrefused'));

// a server answer with an error: status 503 and a body
await page.route('**/api/orders', (route) =>
  route.fulfill({ status: 503, json: { error: 'unavailable' } }),
);

// a real request, but a changed response
await page.route('**/api/profile', async (route) => {
  const response = await route.fetch();
  const body = await response.json();
  body.plan = 'expired';
  await route.fulfill({ response, json: body });
});

// slowing one request
await page.route('**/api/report', async (route) => {
  await new Promise((r) => setTimeout(r, 5000));
  await route.continue();
});

abort and fulfill are different cases. abort models a network failure: in the browser fetch ends with a TypeError, Network shows failed, and there is no response at all. fulfill({ status: 503 }) is a server answer with an error code: the client got a status and a body. An app usually handles them in different branches, so test both.

Where to intercept

Interception in a test

Playwright page.route: only the test’s browser, code next to the assertion, easy to repeat in CI.

A proxy tool

Charles, Fiddler, Proxyman, mitmproxy: any client including mobile, a trusted certificate is needed.

Figure 1. The two approaches complement each other: the test locks in a scenario, a proxy lets you explore any client.

Proxy tools. They sit between the client and the server and work with any client: a mobile app, a desktop app, an SDK. Charles, Fiddler and Proxyman have a graphical interface, traffic viewing and editing, replacing a response with a local file or another host. mitmproxy works from a terminal or web interface and is driven by Python scripts, so it automates well.

# addon.py: reply from the proxy for one endpoint: the request never reaches the backend
from mitmproxy import http


def request(flow: http.HTTPFlow) -> None:
    if flow.request.pretty_url.endswith("/api/orders"):
        flow.response = http.Response.make(
            503,
            b'{"error": "unavailable"}',
            {"Content-Type": "application/json"},
        )

# run: mitmdump -s addon.py
# on the device: proxy host:8080, the certificate from http://mitm.it

Why HTTPS needs a certificate. To read or change encrypted traffic, the proxy acts as a man in the middle: it signs certificates for hosts with its own root certificate. The client must trust that root or it shows an error. So the root is added to the device’s trusted ones. On Android, apps targeting API 24+ (Android 7.0) do not trust user roots unless their network security config allows them, and on iOS the root is also switched on in the certificate trust settings. Certificate pinning blocks such traffic completely.

What must line up for a proxy to see HTTPS

1. The proxy signs a certificate with its own rootthe client sees not the server’s certificate2. The root is trusted on the deviceotherwise a certificate error3. The app trusts user rootsAndroid 7+: only via network security config4. No certificate pinningpinning blocks a proxy completely
Figure 2. If any step is broken, the app refuses to work through the proxy.

Typical scenarios. Replace a backend response without changing code; reproduce a 500 or a slow response on mobile; see what exactly an SDK sends; check that the app does not send extra data. For an app with pinning, use a test build without pinning rather than bypassing protection in the production build.

Limits and alternatives. Replacing always hides the real integration: a mock returns what you invented, not what the backend would. So mocks supplement a few end-to-end tests without mocks, not replace them. If you need to replay many services’ responses, contract tests and a dedicated mock server beat manual replacement through a proxy. A proxy adds its own latency and sometimes changes behaviour (the HTTP version, for example), so performance measurements through it are not representative.

Typical mistakes.

  • Testing only fulfill with 500 and not the network error (abort).
  • Replacing all integration tests with mocks and missing that the API changed its schema.
  • Registering page.route after goto, when the request has already left.
  • Leaving the proxy root trusted on a test device after work.
  • Not knowing that pinning blocks a proxy and hunting for the error in the proxy settings.

Check yourself

How do route.abort('connectionrefused') and route.fulfill({ status: 503 }) differ for the UI?

Answer

The first simulates a network failure: no response, fetch ends with a TypeError. The second is a server response with status 503. An app usually handles these differently, so test both.

A mobile app does not work through the proxy although the certificate is installed on the phone. Name two causes.

Answer

An Android app with targetSdkVersion 24+ does not trust user roots without permission in the network security config, or it has certificate pinning, which blocks a proxy. On iOS the root also has to be switched on in the trust settings.

Why do mocks not replace all integration tests?

Answer

A mock returns what you put in and will not show that the real backend changed schema or behaviour. A few end-to-end tests without replacement are needed.

In an interview. Junior: In Playwright page.route lets me replace a response (fulfill), pass it (continue) or cut it (abort). For mobile apps there are proxies such as Charles, Fiddler or mitmproxy. Middle/Senior: I separate a network failure from an error response and test both. With route.fetch I change only the needed field of a real response. HTTPS through a proxy needs a trusted root; on Android 7+ that depends on the network security config, and pinning blocks interception, so tests need a separate build. I supplement mocks with a few tests without replacement.

More articles in this chapter