diff --git a/django/http/response.py b/django/http/response.py index ed5c14eef8..94d14ddb54 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -190,6 +190,8 @@ class HttpResponseBase(six.Iterator): max_age = max(0, delta.days * 86400 + delta.seconds) else: self.cookies[key]['expires'] = expires + else: + self.cookies[key]['expires'] = '' if max_age is not None: self.cookies[key]['max-age'] = max_age # IE requires expires, so set it if hasn't been already. diff --git a/tests/requests/tests.py b/tests/requests/tests.py index 66fe8c92ec..180a5bca6b 100644 --- a/tests/requests/tests.py +++ b/tests/requests/tests.py @@ -208,6 +208,18 @@ class RequestsTests(SimpleTestCase): datetime_cookie = response.cookies['datetime'] self.assertEqual(datetime_cookie['max-age'], 10) + def test_create_cookie_after_deleting_cookie(self): + """ + Setting a cookie after deletion should clear the expiry date. + """ + response = HttpResponse() + response.set_cookie('c', 'old-value') + self.assertEqual(response.cookies['c']['expires'], '') + response.delete_cookie('c') + self.assertEqual(response.cookies['c']['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT') + response.set_cookie('c', 'new-value') + self.assertEqual(response.cookies['c']['expires'], '') + def test_far_expiration(self): "Cookie will expire when an distant expiration time is provided" response = HttpResponse()