From 05a83d240ab5b01ecf762564c6db964cb3bd2cf0 Mon Sep 17 00:00:00 2001 From: Maksim Beliaev Date: Wed, 16 Mar 2022 23:05:55 +0100 Subject: [PATCH 1/2] * Fixed the issue when `reset()` method was called in not stopped mock. See #511 --- CHANGES | 4 ++++ responses/__init__.py | 7 ++++++- responses/tests/test_responses.py | 12 ++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index e901fd24..46475c00 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +0.20.0 +------ +* Fixed the issue when `reset()` method was called in not stopped mock. See #511 + 0.19.0 ------ diff --git a/responses/__init__.py b/responses/__init__.py index 2f512b2f..7f58a803 100644 --- a/responses/__init__.py +++ b/responses/__init__.py @@ -560,7 +560,6 @@ def reset(self): self._registry = FirstMatchRegistry() self._calls.reset() self.passthru_prefixes = () - self._patcher = None def add( self, @@ -834,6 +833,8 @@ def _on_request(self, adapter, request, **kwargs): def start(self): if self._patcher: # we must not override value of the _patcher if already applied + # this prevents issues when one decorated function is called from + # another decorated function return def unbound_on_send(adapter, request, *a, **kwargs): @@ -847,6 +848,10 @@ def stop(self, allow_assert=True): # prevent stopping unstarted patchers self._patcher.stop() + # once patcher is stopped, clean it. This is required to create a new + # fresh patcher on self.start() + self._patcher = None + if not self.assert_all_requests_are_fired: return diff --git a/responses/tests/test_responses.py b/responses/tests/test_responses.py index 80456d28..5898ee64 100644 --- a/responses/tests/test_responses.py +++ b/responses/tests/test_responses.py @@ -2114,3 +2114,15 @@ def run(): run() assert_reset() + + +def test_reset_in_the_middle(): + @responses.activate + def run(): + with responses.RequestsMock() as rsps2: + rsps2.reset() + responses.add(responses.GET, "https://example.invalid", status=200) + print(requests.request("GET", "https://example.invalid")) + + run() + assert_reset() From 42415ef44303a7b29981d37f9ca1f78c9a3e3d53 Mon Sep 17 00:00:00 2001 From: Maksim Beliaev Date: Thu, 17 Mar 2022 17:20:27 +0100 Subject: [PATCH 2/2] test --- CHANGES | 1 - responses/tests/test_responses.py | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 7fd926dc..cd8cc233 100644 --- a/CHANGES +++ b/CHANGES @@ -28,7 +28,6 @@ * An error is now raised when both `content_type` and `headers[content-type]` are provided as parameters. * When a request isn't matched the passthru prefixes are now included in error messages. - 0.18.0 ------ diff --git a/responses/tests/test_responses.py b/responses/tests/test_responses.py index 30d7354b..05e816e2 100644 --- a/responses/tests/test_responses.py +++ b/responses/tests/test_responses.py @@ -2135,7 +2135,8 @@ def run(): with responses.RequestsMock() as rsps2: rsps2.reset() responses.add(responses.GET, "https://example.invalid", status=200) - print(requests.request("GET", "https://example.invalid")) + resp = requests.request("GET", "https://example.invalid") + assert resp.status_code == 200 run() assert_reset()