Skip to content

Commit

Permalink
Add IE support for cookie deletion. (#994)
Browse files Browse the repository at this point in the history
* Add IE support for cookie deletion.

IE<=11 ( !!! ) doesn't support the MaxAge property for cookies so del_cookie() also needs to set the Expires cookie header to support IE. Uses the UTC epoch for a standard expire time.

* Add IE support for cookie deletion. Style and test fixes.

* Add IE support for cookie deletion. Documentation updates.

* Add IE support for cookie deletion. Python 3.4 related test fixes.

* Add IE support for cookie deletion. set_cookie correction.
  • Loading branch information
Aleksey Kutepov authored and asvetlov committed Jul 23, 2016
1 parent 8a2a5dc commit 2bcdd5c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 7 additions & 1 deletion aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,12 @@ def set_cookie(self, name, value, *, expires=None,

self._cookies[name] = value
c = self._cookies[name]

if expires is not None:
c['expires'] = expires
elif c.get('expires') == 'Thu, 01 Jan 1970 00:00:00 GMT':
del c['expires']

if domain is not None:
c['domain'] = domain

Expand All @@ -537,7 +541,9 @@ def del_cookie(self, name, *, domain=None, path='/'):
"""
# TODO: do we need domain/path here?
self._cookies.pop(name, None)
self.set_cookie(name, '', max_age=0, domain=domain, path=path)
self.set_cookie(name, '', max_age=0,
expires="Thu, 01 Jan 1970 00:00:00 GMT",
domain=domain, path=path)

@property
def content_length(self):
Expand Down
5 changes: 5 additions & 0 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@ StreamResponse

:param str path: optional cookie path, ``'/'`` by default

.. versionchanged:: 0.23

Fixed cookie expiration support for
Internet Explorer (version less than 11).

.. attribute:: content_length

*Content-Length* for outgoing response.
Expand Down
7 changes: 5 additions & 2 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ def test_response_cookies():
'Set-Cookie: name=another_other_value; Max-Age=10; Path=/')

resp.del_cookie('name')
expected = 'Set-Cookie: name=("")?; Max-Age=0; Path=/'
expected = ('Set-Cookie: name=("")?; '
'expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/')
assert re.match(expected, str(resp.cookies))

resp.set_cookie('name', 'value', domain='local.host')
Expand Down Expand Up @@ -491,7 +492,8 @@ def test_response_cookie__issue_del_cookie():
assert str(resp.cookies) == ''

resp.del_cookie('name')
expected = 'Set-Cookie: name=("")?; Max-Age=0; Path=/'
expected = ('Set-Cookie: name=("")?; '
'expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/')
assert re.match(expected, str(resp.cookies))


Expand All @@ -502,6 +504,7 @@ def test_cookie_set_after_del():
resp.set_cookie('name', 'val')
# check for Max-Age dropped
expected = 'Set-Cookie: name=val; Path=/'
print(expected)
assert str(resp.cookies) == expected


Expand Down

0 comments on commit 2bcdd5c

Please sign in to comment.