允许您在测试中轻松模拟http响应
responses_prox的Python项目详细描述
responses_proxy允许您在测试中轻松模拟http响应
安装
使用pip:
$ pip install responses_proxy
使用Docker:
$ docker run --rm -v tests/responses:/tests/responses bearstech/responses_proxy -h
用法
检查命令行参数:
$ responses-proxy -h
首先使用代理模式保存一些内容:
$ responses-proxy --proxy
如果目标站点使用SSL,则使用:
$ responses-proxy --proxy --use-ssl
代理不支持ssl,因此需要发出http请求。但第一盘 http\u代理服务器env var:
$ export HTTP_PROXY=http://localhost:3333
然后运行一些代码来发出一些请求:
python -c "import requests; requests.get('http://bearstech.com')"
这将在tests/responses/
中生成一些文件注意:使用Docker,您必须装入卷:
$ docker run --rm -v tests/responses:/tests/responses bearstech/responses_proxy
现在您可以在不使用代理模式的情况下重新启动服务器,客户端将作出反应 同样不需要调用真正的服务器。
您也可以在单元测试中使用requestsmock
import responses_proxy import requsests def test_url(): with responses_proxy.RequestsMock(): requests.get('http://bearstech.com') # https will work to. both are registered requests.get('https://bearstech.com')