Skip to content

Commit

Permalink
Fixed #31982 -- Convert max_age to an int in HttpResponseBase.set_coo…
Browse files Browse the repository at this point in the history
…kie().
  • Loading branch information
hramezani committed Sep 5, 2020
1 parent 17407ec commit 23c57c6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion django/http/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def set_cookie(self, key, value='', max_age=None, expires=None, path='/',
else:
self.cookies[key]['expires'] = ''
if max_age is not None:
self.cookies[key]['max-age'] = max_age
self.cookies[key]['max-age'] = int(max_age)
# IE requires expires, so set it if hasn't been already.
if not expires:
self.cookies[key]['expires'] = http_date(time.time() + max_age)
Expand Down
4 changes: 2 additions & 2 deletions docs/ref/request-response.txt
Original file line number Diff line number Diff line change
Expand Up @@ -834,8 +834,8 @@ Methods
Sets a cookie. The parameters are the same as in the
:class:`~http.cookies.Morsel` cookie object in the Python standard library.

* ``max_age`` should be a number of seconds, or ``None`` (default) if
the cookie should last only as long as the client's browser session.
* ``max_age`` should be an integer number of seconds, or ``None`` (default)
if the cookie should last only as long as the client's browser session.
If ``expires`` is not specified, it will be calculated.
* ``expires`` should either be a string in the format
``"Wdy, DD-Mon-YY HH:MM:SS GMT"`` or a ``datetime.datetime`` object
Expand Down
8 changes: 8 additions & 0 deletions tests/responses/test_cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ def test_max_age_expiration(self):
self.assertEqual(max_age_cookie['max-age'], 10)
self.assertEqual(max_age_cookie['expires'], http_date(set_cookie_time + 10))

def test_max_age_cookie_type(self):
response = HttpResponse()
for max_age in (10, 10.0):
with self.subTest(max_age=max_age):
response.set_cookie('max_age', max_age=max_age)
self.assertIs(response.cookies['max_age']['max-age'], 10)
self.assertIs(type(response.cookies['max_age']['max-age']), int)

def test_httponly_cookie(self):
response = HttpResponse()
response.set_cookie('example', httponly=True)
Expand Down

0 comments on commit 23c57c6

Please sign in to comment.