一个Python库,提供了一个可用于测试需要与HTTP服务器交互的代码的HTTP服务器模拟。
项目描述
httpservermock
httpservermock
提供了一个HTTP服务器模拟,可用于测试需要与HTTP服务器交互的代码。
pip install httpservermock
# or
poetry add --dev httpservermock
示例用法
from urllib.error import HTTPError
from urllib.request import urlopen
import pytest
from httpservermock import MethodName, MockHTTPResponse, ServedBaseHTTPServerMock
def test_example() -> None:
with ServedBaseHTTPServerMock() as httpmock:
httpmock.responses[MethodName.GET].append(
MockHTTPResponse(404, "Not Found", b"gone away", {})
)
httpmock.responses[MethodName.GET].append(
MockHTTPResponse(200, "OK", b"here it is", {})
)
# send a request to get the first response
with pytest.raises(HTTPError) as raised:
urlopen(f"{httpmock.url}/bad/path")
assert raised.value.code == 404
# get and validate request that the mock received
req = httpmock.requests[MethodName.GET].pop(0)
assert req.path == "/bad/path"
# send a request to get the second response
resp = urlopen(f"{httpmock.url}/")
assert resp.status == 200
assert resp.read() == b"here it is"
httpmock.responses[MethodName.GET].append(
MockHTTPResponse(404, "Not Found", b"gone away", {})
)
httpmock.responses[MethodName.GET].append(
MockHTTPResponse(200, "OK", b"here it is", {})
)
项目详情
关闭
httpservermock-0.1.0.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 50040da959d83a31a50dcfdbf336ec9d8a810287c7f1336c5be30e901967ed30 |
|
MD5 | 04396a220a821eb5703d0411e2f674ca |
|
BLAKE2b-256 | 3dbfae99defe32a5afd45d1616f2dc8623e464e1ded5963bb1882bcd8c649292 |