Skip to content

Commit

Permalink
feat(cookies): extend monkey-patching of Partitioned to make it a f…
Browse files Browse the repository at this point in the history
…lag (#2428)

* feat(cookies): extend monkey-patching of Partitioned to make it flag

* fix(testing): restore an accidentally removed full stop
  • Loading branch information
vytas7 authored Jan 20, 2025
1 parent f6203f0 commit 8e866bd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
4 changes: 3 additions & 1 deletion falcon/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@
# attribute that will probably be added in Python 3.14.
# We do it this way because SimpleCookie does not give us a simple way to
# specify our own subclass of Morsel.
_reserved_cookie_attrs = http_cookies.Morsel._reserved # type: ignore
_reserved_cookie_attrs = http_cookies.Morsel._reserved # type: ignore[attr-defined]
if 'partitioned' not in _reserved_cookie_attrs: # pragma: no cover
_reserved_cookie_attrs['partitioned'] = 'Partitioned'
# NOTE(vytas): Partitioned is a boolean flag, similar to HttpOnly and Secure.
http_cookies.Morsel._flags.add('partitioned') # type: ignore[attr-defined]
# NOTE(vytas): Morsel._reserved_defaults is a new optimization in Python 3.14+
# for faster initialization of Morsel instances.
# TODO(vytas): Remove this part of monkey-patching in the case CPython 3.14
Expand Down
23 changes: 17 additions & 6 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,23 @@ def test_cookies_setable():

resp.set_cookie('foo', 'wrong-cookie', max_age=301)
resp.set_cookie('foo', 'bar', max_age=300)
morsel = resp._cookies['foo']

assert isinstance(morsel, http_cookies.Morsel)
assert morsel.key == 'foo'
assert morsel.value == 'bar'
assert morsel['max-age'] == 300
resp.set_cookie('bar', 'baz', same_site='None', partitioned=True)

morsel1 = resp._cookies['foo']
morsel2 = resp._cookies['bar']

assert isinstance(morsel1, http_cookies.Morsel)
assert morsel1.key == 'foo'
assert morsel1.value == 'bar'
assert morsel1['max-age'] == 300

assert isinstance(morsel2, http_cookies.Morsel)
assert morsel2.key == 'bar'
assert morsel2.value == 'baz'
assert morsel2['partitioned'] is True
assert morsel2.output() == (
'Set-Cookie: bar=baz; HttpOnly; Partitioned; SameSite=None; Secure'
)


@pytest.mark.parametrize('cookie_name', ('foofloat', 'foostring'))
Expand Down

0 comments on commit 8e866bd

Please sign in to comment.