Overview
http-client uses a real mock, not a fake.
The package-level deterministic testing seam is MockHttpDriver, plus the builder
shortcut withMock() and the helper factory MockHttpResponseFactory.
Use this seam when you want to test:
- request matching and expectations
- response status and body handling
- retries and sequential responses
- streaming and SSE payloads without network calls
MockHttpDriver
MockHttpDriver is expectation-driven.
That means it is the right tool when the test needs to say:
- which request should be matched
- how many times it should match
- which response should be returned
- fluent expectations through
expect()andon() - request matching by method, URL, path, headers, body, and stream mode
- sequential replies with
times(...) - request inspection through
getReceivedRequests()andgetLastRequest()
withMock()
HttpClientBuilder::withMock() is the easiest way to build a deterministic client.
Minimal example:
MockHttpResponseFactory
MockHttpResponseFactory helps when a test needs to construct:
- JSON responses
- error responses
- streaming chunk responses
- SSE responses
replyJson(...) or
replyText(...).
Which One To Use
Use this rule of thumb:withMock()for most client-builder testsMockHttpDriverdirectly when you need to inspect or reuse the driver objectMockHttpResponseFactorywhen you need richer reply shapes
packages/http-client/MOCK_HTTP_CLIENT.md.