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:
mock_response_delay.for_requestsforresponsescallbacks.mock_response_delay.for_httpxforrespxside effects andhttpxtransport handlers.mock_response_delay.for_httpx2forhttpx2transport handlers.
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
responsescallback answer as a slow server would.Give the result to
responses.RequestsMock.add_callbackin place ofcallback.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, andcallbackis not called. Any other request gets the response fromcallbackafter waiting for the delay.- Parameters:
callback¶ – The
responsescallback 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
responsescallback.
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
httpxhandler answer as a slow server would.Give the result to
httpx.MockTransportin place ofhandler, or torespxas 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, andhandleris not called. Any other request gets the response fromhandlerafter waiting for the delay.- Parameters:
- Returns:
An
httpxhandler.
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
httpx2handler answer as a slow server would.Give the result to
httpx2.MockTransportin place ofhandler.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, andhandleris not called. Any other request gets the response fromhandlerafter waiting for the delay.- Parameters:
- Returns:
An
httpx2handler.