Skip to content

Record the app's traffic once and replay it with no backend (#150) - #178

Merged
bitomule merged 3 commits into
mainfrom
worktree-t-mav-150-network-mocking
Dec 4, 2026
Merged

bitomule merged 3 commits into
mainfrom
worktree-t-mav-150-network-mocking

Conversation

@bitomule

@bitomule bitomule commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Closes #150.

Adds network.mock: the app's traffic is recorded once against the real backend into a HAR file, and replayed afterwards with no backend at all. The app does not change.

- network.mock: { record: mocks/search.har }   # real backend
- network.mock: { replay: mocks/search.har }   # no backend; unrecorded → 404
- network.mock: { replay: mocks/search.har, unmatched: 503, strict: true, matchBody: true, ignoreParams: [ts, nonce] }
- network.mock.stop: {}                        # optional; the run end stops it

CLI: mav network mock --record FILE | --replay FILE [--unmatched N] [--strict] [--match-body] [--ignore-params a,b], ended by mav network stop, read by mav network status.

How

  • Traffic: mitmproxy local mode with the pattern <simulator>/data/Containers/Bundle/Application/, so only the run's simulator's apps are intercepted (verified: the same app on a second simulator is not). mav boots the simulator and installs mitmproxy's CA with simctl keychain add-root-cert once mitmdump is up. Simulator only: device and macOS get network_mock_unsupported.
  • Hermetic replay: --server-replay with server_replay_extra=<unmatched>, server_replay_reuse=true, connection_strategy=lazy, upstream_cert=false, http3=false. A replay that opened any server connection fails the run with network_mock_not_hermetic.
  • Embedded addon (network_mock_addon.py, go:embed): one JSON line per request (recorded / replayed / unmatched, reached_server) plus every server connection. Two things mitmproxy does not do on its own:
    • serverplayback marks its server_replay_extra answer as a replay too, so the addon tells unmatched requests apart by looking them up in the recording;
    • with server_replay_reuse serverplayback always serves the first recorded response; the addon makes equal requests get them in recorded order, then the last one again.
    • It also writes a ready file once the proxy listens: mitmdump's "Local redirector started." line is buffered when stdout is a file and reaches the disk only at exit, so it cannot be waited on. The An exec step cannot start a helper process that outlives the step #149 registry gets a ReadyFile check for this.
  • Lifecycle: in mav run the mock is the network-mock helper of the An exec step cannot start a helper process that outlives the step #149 registry, stopped on every way out. From the CLI it is detached and recorded in processes.jsonl, like network.start.
  • Recording replaces the named file only when the run passed and something was recorded; otherwise it stays in the run directory and the summary/report say where.
  • Results: mock_replayed / mock_unmatched (mock_recorded / mock_recording when recording) on the run's result line, network_mock in report.json (with a warning for unrecorded requests), network-mock.json in the run. strict: true fails with network_mock_unmatched naming the requests.
  • Lint: exactly one of record/replay, replay file exists, unmatched is 100–599, strict/matchBody/ignoreParams/unmatched only with replay, device/macOS refused, network.mock with network.start refused (network_mock_conflicts_capture, also at run time).
  • Two mocks at once on one Mac (each on its own simulator) coexist: measured with two local-mode mitmdumps, each captured only its own simulator. So no machine lock. One start right after another instance stopped failed once with "failed to establish connection to macOS system extension"; that maps to network_mock_redirector_unavailable, whose hint says to approve the extension or retry.

Docs: README (flow steps, "Network mocks" section with what it does not cover, the secrets warning and the Maestro comparison), skills/mav/SKILL.md, CHANGELOG.

Tests

  • drivers/network/mock_test.go: arguments for record and replay, each option (unmatched, matchBody, ignoreParams, lazy, upstream_cert), and refused specs.
  • network_mock_test.go: summary counters, strict, not hermetic, recording moved only when the run passed, params, CLI flags, flow parsing (paths relative to the flow), lint, the run's result line and the run end failing a strict replay, report section.
  • network_mock_integration_test.go (skipped without mitmdump): real mitmdump in regular mode with the embedded addon. Records against httptest, shuts it down and puts a tripwire on its address, replays: recorded order then last, ignoreParams, 404 for an unrecorded request, reached_server = 0, no connection to the tripwire, and strict failing. One request goes through a CONNECT tunnel, because then the proxy knows the server before any request, as in local mode.

Removing each fix makes its test fail (then restored, green again):

=== replay with connection_strategy=eager instead of lazy
    network_mock_integration_test.go:117: replay failed: network_mock_not_hermetic map[... reached_server:1 server_connects:1 ...]
    network_mock_integration_test.go:142: the replay connected to the backend's address 2 times
