From 29f4f7db1f623a2c2adab3006a21f4dcc59be791 Mon Sep 17 00:00:00 2001 From: Yegor Roganov Date: Thu, 13 Jul 2017 22:47:42 +0300 Subject: [PATCH] Fix bug in CookieJar when a cookie would be expired by mistake Fixes #2084 --- CONTRIBUTORS.txt | 1 + aiohttp/cookiejar.py | 2 +- changes/2084.bugfix | 1 + tests/test_cookiejar.py | 19 +++++++++++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 changes/2084.bugfix diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index c20e0cb217f..c6c44075700 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -178,6 +178,7 @@ Will McGugan Willem de Groot Wilson Ong Yannick Koechlin +Yegor Roganov Young-Ho Cha Yuriy Shatrov Yury Selivanov diff --git a/aiohttp/cookiejar.py b/aiohttp/cookiejar.py index 968b084716b..37e802cb15d 100644 --- a/aiohttp/cookiejar.py +++ b/aiohttp/cookiejar.py @@ -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)) diff --git a/changes/2084.bugfix b/changes/2084.bugfix new file mode 100644 index 00000000000..f3c61ba1237 --- /dev/null +++ b/changes/2084.bugfix @@ -0,0 +1 @@ +Fix issue with CookieJar incorrectly expiring cookies in some edge cases. diff --git a/tests/test_cookiejar.py b/tests/test_cookiejar.py index eef27ddfe8c..28475f26dcc 100644 --- a/tests/test_cookiejar.py +++ b/tests/test_cookiejar.py @@ -1,5 +1,6 @@ import asyncio import datetime +import itertools import os import tempfile import unittest @@ -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