diff --git a/src/microdot/microdot.py b/src/microdot/microdot.py index 0341583..49128ad 100644 --- a/src/microdot/microdot.py +++ b/src/microdot/microdot.py @@ -598,7 +598,7 @@ class Response: else: # pragma: no cover http_cookie += '; Expires=' + time.strftime( '%a, %d %b %Y %H:%M:%S GMT', expires.timetuple()) - if max_age: + if max_age is not None: http_cookie += '; Max-Age=' + str(max_age) if secure: http_cookie += '; Secure' @@ -616,10 +616,10 @@ class Response: :param cookie: The cookie's name. :param kwargs: Any cookie opens and flags supported by - ``set_cookie()`` except ``expires``. + ``set_cookie()`` except ``expires`` and ``max_age``. """ self.set_cookie(cookie, '', expires='Thu, 01 Jan 1970 00:00:01 GMT', - **kwargs) + max_age=0, **kwargs) def complete(self): if isinstance(self.body, bytes) and \ diff --git a/src/microdot/test_client.py b/src/microdot/test_client.py index 4ae83d8..fcc5a9c 100644 --- a/src/microdot/test_client.py +++ b/src/microdot/test_client.py @@ -141,7 +141,17 @@ class TestClient: cookie_options = cookie_value.split(';') delete = False for option in cookie_options[1:]: - if option.strip().lower().startswith('expires='): + if option.strip().lower().startswith( + 'max-age='): # pragma: no cover + _, age = option.strip().split('=', 1) + try: + age = int(age) + except ValueError: # pragma: no cover + age = 0 + if age <= 0: + delete = True + break + elif option.strip().lower().startswith('expires='): _, e = option.strip().split('=', 1) # this is a very limited parser for cookie expiry # that only detects a cookie deletion request when diff --git a/tests/test_response.py b/tests/test_response.py index 1fdae2b..ae2f4c4 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -203,7 +203,8 @@ class TestResponse(unittest.TestCase): 'foo7=bar7; Path=/foo; Domain=example.com:1234; ' 'Expires=Tue, 05 Nov 2019 02:23:54 GMT; Max-Age=123; Secure; ' 'HttpOnly', - 'foo8=; Expires=Thu, 01 Jan 1970 00:00:01 GMT; HttpOnly', + ('foo8=; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Max-Age=0; ' + 'HttpOnly'), ]}) def test_redirect(self):