What is stubbing HTTP calls in tests?

Answer

When your code makes HTTP calls to external APIs, you need to stub those calls in tests to avoid: network dependency, rate limits, costs, unreliable external services, and test data pollution. Approaches: WireMock: runs a real HTTP server locally that you configure to return specific responses: stubFor(get(urlEqualTo("/api/users/1")).willReturn(aResponse().withBody(json).withStatus(200)));. Verify calls were made. MSW (Mock Service Worker — JavaScript): intercepts fetch/XHR at the network level using Service Workers: rest.get('/api/users', (req, res, ctx) => res(ctx.json({ name: 'Alice' }))). Works in both browser and Node.js tests. nock (Node.js): intercepts HTTP requests in Node: nock('https://api.example.com').get('/users/1').reply(200, { name: 'Alice' });. pytest-responses / responses (Python): similar for Python requests library. Test both success and error responses (404, 500, timeouts) to ensure resilience code is exercised.