diff --git a/CHANGES b/CHANGES index fbf8ddb0..bcd26b59 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,8 @@ +0.22.0 +------ + +* Add `passthrough` argument to `BaseResponse` object. See #557 + 0.21.0 ------ diff --git a/README.rst b/README.rst index cd06dba3..357bd76e 100644 --- a/README.rst +++ b/README.rst @@ -1172,14 +1172,18 @@ need to allow an entire domain or path subtree to send requests: responses.add_passthru(re.compile("https://percy.io/\\w+")) -Lastly, you can use the ``response.passthrough`` attribute on ``BaseResponse`` or -use ``PassthroughResponse`` to enable a response to behave as a pass through. +Lastly, you can use the ``passthrough`` argument of the ``Response`` object +to force a response to behave as a pass through. .. code-block:: python # Enable passthrough for a single response - response = Response(responses.GET, "http://example.com", body="not used") - response.passthrough = True + response = Response( + responses.GET, + "http://example.com", + body="not used", + passthrough=True, + ) responses.add(response) # Use PassthroughResponse diff --git a/responses/__init__.py b/responses/__init__.py index 9ab25709..30b9597c 100644 --- a/responses/__init__.py +++ b/responses/__init__.py @@ -354,6 +354,8 @@ def __init__( url: _URLPatternType, match_querystring: Union[bool, object] = None, match: "_MatcherIterable" = (), + *, + passthrough: bool = False, ) -> None: self.method: str = method # ensure the url has a default path set if the url is a string @@ -366,6 +368,7 @@ def __init__( self.match: "_MatcherIterable" = match self.call_count: int = 0 + self.passthrough = passthrough def __eq__(self, other: Any) -> bool: if not isinstance(other, BaseResponse): @@ -596,7 +599,8 @@ def get_response(self, request: "PreparedRequest") -> HTTPResponse: class PassthroughResponse(BaseResponse): - passthrough: bool = True + def __init__(self, *args: Any, **kwargs: Any): + super().__init__(*args, passthrough=True, **kwargs) class OriginalResponseShim(object): diff --git a/responses/tests/test_responses.py b/responses/tests/test_responses.py index 4dd93f02..083fcedd 100644 --- a/responses/tests/test_responses.py +++ b/responses/tests/test_responses.py @@ -1589,6 +1589,31 @@ def run_mocked(): assert_reset() +def test_passthrough_kwarg(httpserver): + httpserver.serve_content("OK", headers={"Content-Type": "text/plain"}) + + def configure_response(passthrough): + responses.get(httpserver.url, body="MOCK", passthrough=passthrough) + + @responses.activate + def run_passthrough(): + configure_response(passthrough=True) + resp = requests.get(httpserver.url) + assert_response(resp, "OK") + + @responses.activate + def run_mocked(): + configure_response(passthrough=False) + resp = requests.get(httpserver.url) + assert_response(resp, "MOCK") + + run_mocked() + assert_reset() + + run_passthrough() + assert_reset() + + def test_passthrough_response(httpserver): httpserver.serve_content("OK", headers={"Content-Type": "text/plain"})