Package: responsaas
ResponsaasWraps the pythonresponseslibrary As
A Service.See the full documentationhere(or more specificallyconverting from responses).QuickstartAutomatic (with pytest)Usingpytest-mock-resources, we
can use Docker to manage the lifecycle of the server.pip install responsaas[pmr]fromresponsaas.pytestimportcreate_responsaas_fixture,create_responsaas_server_fixtureresponsaas_server=create_responsaas_server_fixture()responsaas=create_responsaas_fixture()deftest_foo(responsaas:Responsaas):responsaas.add("/foo",json={"bar":True})response=requests.get(responsaas.base_url+"/foo")assertresponse.json()=={"bar":True}ManualThe manual examples assume you have some external way of standing up the serverpip install responsaasimportrequestsfromresponsaasimportResponsaasServer,Responsaas# With pytestfromresponsaas.pytestimportcreate_responsaas_fixtureresponsaas=create_responsaas_fixture("http://localhost:7564")deftest_foo(responsaas:Responsaas):responsaas.add("/foo",json={"bar":True})response=requests.get(responsaas.base_url+"/foo")assertresponse.json()=={"bar":True}# Or completely manually.deftest_foo():responsaas_server=ResponsaasServer("http://localhost:7564")withresponsaas_server.activate()asresponsaas:responsaas.add("/foo",json={"bar":True})response=requests.get(responsaas.base_url+"/foo")assertresponse.json()=={"bar":True}Why?!?Under the hood,repsonsesispatching 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 makingactualnetwork calls.Howeverthe same (patch) strategy that makes it useful has some issues.This can run afoul of other libraries which performpatchoperations. 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 throughpatchissues, or through programmer error,responsescan besonon-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 theresponsaasserverEach new namespace corresponds to oneresponses.RequestsMock.As incoming requests are received by the server, they're mapped to the request
shape expected byresponses, and routed directly through its request
matching and responds logic.