Skip to content

Commit

Permalink
Fix bug in CookieJar when a cookie would be expired by mistake (#2088)
Browse files Browse the repository at this point in the history
Fixes #2084
  • Loading branch information
roganov authored and asvetlov committed Jul 25, 2017
1 parent 81357fd commit f6896b5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ Will McGugan
Willem de Groot
Wilson Ong
Yannick Koechlin
Yegor Roganov
Young-Ho Cha
Yuriy Shatrov
Yury Selivanov
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _do_expiration(self):
cookies = self._cookies
expirations = self._expirations
for (domain, name), when in expirations.items():
if when < now:
if when <= now:
cookies[domain].pop(name, None)
to_del.append((domain, name))
self._host_only_cookies.discard((domain, name))
Expand Down
1 change: 1 addition & 0 deletions changes/2084.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix issue with CookieJar incorrectly expiring cookies in some edge cases.
19 changes: 19 additions & 0 deletions tests/test_cookiejar.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import datetime
import itertools
import os
import tempfile
import unittest
Expand Down Expand Up @@ -577,3 +578,21 @@ def test_invalid_values(self):

cookie = cookies_sent["invalid-expires-cookie"]
self.assertEqual(cookie["expires"], "")

def test_cookie_not_expired_when_added_after_removal(self):
"""Test case for https://github.com/aio-libs/aiohttp/issues/2084"""
timestamps = [533588.993, 533588.993, 533588.993,
533588.993, 533589.093, 533589.093]

loop = mock.Mock()
loop.time.side_effect = itertools.chain(
timestamps, itertools.cycle([timestamps[-1]]))

jar = CookieJar(unsafe=True, loop=loop)
# Remove `foo` cookie.
jar.update_cookies(SimpleCookie('foo=""; Max-Age=0'))
# Set `foo` cookie to `bar`.
jar.update_cookies(SimpleCookie('foo="bar"'))

# Assert that there is a cookie.
assert len(jar) == 1

0 comments on commit f6896b5

Please sign in to comment.