请求的模拟库。
httmock的Python项目详细描述
根据Apache许可证2.0版(以下简称“许可证”);
除非符合许可证,否则您不得使用此文件。
您可以在http://www.apache.org/licenses/license-2.0
获取许可证副本,除非适用法律要求或书面同意,否则根据许可证按“原样”发放,
无任何保证或条件,无论是明示的还是暗示的。
请参阅许可证以了解控制许可的特定语言以及许可证下的限制。
描述:httmock
==
python 2.7和3.4+的“请求”模拟库。
pip install httmock
,或者,如果您是gentoo用户:
emerge dev python/httmock
usage
----
您可以使用它来模拟第三方api并测试内部使用“请求”的库,有条件地将模拟回复与“urlmatch”装饰符一起使用:
``python
from httmock import urlmatch,httmock
import requests
@urlmatch(netloc=r'(.*)?google\.com$')
定义google_mock(url,请求):
返回“感觉幸运,朋克?”
使用httmock(google_mock):
r=requests.get('http://google.com/')
print r.content“感觉很幸运,朋克?”
```
`all_requests`装饰符不按条件阻止实际请求。如果返回字典,它将映射到返回的“requests.response”对象:
`` python
从httmock导入所有请求,httmock
导入请求
@所有请求
定义响应内容(url,请求):
返回{状态代码:200,
'content':'oh hai'}
with httmock(response廑content):
r=requests.get('https://foo廑bar')
print r.status廑code
print r.content
````
您也可以直接使用“response”方法,而不是返回dict:
`` python
从httmock导入所有请求、响应、httmock
导入请求
@所有请求
def response内容(url,请求):
头={'content-type':'application/json',
“设置cookie”:“foo=bar;”}
内容={“消息”:“超出API速率限制”}
返回响应(403,内容,头,无,5,请求)
使用httmock(响应内容):
r=requests.get('https://api.github.com/users/whatever')
print r.json().get('message')
print r.cookies['foo']
```
关键字:请求、测试,mock
平台:未知
分类器:编程语言::python::2
分类器:编程语言::python::3
分类器:预期受众::开发人员
分类器:主题::软件开发::测试
分类器:操作系统::操作系统无关
除非符合许可证,否则您不得使用此文件。
您可以在http://www.apache.org/licenses/license-2.0
获取许可证副本,除非适用法律要求或书面同意,否则根据许可证按“原样”发放,
无任何保证或条件,无论是明示的还是暗示的。
请参阅许可证以了解控制许可的特定语言以及许可证下的限制。
描述:httmock
==
python 2.7和3.4+的“请求”模拟库。
pip install httmock
,或者,如果您是gentoo用户:
emerge dev python/httmock
usage
----
您可以使用它来模拟第三方api并测试内部使用“请求”的库,有条件地将模拟回复与“urlmatch”装饰符一起使用:
``python
from httmock import urlmatch,httmock
import requests
@urlmatch(netloc=r'(.*)?google\.com$')
定义google_mock(url,请求):
返回“感觉幸运,朋克?”
使用httmock(google_mock):
r=requests.get('http://google.com/')
print r.content“感觉很幸运,朋克?”
```
`all_requests`装饰符不按条件阻止实际请求。如果返回字典,它将映射到返回的“requests.response”对象:
`` python
从httmock导入所有请求,httmock
导入请求
@所有请求
定义响应内容(url,请求):
返回{状态代码:200,
'content':'oh hai'}
with httmock(response廑content):
r=requests.get('https://foo廑bar')
print r.status廑code
print r.content
````
您也可以直接使用“response”方法,而不是返回dict:
`` python
从httmock导入所有请求、响应、httmock
导入请求
@所有请求
def response内容(url,请求):
头={'content-type':'application/json',
“设置cookie”:“foo=bar;”}
内容={“消息”:“超出API速率限制”}
返回响应(403,内容,头,无,5,请求)
使用httmock(响应内容):
r=requests.get('https://api.github.com/users/whatever')
print r.json().get('message')
print r.cookies['foo']
```
关键字:请求、测试,mock
平台:未知
分类器:编程语言::python::2
分类器:编程语言::python::3
分类器:预期受众::开发人员
分类器:主题::软件开发::测试
分类器:操作系统::操作系统无关