Skip to content

Commit

Permalink
Fixed the issue when reset() method was called in not stopped mock (#…
Browse files Browse the repository at this point in the history
…516)

* Fixed the issue when `reset()` method was called in not stopped mock. See #511
  • Loading branch information
beliaev-maksim committed Mar 17, 2022
1 parent 1724937 commit d0c9b7c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
since they are not actual properties of the class instance.
Use `responses.mock.assert_all_requests_are_fired`,
`responses.mock.passthru_prefixes`, `responses.mock.target` instead.
* Fixed the issue when `reset()` method was called in not stopped mock. See #511

0.19.0
------
Expand Down
7 changes: 6 additions & 1 deletion responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,6 @@ def reset(self):
self._registry = FirstMatchRegistry()
self._calls.reset()
self.passthru_prefixes = ()
self._patcher = None

def add(
self,
Expand Down Expand Up @@ -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):
Expand All @@ -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

Expand Down
13 changes: 13 additions & 0 deletions responses/tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2127,3 +2127,16 @@ 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)
resp = requests.request("GET", "https://example.invalid")
assert resp.status_code == 200

run()
assert_reset()

0 comments on commit d0c9b7c

Please sign in to comment.