Skip to content

Commit 8b865ae

Browse files
lesnik512claude
andcommitted
feat(story-2.3): Request.with_cookie and with_cookies helpers
Adds Request.with_cookie(name, value) -> Self and Request.with_cookies(cookies: Mapping) -> Self. Singular adds/replaces one cookie; plural merges a mapping with incoming keys overriding. Three tests cover the add, replace, and merge cases. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 704999d commit 8b865ae

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎src/httpware/request.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,11 @@ def with_query(self, params: Mapping[str, str]) -> Self:
3737
def with_headers(self, headers: Mapping[str, str]) -> Self:
3838
"""Return a copy with the given headers merged in (incoming keys override existing)."""
3939
return dataclasses.replace(self, headers={**self.headers, **headers})
40+
41+
def with_cookie(self, name: str, value: str) -> Self:
42+
"""Return a copy with the given cookie added or replaced."""
43+
return dataclasses.replace(self, cookies={**self.cookies, name: value})
44+
45+
def with_cookies(self, cookies: Mapping[str, str]) -> Self:
46+
"""Return a copy with the given cookies merged in (incoming keys override existing)."""
47+
return dataclasses.replace(self, cookies={**self.cookies, **cookies})

‎tests/test_request.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,24 @@ def test_with_headers_empty_mapping_returns_distinct_copy() -> None:
9797
new = r.with_headers({})
9898
assert new == r
9999
assert new is not r
100+
101+
102+
def test_with_cookie_adds_single_cookie() -> None:
103+
r = Request(method="GET", url="/")
104+
new = r.with_cookie("session", "abc")
105+
assert new.cookies == {"session": "abc"}
106+
assert r.cookies == {}
107+
108+
109+
def test_with_cookie_replaces_existing_cookie() -> None:
110+
r = Request(method="GET", url="/", cookies={"session": "old"})
111+
new = r.with_cookie("session", "new")
112+
assert new.cookies == {"session": "new"}
113+
assert r.cookies == {"session": "old"}
114+
115+
116+
def test_with_cookies_merges_new_cookies() -> None:
117+
r = Request(method="GET", url="/", cookies={"keep": "1", "replace": "old"})
118+
new = r.with_cookies({"replace": "new", "add": "2"})
119+
assert new.cookies == {"keep": "1", "replace": "new", "add": "2"}
120+
assert r.cookies == {"keep": "1", "replace": "old"}

0 commit comments

Comments
 (0)