APACHE-2.0 License
Wraps the python responses library As A Service.
See the full documentation here (or more specifically converting from responses).
Using pytest-mock-resources, we can use Docker to manage the lifecycle of the server.
pip install responsaas[pmr]
from responsaas.pytest import create_responsaas_fixture, create_responsaas_server_fixture
responsaas_server = create_responsaas_server_fixture()
responsaas = create_responsaas_fixture()
def test_foo(responsaas: Responsaas):
responsaas.add("/foo", json={"bar": True})
response = requests.get(responsaas.base_url + "/foo")
assert response.json() == {"bar": True}
The manual examples assume you have some external way of standing up the server
pip install responsaas
import requests
from responsaas import ResponsaasServer, Responsaas
# With pytest
from responsaas.pytest import create_responsaas_fixture
responsaas = create_responsaas_fixture("http://localhost:7564")
def test_foo(responsaas: Responsaas):
responsaas.add("/foo", json={"bar": True})
response = requests.get(responsaas.base_url + "/foo")
assert response.json() == {"bar": True}
# Or completely manually.
def test_foo():
responsaas_server = ResponsaasServer("http://localhost:7564")
with responsaas_server.activate() as responsaas:
responsaas.add("/foo", json={"bar": True})
response = requests.get(responsaas.base_url + "/foo")
assert response.json() == {"bar": True}
Under the hood, repsonses
is patch
ing the network calls being made and
replacing their result with the result you specify. It's very fast, convenient,
and (by default) disallows you from making actual network calls.
However the same (patch
) strategy that makes it useful has some issues.
This can run afoul of other libraries which perform patch
operations. The
issue history of responses has many instances (frequently with moto
), where
patches get clobbered in one way or another.
responsaas
does not use patch
at all. It is a real standalone serviceEither through patch
issues, or through programmer error, responses
can be
so non-invasive that API calls accidentally get made through to the
original destination URL.
responsaas
forces you to change (or really, make configurable) the URLresponses
allows you to return arbitrary python objects (like exceptions)
which wouldn't be possible for a request to actually return.
responsaas
(once again), is a literal service responding to requests. Theresponses
is(?) limited to mocking the requests
library. Which doesn't
cover cases like httpx
, aiohttp
, etc.
responsaas
is client agnostic, given that it's a real service.responses
needs an additional mechanism to allow "passthru" requests
responsaas
(once again), is a literal service responding to requests, soWhat's going on internally is:
responsaas
serverresponses.RequestsMock
.responses
, and routed directly through its request