Skip to content

Commit

Permalink
Allow configuration of passthrough during BaseResponse construction (#…
Browse files Browse the repository at this point in the history
…558)

Allow configuration of passthrough during BaseResponse construction

Resolves #557

Co-authored-by: Maksim Beliaev <beliaev.m.s@gmail.com>
  • Loading branch information
jayaddison and beliaev-maksim committed Jun 13, 2022
1 parent 3d83a88 commit b129335
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
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,
) -> 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

0 comments on commit b129335

Please sign in to comment.