Skip to content

Commit

Permalink
Merge pull request #261 from KeepSafe/cookies_path
Browse files Browse the repository at this point in the history
Use path=/ by default for cookies
  • Loading branch information
asvetlov committed Jan 28, 2015
2 parents a336404 + f7a43d0 commit a635563
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def cookies(self):
return self._cookies

def set_cookie(self, name, value, *, expires=None,
domain=None, max_age=None, path=None,
domain=None, max_age=None, path='/',
secure=None, httponly=None, version=None):
"""Set or update response cookie.
Expand Down Expand Up @@ -501,7 +501,7 @@ def set_cookie(self, name, value, *, expires=None,
if version is not None:
c['version'] = version

def del_cookie(self, name, *, domain=None, path=None):
def del_cookie(self, name, *, domain=None, path='/'):
"""Delete cookie.
Creates new empty expired cookie.
Expand Down
18 changes: 13 additions & 5 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ StreamResponse
:meth:`set_cookie`, :meth:`del_cookie` for cookie
manipulations.

.. method:: set_cookie(name, value, *, expires=None, \
domain=None, max_age=None, path=None, \
.. method:: set_cookie(name, value, *, path='/', expires=None, \
domain=None, max_age=None, \
secure=None, httponly=None, version=None)

Convenient way for setting :attr:`cookies`, allows to specify
Expand All @@ -436,7 +436,7 @@ StreamResponse
immediately. (optional)

:param str path: specifies the subset of URLs to
which this cookie applies. (optional)
which this cookie applies. (optional, ``'/'`` by default)

:param bool secure: attribute (with no value) directs
the user agent to use only (unspecified)
Expand All @@ -458,15 +458,23 @@ StreamResponse
specification the cookie
conforms. (Optional, *version=1* by default)

.. method:: del_cookie(name, *, domain=None, path=None)
.. versionchanged:: 0.14.3

Default value for *path* changed from ``None`` to ``'/'``.

.. method:: del_cookie(name, *, path='/', domain=None)

Deletes cookie.

:param str name: cookie name

:param str domain: optional cookie domain

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

.. versionchanged:: 0.14.3

Default value for *path* changed from ``None`` to ``'/'``.

.. attribute:: content_length

Expand Down
24 changes: 15 additions & 9 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,19 +256,23 @@ def test_response_cookies(self):
self.assertEqual(str(resp.cookies), '')

resp.set_cookie('name', 'value')
self.assertEqual(str(resp.cookies), 'Set-Cookie: name=value')
self.assertEqual(str(resp.cookies), 'Set-Cookie: name=value; Path=/')
resp.set_cookie('name', 'other_value')
self.assertEqual(str(resp.cookies), 'Set-Cookie: name=other_value')
self.assertEqual(str(resp.cookies),
'Set-Cookie: name=other_value; Path=/')

resp.cookies['name'] = 'another_other_value'
resp.cookies['name']['max-age'] = 10
self.assertEqual(str(resp.cookies),
'Set-Cookie: name=another_other_value; Max-Age=10')
self.assertEqual(
str(resp.cookies),
'Set-Cookie: name=another_other_value; Max-Age=10; Path=/')

resp.del_cookie('name')
self.assertEqual(str(resp.cookies), 'Set-Cookie: name=; Max-Age=0')
self.assertEqual(
str(resp.cookies),
'Set-Cookie: name=; Max-Age=0; Path=/')

resp.set_cookie('name', 'value', domain='local.host')
resp.set_cookie('name', 'value', domain='local.host', path=None)
self.assertEqual(str(resp.cookies),
'Set-Cookie: name=value; Domain=local.host')

Expand All @@ -283,7 +287,7 @@ def test_response_cookie_path(self):
resp.set_cookie('name', 'value', expires='123')
self.assertEqual(str(resp.cookies),
'Set-Cookie: name=value; expires=123;'
' Path=/some/path')
' Path=/')
resp.set_cookie('name', 'value', domain='example.com',
path='/home', expires='123', max_age='10',
secure=True, httponly=True, version='2.0')
Expand All @@ -304,15 +308,17 @@ def test_response_cookie__issue_del_cookie(self):
self.assertEqual(str(resp.cookies), '')

resp.del_cookie('name')
self.assertEqual(str(resp.cookies), 'Set-Cookie: name=; Max-Age=0')
self.assertEqual(str(resp.cookies),
'Set-Cookie: name=; Max-Age=0; Path=/')

def test_cookie_set_after_del(self):
resp = StreamResponse()

resp.del_cookie('name')
resp.set_cookie('name', 'val')
# check for Max-Age dropped
self.assertEqual(str(resp.cookies), 'Set-Cookie: name=val')
self.assertEqual(str(resp.cookies),
'Set-Cookie: name=val; Path=/')

def test_set_status_with_reason(self):
resp = StreamResponse()
Expand Down

0 comments on commit a635563

Please sign in to comment.