Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration of passthrough during BaseResponse construction #558

Merged
merged 9 commits into from
Jun 13, 2022
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.22.0
------

* Add `passthrough` argument to `BaseResponse` object. See #557

0.21.0
------

Expand Down
12 changes: 8 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ def __init__(
url: _URLPatternType,
match_querystring: Union[bool, object] = None,
match: "_MatcherIterable" = (),
*,
passthrough: bool = False,
jayaddison marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
self.method: str = method
# ensure the url has a default path set if the url is a string
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
25 changes: 25 additions & 0 deletions responses/tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})

Expand Down