diff --git a/CHANGES b/CHANGES index 60f6a8e9..200fc1be 100644 --- a/CHANGES +++ b/CHANGES @@ -12,6 +12,8 @@ * Added `responses.registries`. Now user can create custom registries to manipulate the order of responses in the match algorithm `responses.activate(registry=CustomRegistry)` +* Fixed issue with response match when requests were performed between adding responses with + same URL. See Issue #212 0.16.0 ------ diff --git a/responses/registries.py b/responses/registries.py index 65467985..1e0f8c53 100644 --- a/responses/registries.py +++ b/responses/registries.py @@ -20,6 +20,11 @@ def find(self, request): found = i found_match = response else: + if self.registered[found].call_count > 0: + # that assumes that some responses were added between calls + self.registered.pop(found) + found_match = response + break # Multiple matches found. Remove & return the first response. return self.registered.pop(found), match_failed_reasons else: diff --git a/responses/test_responses.py b/responses/test_responses.py index e076ec7f..92dfb432 100644 --- a/responses/test_responses.py +++ b/responses/test_responses.py @@ -1405,15 +1405,48 @@ def test_multiple_responses(): def run(): responses.add(responses.GET, "http://example.com", body="test") responses.add(responses.GET, "http://example.com", body="rest") + responses.add(responses.GET, "http://example.com", body="fest") + responses.add(responses.GET, "http://example.com", body="best") resp = requests.get("http://example.com") assert_response(resp, "test") + resp = requests.get("http://example.com") assert_response(resp, "rest") + + resp = requests.get("http://example.com") + assert_response(resp, "fest") + + resp = requests.get("http://example.com") + assert_response(resp, "best") + # After all responses are used, last response should be repeated resp = requests.get("http://example.com") + assert_response(resp, "best") + + run() + assert_reset() + + +def test_multiple_responses_intermixed(): + @responses.activate + def run(): + responses.add(responses.GET, "http://example.com", body="test") + resp = requests.get("http://example.com") + assert_response(resp, "test") + + responses.add(responses.GET, "http://example.com", body="rest") + resp = requests.get("http://example.com") assert_response(resp, "rest") + responses.add(responses.GET, "http://example.com", body="best") + resp = requests.get("http://example.com") + assert_response(resp, "best") + + # After all responses are used, last response should be repeated + resp = requests.get("http://example.com") + assert_response(resp, "best") + run() assert_reset() @@ -1916,3 +1949,21 @@ class CustomRegistry(registries.FirstMatchRegistry): run() assert_reset() + + +def test_requests_between_add(): + @responses.activate + def run(): + responses.add(responses.GET, "https://example.com/", json={"response": "old"}) + assert requests.get("https://example.com/").content == b'{"response": "old"}' + assert requests.get("https://example.com/").content == b'{"response": "old"}' + assert requests.get("https://example.com/").content == b'{"response": "old"}' + + responses.add(responses.GET, "https://example.com/", json={"response": "new"}) + + assert requests.get("https://example.com/").content == b'{"response": "new"}' + assert requests.get("https://example.com/").content == b'{"response": "new"}' + assert requests.get("https://example.com/").content == b'{"response": "new"}' + + run() + assert_reset()