--- FAIL: TestNetworkMockRecordsThenReplaysWithTheBackendDown
=== strict ignored
--- FAIL: TestNetworkMockRecordsThenReplaysWithTheBackendDown (strict)
--- FAIL: TestStopNetworkMockStrictReplayFailsOnUnmatched: err = <nil>, want network_mock_unmatched
--- FAIL: TestRunFlowFailsAStrictReplayWithUnmatchedRequestsAtRunEnd: run did not fail on the mock
=== run end does not finish the mock
--- FAIL: TestRunFlowFailsAStrictReplayWithUnmatchedRequestsAtRunEnd
--- FAIL: TestRunFlowPrintsTheMockCountersOnItsResultLine: result line = ok cmd=run ... (no mock_ counters)
=== addon always serves the first recorded response
--- FAIL: TestNetworkMockRecordsThenReplaysWithTheBackendDown
=== recording moved even when the run failed
--- FAIL: TestStopNetworkMockRecordingReplacesTheFileOnlyWhenTheRunPassed

make check passes on the final commit.

End to end on a simulator

A throwaway simulator created for this, a minimal UIKit app (not in the PR) that GETs the URLs in its environment and shows the answers, a local Python backend, and mav run. Paths, IP and UDID replaced.

== 1. RECORD (backend up)
ok cmd=run dir=/Users/ana/probe/.mav/runs/384d3d07 elapsed=15.226422291s mock_mode=record mock_recorded=3 mock_recording=/Users/ana/probe/mocks/probe.har name=record run=384d3d07 steps=4 target_kind=simulator target_source=config udid=SIM-A-UDID
backend hits during record: 2
recording: -rw-r--r--@ 1 ana  staff  11087 Dec  3 14:26 mocks/probe.har
== 2. REPLAY (backend down)
ok cmd=run dir=/Users/ana/probe/.mav/runs/90268b77 elapsed=7.511198167s mock_mode=replay mock_replayed=3 mock_unmatched=1 name=replay run=90268b77 steps=4 target_kind=simulator target_source=config udid=SIM-A-UDID
-- network-mock.json:
{
  "mode": "replay",
  "replay": "/Users/ana/probe/mocks/probe.har",
  "requests": 4,
  "recorded": 0,
  "replayed": 3,
  "unmatched": 1,
  "reached_server": 0,
  "server_connects": 0,
  "unmatched_status": 404,
  "unmatched_requests": [
    "GET http://192.0.2.10:18150/not-recorded"
  ],
  "har": "/Users/ana/probe/.mav/runs/90268b77/network.har",
  "events": "/Users/ana/probe/.mav/runs/90268b77/network-mock.jsonl"
}-- app on A:
PROBE /hello → 200 {"n": 1, "path": "/hello", "items": ["apple", "pear"], "source": "real-backend"}
PROBE /items → 200 {"n": 2, "path": "/items", "items": ["apple", "pear"], "source": "real-backend"}
PROBE / → 200 <!doctype html><html lang=en><head><meta charset=utf-8><link rel=icon href=data:
PROBE /items → 200 {"n": 2, "path": "/items", "items": ["apple", "pear"], "source": "real-backend"}
PROBE /hello → 200 {"n": 1, "path": "/hello", "items": ["apple", "pear"], "source": "real-backend"}
PROBE /not-recorded → 404
PROBE / → 200 <!doctype html><html lang=en><head><meta charset=utf-8><link rel=icon href=data:
== 3. REPLAY strict (backend down)
fail code=network_mock_unmatched action=network.mock code=network_mock_unmatched mock_mode=replay mock_replayed=3 mock_unmatched=1 next="record the flow again so the recording has these requests, add the changing query parameters to ignoreParams, or drop strict" requests="GET http://192.0.2.10:18150/not-recorded" run=bc81f22b screenshot=/Users/ana/probe/.mav/runs/bc81f22b/failure.png
== 4. OTHER SIMULATOR during a replay on A (backend down)
ok cmd=network.mock events=/Users/ana/probe/.mav/runs/bc81f22b/network-mock.jsonl har=/Users/ana/probe/.mav/runs/bc81f22b/network.har mode=replay next="mav network stop" pid=79993 replay=mocks/probe.har run=bc81f22b udid=SIM-A-UDID unmatched=404
-- app on B (not intercepted: no recorded data, connection refused):
PROBE /items → -1 ERR Could not connect to the server.
ok cmd=network.status active=true har=/Users/ana/probe/.mav/runs/bc81f22b/network.har mock_active=true mock_mode=replay mock_replayed=0 mock_requests=0 mock_unmatched=0 pid=79993 run=bc81f22b target_kind=simulator target_source=config udid=SIM-A-UDID
ok cmd=network.stop mock_mode=replay mock_replayed=0 mock_unmatched=0 run=bc81f22b summary=/Users/ana/probe/.mav/runs/bc81f22b/network-mock.json target_kind=simulator target_source=config udid=SIM-A-UDID

The replay run's screenshot shows the recorded answers ("source": "real-backend", with the backend down) and /not-recorded → 404; the second simulator, not intercepted, gets "Could not connect to the server".

@bitomule
bitomule force-pushed the worktree-t-mav-150-network-mocking branch from be76d1e to 52bcc5c Compare December 3, 2026 13:59
@bitomule
bitomule merged commit 152825a into main Dec 4, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Network mocking for simulator targets: record once, replay with no backend

1 participant