Skip to content

Commit

Permalink
Fix registries from being re-used instead of the default registry
Browse files Browse the repository at this point in the history
* fix registry #563
* add async test
  • Loading branch information
beliaev-maksim committed Jun 27, 2022
1 parent b129335 commit 72c67ba
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
------

* Add `passthrough` argument to `BaseResponse` object. See #557
* Fix `registries` leak. See #563

0.21.0
------
Expand Down
9 changes: 6 additions & 3 deletions responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,6 @@ def get_wrapped(
Wrapped function
"""
if registry is not None:
responses._set_registry(registry)

assert_mock = std_mock.patch.object(
target=responses,
attribute="assert_all_requests_are_fired",
Expand All @@ -195,6 +192,9 @@ def get_wrapped(
@wraps(func)
async def wrapper(*args: Any, **kwargs: Any) -> Any: # type: ignore[misc]

if registry is not None:
responses._set_registry(registry)

with assert_mock, responses:
return await func(*args, **kwargs)

Expand All @@ -203,6 +203,9 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any: # type: ignore[misc]
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any: # type: ignore[misc]

if registry is not None:
responses._set_registry(registry)

with assert_mock, responses:
# set 'assert_all_requests_are_fired' temporarily for a single run.
# Mock automatically unsets to avoid leakage to another decorated
Expand Down
38 changes: 38 additions & 0 deletions responses/tests/test_registries.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,44 @@ def run():
assert_reset()


def test_set_registry_reversed():
"""See https://github.com/getsentry/responses/issues/563"""

class CustomRegistry(registries.FirstMatchRegistry):
pass

@responses.activate
def run():
# test that registry does not leak to another test
assert type(responses.mock.get_registry()) == registries.FirstMatchRegistry

@responses.activate(registry=CustomRegistry)
def run_with_registry():
assert type(responses.mock.get_registry()) == CustomRegistry

run()
run_with_registry()
assert_reset()


async def test_registry_async():
class CustomRegistry(registries.FirstMatchRegistry):
pass

@responses.activate
async def run():
# test that registry does not leak to another test
assert type(responses.mock.get_registry()) == registries.FirstMatchRegistry

@responses.activate(registry=CustomRegistry)
async def run_with_registry():
assert type(responses.mock.get_registry()) == CustomRegistry

await run()
await run_with_registry()
assert_reset()


def test_set_registry_context_manager():
def run():
class CustomRegistry(registries.FirstMatchRegistry):
Expand Down

0 comments on commit 72c67ba

Please sign in to comment.