How to assert a URL was not requested with HTTPretty

842 Views Asked by At

How does one go about asserting that a target URL was never requested when using HTTPretty to intercept HTTP requests in Python unit tests?

1

There are 1 best solutions below

0
On

By default, if a request doesn't match any registered URI it goes through.

One solution would be to add a generic matcher using Matching regular expressions and making it failing.

>>> httpretty.register_uri(httpretty.GET,
...                        re.compile(".*"),
...                        status=500,
...                        body="Fail")
...
>>> requests.get("http://www.google.ch/")
<Response [200]>
>>> httpretty.activate()
>>> requests.get("http://www.google.ch/")
<Response [500]>

Of course, your application must fail in such cases. Another idea might be to use record.

Another idea would be to use HTTPretty.record and then analyze the produced file. But it feels a bit more complicated.