Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion doc/howto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -838,4 +838,13 @@ The ``bake()`` method can be chained to layer additional defaults:
All ``expect_request``, ``expect_oneshot_request``, and
``expect_ordered_request`` methods are available on the baked object. Other
attributes such as ``url_for()`` and ``check_assertions()`` are delegated to
the underlying server transparently.
the underlying server transparently. The ``no_handler_status_code`` setting is
read-only on baked proxies. Configure the underlying server explicitly; all
proxies then read its current value::

httpserver.no_handler_status_code = 404
assert json_post.no_handler_status_code == 404

Assigning or deleting ``no_handler_status_code`` on a baked proxy raises
``AttributeError`` and leaves the server setting unchanged. Clearing the
underlying server resets the value read by every proxy.
11 changes: 10 additions & 1 deletion pytest_httpserver/bake.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class BakedHTTPServer:

Any attribute not explicitly defined here is delegated to the wrapped
:py:class:`HTTPServer`, so ``url_for()``, ``check_assertions()``, etc.
work transparently.
work transparently. ``no_handler_status_code`` is read-only on the proxy;
configure it directly on the wrapped server instead.
"""

def __init__(self, server: HTTPServer, **kwargs: Unpack[RequestMatcherKwargs]) -> None:
Expand Down Expand Up @@ -62,6 +63,14 @@ def __exit__(
def __getattr__(self, name: str) -> Any:
return getattr(self._server, name)

@property
def no_handler_status_code(self) -> int:
"""The wrapped server's current status for unmatched requests.

Configure this setting on the main server, not on a baked proxy.
"""
return self._server.no_handler_status_code

def __repr__(self) -> str:
return f"<{self.__class__.__name__} defaults={self._defaults!r} server={self._server!r}>"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Make ``no_handler_status_code`` read-only on baked proxies, preventing a
proxy-local setting from disagreeing with actual HTTP responses. Configure
the underlying server explicitly; all baked proxies read its current value.
49 changes: 49 additions & 0 deletions tests/test_bake.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,52 @@ def test_bake_returns_baked_type(
) -> None:
server = bake_chain(httpserver)
assert isinstance(server, BakedHTTPServer)


@pytest.mark.parametrize("status_code", [500, 404])
@pytest.mark.parametrize("chained", [False, True])
def test_bake_no_handler_status_code_is_read_only(httpserver: HTTPServer, status_code: int, *, chained: bool) -> None:
httpserver.no_handler_status_code = status_code
baked = httpserver.bake(method="GET")
sibling = httpserver.bake(method="POST")
if chained:
baked = baked.bake(headers={"X-Request": "test"})

Comment thread
csernazs marked this conversation as resolved.
Resolved
with pytest.raises(AttributeError, match="no_handler_status_code"):
baked.no_handler_status_code = 503 # type: ignore[misc]

assert httpserver.no_handler_status_code == status_code
assert baked.no_handler_status_code == status_code
assert sibling.no_handler_status_code == status_code
assert requests.get(baked.url_for("/unmatched")).status_code == status_code
with pytest.raises(AttributeError, match="no_handler_status_code"):
del baked.no_handler_status_code
assert httpserver.no_handler_status_code == status_code


def test_bake_reads_current_no_handler_status_code(httpserver: HTTPServer) -> None:
baked = httpserver.bake(method="GET")
sibling = httpserver.bake(method="POST")
chained = baked.bake(headers={"X-Request": "test"})

httpserver.no_handler_status_code = 404
assert baked.no_handler_status_code == sibling.no_handler_status_code == chained.no_handler_status_code == 404
assert requests.get(chained.url_for("/unmatched")).status_code == 404

httpserver.clear()
assert baked.no_handler_status_code == sibling.no_handler_status_code == chained.no_handler_status_code == 500
assert requests.get(baked.url_for("/unmatched")).status_code == 500


def test_bake_keeps_other_attribute_assignments_local() -> None:
server = HTTPServer(host="localhost", port=12345)
baked = server.bake(method="GET")

baked.host = "proxy" # type: ignore[attr-defined]
baked.port = 54321 # type: ignore[attr-defined]

assert baked.host == "proxy"
assert baked.port == 54321
assert server.host == "localhost"
assert server.port == 12345
assert baked.url_for("/path") == "http://localhost:12345/path"
Loading