Closed Seluj78 closed 1 year ago
@Seluj78 If I understand it correctly, the root cause is that Moto has no way to retrieve the existing responses, so it will always override them with it's own.
This has been raised in Moto as well, with a possible workaround: https://github.com/getmoto/moto/issues/6417
Hmm, I didn't think moto
would be the core of the problem, but I had it included since it was showing up a lot in the registered responses.
My main problem here is the fact that my self.r_mock
responses are ignored and only the ones in responses
are accepted. I will try responses._real_send = rsps.unbound_on_send()
in my codebase and see if it changes anything.
Although in the example, it uses rsps
which is from the with
statement, which I do not have in my code. I will try with self.r_mock
~but if it doesn't work, I suppose I need to use the rsps
from moto
?~ it seems like it worked
Ok, it seemed to have worked immediately. Further investigation needed on more tests 👀
Yeah it's half working. With a passthrough, I'm getting a recursion error.
Inside the setUp
self.r_mock = responses.RequestsMock(assert_all_requests_are_fired=True)
self.r_mock.start()
self.r_mock.add_passthru(re.compile(rf"{MEILISEARCH_URL}.*"))
responses._real_send = self.r_mock.unbound_on_send()
exception log:
hub_backend/routes/api/admin/deployment.py:292: in deploy_user
index_cohort(cohort)
hub_backend/utils/meilisearch.py:57: in index_cohort
cohort_index.add_documents(
.venv/lib/python3.11/site-packages/meilisearch/index.py:381: in add_documents
add_document_task = self.http.post(url, documents)
.venv/lib/python3.11/site-packages/meilisearch/_httprequests.py:73: in post
return self.send_request(requests.post, path, body, content_type)
.venv/lib/python3.11/site-packages/meilisearch/_httprequests.py:51: in send_request
request = http_method(
.venv/lib/python3.11/site-packages/requests/api.py:115: in post
return request("post", url, data=data, json=json, **kwargs)
.venv/lib/python3.11/site-packages/requests/api.py:59: in request
return session.request(method=method, url=url, **kwargs)
.venv/lib/python3.11/site-packages/requests/sessions.py:587: in request
resp = self.send(prep, **send_kwargs)
.venv/lib/python3.11/site-packages/requests/sessions.py:701: in send
r = adapter.send(request, **kwargs)
.venv/lib/python3.11/site-packages/responses/__init__.py:1127: in send
return self._on_request(adapter, request, **kwargs)
.venv/lib/python3.11/site-packages/responses/__init__.py:1033: in _on_request
return _real_send(adapter, request, **kwargs)
.venv/lib/python3.11/site-packages/responses/__init__.py:1127: in send
return self._on_request(adapter, request, **kwargs)
.venv/lib/python3.11/site-packages/responses/__init__.py:1033: in _on_request
return _real_send(adapter, request, **kwargs)
.venv/lib/python3.11/site-packages/responses/__init__.py:1127: in send
return self._on_request(adapter, request, **kwargs)
E RecursionError: maximum recursion depth exceeded
!!! Recursion detected (same locals & position)
Every single test in my test suite that calls a route that uses requests
fails with a RecursionError
.
@Seluj78 it looks like original issue is solved
But now you hit another one, would you mind open another bug and fill the details that will help us to debug
Makes sense. Should I close this one and open another one in getsentry/responses ? Or do you think this has more to do with moto
still ?
If you can provide a reproducible without moto, then we can have a look in responses
Then please close current issue
Closing this, new issue is https://github.com/getsentry/responses/issues/670
Describe the bug
I am using
unittest
along withresponses
to test my Flask API. I use responses to mock the requests made to other servers, for example to send an email. I am also usingmoto
for other mocking reasons.My tests are configured with a
_TestCase
which, stripped down to just what matters to responses, looks like this:When running tests classes (located in different files, that inherit from
_TestCase
), I get a weird behavior where the responses are registered in theself.r_mock
registery but ignored. My API only uses the ones I register withresponses.add
and notresponses.add
(which also bypasses theassert_all_requests_are_fired
)I'm at a loss here 🙏
Additional context
An example test class would look something like
When running in debug mode, here are the
registered()
ofself.r_mock
andresponses
at different point in my testAt the start of the test method, before anything is executed in the test
After registering some
self.r_mock.add
callsMoving through the test until before I call the test client to make a request to test my code, it's exactly the same thing. Now, With my debugger, I enter the code being executed when the API is called by my test and I reach the first call that I want mocked.
self.r_mock
doesn't exist in this context because we're in a completely different code, but I can importresponses
and I get this (cut for clarity):Using the PyCharm debugger, I can move back through the thread back to my unit test where my test code is awaiting a response and I get this:
And, if I step through, the
requests.post
are executed and not mocked because they aren't inresponses.registered
but inself.r_mock.registered()
.Version of
responses
0.23.3
Steps to Reproduce
See above :)
Expected Result
Actual Result