Skip to content

Commit

Permalink
Fix responses added after requests are made not being used (#455)
Browse files Browse the repository at this point in the history
Fixes #212
  • Loading branch information
beliaev-maksim committed Jan 7, 2022
1 parent 0d340eb commit b8a5311
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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
------
Expand Down
5 changes: 5 additions & 0 deletions responses/registries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
51 changes: 51 additions & 0 deletions responses/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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()

0 comments on commit b8a5311

Please sign in to comment.