No project description provided
Project description
Responsaas
Wraps the python responses library As A Service.
See the full documentation here (or more specifically converting from responses).
Quickstart
Automatic (with pytest)
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}
Manual
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}
Why?!?
Under the hood, repsonses is patching 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
patchoperations. The issue history of responses has many instances (frequently withmoto), where patches get clobbered in one way or another.responsaasdoes not usepatchat all. It is a real standalone service responding to real requests.
-
Either through
patchissues, or through programmer error,responsescan be so non-invasive that API calls accidentally get made through to the original destination URL.responsaasforces you to change (or really, make configurable) the URL you're hitting for tests, which should make it impossible to hit the original destination url in tests on accident.
-
responsesallows 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. The requesting client code is receiving bytes over the wire, and parsing it normally.
-
responsesis(?) limited to mocking therequestslibrary. Which doesn't cover cases likehttpx,aiohttp, etc.responsaasis client agnostic, given that it's a real service.
-
responsesneeds an additional mechanism to allow "passthru" requestsresponsaas(once again), is a literal service responding to requests, so it can only return.
How?
What's going on internally is:
- Each test registers a new "namespace" against the
responsaasserver - Each new namespace corresponds to one
responses.RequestsMock. - As incoming requests are received by the server, they're mapped to the request
shape expected by
responses, and routed directly through its request matching and responds logic.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file responsaas-0.1.0.tar.gz.
File metadata
- Download URL: responsaas-0.1.0.tar.gz
- Upload date:
- Size: 14.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83f8697bf489d4784eb0e3a9d89d127caa88f5178a79ed560204754da7c944f2
|
|
| MD5 |
a81e381a4506a227501ffca29fdef835
|
|
| BLAKE2b-256 |
770db030cc78ea91fa26e14dcd25456eae613be362ed417d7830c61d9c7b4af6
|
File details
Details for the file responsaas-0.1.0-py3-none-any.whl.
File metadata
- Download URL: responsaas-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
232ba7b09665d78a27af231811806656151c514c700189f34aed905456aa32a6
|
|
| MD5 |
c01125dc83525b467a98d8a3b9e2bab3
|
|
| BLAKE2b-256 |
505aef7c2208a89a44ece6bdbe168bd91febf021693822a918581404281a008a
|