Skip to content

Commit

Permalink
Add support for third-party partitioned cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Dec 22, 2023
1 parent f9cc1e1 commit e91eca8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/microdot/microdot.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ def __init__(self, body='', status_code=200, headers=None, reason=None):
self.is_head = False

def set_cookie(self, cookie, value, path=None, domain=None, expires=None,
max_age=None, secure=False, http_only=False):
max_age=None, secure=False, http_only=False,
partitioned=False):
"""Add a cookie to the response.
:param cookie: The cookie's name.
Expand All @@ -584,6 +585,7 @@ def set_cookie(self, cookie, value, path=None, domain=None, expires=None,
:param max_age: The cookie's ``Max-Age`` value.
:param secure: The cookie's ``secure`` flag.
:param http_only: The cookie's ``HttpOnly`` flag.
:param partitioned: Whether the cookie is partitioned.
"""
http_cookie = '{cookie}={value}'.format(cookie=cookie, value=value)
if path:
Expand All @@ -602,6 +604,8 @@ def set_cookie(self, cookie, value, path=None, domain=None, expires=None,
http_cookie += '; Secure'
if http_only:
http_cookie += '; HttpOnly'
if partitioned:
http_cookie += '; Partitioned'
if 'Set-Cookie' in self.headers:
self.headers['Set-Cookie'].append(http_cookie)
else:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def test_create_with_status_and_reason(self):
def test_cookies(self):
res = Response('ok')
res.set_cookie('foo1', 'bar1')
res.set_cookie('foo2', 'bar2', path='/')
res.set_cookie('foo2', 'bar2', path='/', partitioned=True)
res.set_cookie('foo3', 'bar3', domain='example.com:1234')
res.set_cookie('foo4', 'bar4',
expires=datetime(2019, 11, 5, 2, 23, 54))
Expand All @@ -196,7 +196,7 @@ def test_cookies(self):
res.delete_cookie('foo8', http_only=True)
self.assertEqual(res.headers, {'Set-Cookie': [
'foo1=bar1',
'foo2=bar2; Path=/',
'foo2=bar2; Path=/; Partitioned',
'foo3=bar3; Domain=example.com:1234',
'foo4=bar4; Expires=Tue, 05 Nov 2019 02:23:54 GMT',
'foo5=bar5; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=123',
Expand Down

0 comments on commit e91eca8

Please sign in to comment.