A python library that provides a http server mock that can be used for testing code that should interact with an http server
Project description
httpservermock
httpservermock
provides a HTTP server mock that can be used to test code that needs to interact with an HTTP server.
pip install httpservermock
# or
poetry add --dev httpservermock
Example usage:
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", {})
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
httpservermock-0.1.0.tar.gz
(7.7 kB
view hashes)
Built Distribution
Close
Hashes for httpservermock-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c48d512a2e048345e0acc98760d8ab9c5de66390cc2f65ab516586529445582 |
|
MD5 | e940441666970f0dbcb316894045b051 |
|
BLAKE2b-256 | 7ab58044cd1616bb8149762109982b7a3671f0eba298e8e0128dfd130364cb69 |