Skip to content

Commit

Permalink
fix #479
Browse files Browse the repository at this point in the history
copy a response object in registry if one is already registered
  • Loading branch information
beliaev-maksim committed Jan 15, 2022
1 parent eb62b46 commit 3f6594b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions responses/registries.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
from typing import (
TYPE_CHECKING,
List,
Expand Down Expand Up @@ -47,6 +48,11 @@ def find(
return found_match, match_failed_reasons

def add(self, response: "BaseResponse") -> None:
if response in self.registered:
# if user adds multiple responses that reference the same instance
# see https://github.com/getsentry/responses/issues/479
response = copy.deepcopy(response)

self.registered.append(response)

def remove(self, response: "BaseResponse") -> None:
Expand Down
25 changes: 25 additions & 0 deletions responses/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1925,3 +1925,28 @@ def run():

run()
assert_reset()


def test_responses_reuse():
@responses.activate
def run():
url = "https://someapi.com/"
fail_response = responses.Response(
method="GET", url=url, body="fail", status=500
)
responses.add(responses.GET, url, "success", status=200)
responses.add(fail_response)
responses.add(fail_response)
responses.add(fail_response)
responses.add(responses.GET, url, "success", status=200)
responses.add(responses.GET, url, "", status=302)

response = requests.get(url)
assert response.content == b"success"

for _ in range(3):
response = requests.get(url)
assert response.content == b"fail"

run()
assert_reset()

0 comments on commit 3f6594b

Please sign in to comment.