简体   繁体   English

未使用HTTPretty请求如何声明URL

[英]How to assert a URL was not requested with HTTPretty

在Python单元测试中使用HTTPretty拦截HTTP请求时,如何断言从未请求目标URL?

By default, if a request doesn't match any registered URI it goes through. 默认情况下,如果请求与任何已注册的URI不匹配,它将通过。

One solution would be to add a generic matcher using Matching regular expressions and making it failing. 一种解决方案是使用Matching正则表达式添加一个通用匹配器并使它失败。

>>> 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. 另一个想法是使用HTTPretty.record ,然后分析生成的文件。 But it feels a bit more complicated. 但是感觉有点复杂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM