Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
beliaev-maksim committed Dec 2, 2021
1 parent 5799ff6 commit c6bde2e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* Deprecate ``match_querystring`` argument in ``Response`` and ``CallbackResponse``.
Use `responses.matchers.query_param_matcher` or `responses.matchers.query_string_matcher`
* Added support for non-UTF-8 bytes in `responses.matchers.multipart_matcher`
* Fixed issue with response match when requests were performed between adding responses with
same URL. See Issue #212

0.16.0
------
Expand Down
6 changes: 6 additions & 0 deletions responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,12 @@ def _find_match(self, request):
found = i
found_match = match
else:
if self._matches[found].call_count > 0:
# that assumes that some responses were added between calls
self._matches.pop(found)
found_match = match
break

# Multiple matches found. Remove & return the first match.
return self._matches.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 @@ -1404,15 +1404,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 @@ -1850,3 +1883,21 @@ def run():

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 c6bde2e

Please sign in to comment.