Skip to content

Commit 4cb155e

Browse files
Improved cookie support in the test client
1 parent dea79c5 commit 4cb155e

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/microdot/microdot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ def set_cookie(self, cookie, value, path=None, domain=None, expires=None,
598598
else: # pragma: no cover
599599
http_cookie += '; Expires=' + time.strftime(
600600
'%a, %d %b %Y %H:%M:%S GMT', expires.timetuple())
601-
if max_age:
601+
if max_age is not None:
602602
http_cookie += '; Max-Age=' + str(max_age)
603603
if secure:
604604
http_cookie += '; Secure'
@@ -616,10 +616,10 @@ def delete_cookie(self, cookie, **kwargs):
616616
617617
:param cookie: The cookie's name.
618618
:param kwargs: Any cookie opens and flags supported by
619-
``set_cookie()`` except ``expires``.
619+
``set_cookie()`` except ``expires`` and ``max_age``.
620620
"""
621621
self.set_cookie(cookie, '', expires='Thu, 01 Jan 1970 00:00:01 GMT',
622-
**kwargs)
622+
max_age=0, **kwargs)
623623

624624
def complete(self):
625625
if isinstance(self.body, bytes) and \

src/microdot/test_client.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,17 @@ def _update_cookies(self, res):
141141
cookie_options = cookie_value.split(';')
142142
delete = False
143143
for option in cookie_options[1:]:
144-
if option.strip().lower().startswith('expires='):
144+
if option.strip().lower().startswith(
145+
'max-age='): # pragma: no cover
146+
_, age = option.strip().split('=', 1)
147+
try:
148+
age = int(age)
149+
except ValueError: # pragma: no cover
150+
age = 0
151+
if age <= 0:
152+
delete = True
153+
break
154+
elif option.strip().lower().startswith('expires='):
145155
_, e = option.strip().split('=', 1)
146156
# this is a very limited parser for cookie expiry
147157
# that only detects a cookie deletion request when

tests/test_response.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ def test_cookies(self):
203203
'foo7=bar7; Path=/foo; Domain=example.com:1234; '
204204
'Expires=Tue, 05 Nov 2019 02:23:54 GMT; Max-Age=123; Secure; '
205205
'HttpOnly',
206-
'foo8=; Expires=Thu, 01 Jan 1970 00:00:01 GMT; HttpOnly',
206+
('foo8=; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Max-Age=0; '
207+
'HttpOnly'),
207208
]})
208209

209210
def test_redirect(self):

0 commit comments

Comments
 (0)