HTTPretty
HTTPretty copied to clipboard
Emulate server down then coming online
Is there a way to emulate the server being down and then coming up in the middle of a test? The code that I am testing does something like
def get_data():
result = requests.post(...)
while is_error(result):
sleep(5)
result = requests.post(...)
return result.json()
I would like to write a test that does something like
httpretty.register(httpretty.POST, 'http://example.com/request', body='{"foo": "bar"},
ready_after_milliseconds=500)
result = get_data()
Ideally, I could check that httpretty rejected one request and then satisfied a second.
I can't register the url after the failed call, because control won't return to the test driver.
It appears that one way to emulate this is
raise MaxRetryError("Mock server not ready yet", url=uri)
inside the callback function to generate the body. You can also increment a variable there to know how many tries have been attempted.