You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there currently anyway to simulate latency on a mocked http response with requests-mock? I am trying to implement some unit tests for request timeouts but having some difficulty in mocking/simulating request latency.
Would be happy to work on this and open a PR if it was something you were open to having added, I was thinking it would be cool to have an additional kwarg for register_uri.
Example:
withself.assertRaises(requests.exceptions.Timeout), requests_mock.Mocker() asm:
# Simulate a 500ms delay on HTTP response.m.register_uri("GET", "https://httpbin.org/get", text="foo", latency=500)
requests.get("https://httpbin.org/get")
...
The text was updated successfully, but these errors were encountered:
Was just looking through old tickets and I still think this is a bad idea to do in unit testing, so probably not something i want to add to the library. However if you (or future people reading this bug) are absolutely convinced you want to do this it's pretty easy to do manually.
import time
import requests
import requests_mock
def delayed_response(timeout, **kwargs):
time.sleep(timeout)
return kwargs
with requests_mock.mock() as m:
m.get("https://httpbin.org/get", **delayed_response(5, text="foo"))
requests.get("https://httpbin.org/get")
Hi @jamielennox,
Is there currently anyway to simulate latency on a mocked http response with requests-mock? I am trying to implement some unit tests for request timeouts but having some difficulty in mocking/simulating request latency.
Would be happy to work on this and open a PR if it was something you were open to having added, I was thinking it would be cool to have an additional kwarg for
register_uri
.Example:
The text was updated successfully, but these errors were encountered: