Skip to content

Commit 913d0a4

Browse files
lesnik512claude
andcommitted
feat(story-2.3): Response.with_headers and with_status helpers
Adds Response.with_headers(headers: Mapping[str, str]) -> Self and Response.with_status(status: int) -> Self for ergonomic Response rewriting from middleware. Both use the existing dataclasses.replace pattern. with_status applies no validation by design — value objects don't enforce protocol semantics. Adds `import dataclasses` and `Self` to response.py's typing imports. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 3f83216 commit 913d0a4

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎src/httpware/response.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"""Immutable response value type."""
22

3+
import dataclasses
34
import json
45
from collections.abc import Mapping
56
from dataclasses import dataclass
6-
from typing import Any
7+
from typing import Any, Self
78

89

910
_CHARSET_PREFIX = "charset="
@@ -50,6 +51,14 @@ def json(self) -> Any: # noqa: ANN401
5051
"""Parse `content` as JSON."""
5152
return json.loads(self.content)
5253

54+
def with_headers(self, headers: Mapping[str, str]) -> Self:
55+
"""Return a copy with the given headers merged in (incoming keys override existing)."""
56+
return dataclasses.replace(self, headers={**self.headers, **headers})
57+
58+
def with_status(self, status: int) -> Self:
59+
"""Return a copy with the given status code."""
60+
return dataclasses.replace(self, status=status)
61+
5362

5463
@dataclass(frozen=True, slots=True)
5564
class StreamResponse:

‎tests/test_response.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,35 @@ def test_response_equality_on_identical_fields() -> None:
8989
assert r1 == r2
9090
assert r1 != Response(status=200, headers={"a": "1"}, content=b"x", url="/", elapsed=0.6)
9191
assert r1 != Response(status=201, headers={"a": "1"}, content=b"x", url="/", elapsed=0.5)
92+
93+
94+
def test_response_with_headers_merges_new_headers() -> None:
95+
resp = Response(status=200, headers={"keep": "1"}, content=b"", url="/", elapsed=0.0)
96+
new = resp.with_headers({"x-trace": "abc"})
97+
assert new.headers == {"keep": "1", "x-trace": "abc"}
98+
assert resp.headers == {"keep": "1"}
99+
100+
101+
def test_response_with_headers_overrides_existing_key() -> None:
102+
resp = Response(status=200, headers={"x-trace": "old"}, content=b"", url="/", elapsed=0.0)
103+
new = resp.with_headers({"x-trace": "new"})
104+
assert new.headers == {"x-trace": "new"}
105+
assert resp.headers == {"x-trace": "old"}
106+
107+
108+
def test_response_with_status_replaces_status() -> None:
109+
resp = Response(status=200, headers={"a": "1"}, content=b"body", url="/x", elapsed=0.5)
110+
new = resp.with_status(503)
111+
assert new.status == 503 # noqa: PLR2004
112+
assert new.headers == {"a": "1"}
113+
assert new.content == b"body"
114+
assert new.url == "/x"
115+
assert new.elapsed == 0.5 # noqa: PLR2004
116+
assert resp.status == 200 # noqa: PLR2004
117+
118+
119+
def test_response_with_status_accepts_arbitrary_int() -> None:
120+
resp = Response(status=200, headers={}, content=b"", url="/", elapsed=0.0)
121+
# No validation by design — value objects don't enforce protocol semantics.
122+
new = resp.with_status(99)
123+
assert new.status == 99 # noqa: PLR2004

0 commit comments

Comments
 (0)