API Reference

Simulate a slow server, and the client timeout it causes, in HTTP mocks.

Each supported client library has its own module, so that only the library which is used needs to be installed:

requests

Delay responses callbacks, and raise the requests timeout.

mock_response_delay.for_requests.delayed_responses_callback(callback: ~collections.abc.Callable[[~requests.models.PreparedRequest], T], *, delay_seconds: float, sleep_fn: ~collections.abc.Callable[[float], None] = <built-in function sleep>) Callable[[PreparedRequest], T]

Make a responses callback answer as a slow server would.

Give the result to responses.RequestsMock.add_callback in place of callback.

A request whose read timeout is shorter than the delay waits for the timeout and then raises requests.exceptions.Timeout, as it would against a real slow server, and callback is not called. Any other request gets the response from callback after waiting for the delay.

Parameters:
  • callback – The responses callback which makes the response.

  • delay_seconds – How long the simulated server takes to respond.

  • sleep_fn – What to call to wait. It is given the number of seconds to wait for. The default really sleeps. Give something else to record the waits, or to advance a fake clock, instead.

Returns:

A responses callback.

httpx

Delay httpx handlers, and raise the httpx timeout.

mock_response_delay.for_httpx.delayed_httpx_handler(handler: ~collections.abc.Callable[[~httpx.Request], T], *, delay_seconds: float, sleep_fn: ~collections.abc.Callable[[float], None] = <built-in function sleep>) Callable[[Request], T]

Make an httpx handler answer as a slow server would.

Give the result to httpx.MockTransport in place of handler, or to respx as a side effect.

A request whose read timeout is shorter than the delay waits for the timeout and then raises httpx.ReadTimeout, as it would against a real slow server, and handler is not called. Any other request gets the response from handler after waiting for the delay.

Parameters:
  • handler – The handler which makes the response.

  • delay_seconds – How long the simulated server takes to respond.

  • sleep_fn – What to call to wait. It is given the number of seconds to wait for. The default really sleeps. Give something else to record the waits, or to advance a fake clock, instead.

Returns:

An httpx handler.

httpx2

Delay httpx2 handlers, and raise the httpx2 timeout.

mock_response_delay.for_httpx2.delayed_httpx2_handler(handler: ~collections.abc.Callable[[~httpx2.Request], T], *, delay_seconds: float, sleep_fn: ~collections.abc.Callable[[float], None] = <built-in function sleep>) Callable[[Request], T]

Make an httpx2 handler answer as a slow server would.

Give the result to httpx2.MockTransport in place of handler.

A request whose read timeout is shorter than the delay waits for the timeout and then raises httpx2.ReadTimeout, as it would against a real slow server, and handler is not called. Any other request gets the response from handler after waiting for the delay.

Parameters:
  • handler – The handler which makes the response.

  • delay_seconds – How long the simulated server takes to respond.

  • sleep_fn – What to call to wait. It is given the number of seconds to wait for. The default really sleeps. Give something else to record the waits, or to advance a fake clock, instead.

Returns:

An httpx2 handler.