Skip to content

Commit

Permalink
Fix duplicate cookie expiration calls in the CookieJar implementation (
Browse files Browse the repository at this point in the history
…#7784) (#8047)

Co-authored-by: Sam Bull <[email protected]>
  • Loading branch information
bdraco and Dreamsorcerer authored Jan 21, 2024
1 parent a42ed20 commit 1a389a5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/7784.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix duplicate cookie expiration calls in the CookieJar implementation
7 changes: 6 additions & 1 deletion aiohttp/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ def __iter__(self) -> "Iterator[Morsel[str]]":
yield from val.values()

def __len__(self) -> int:
return sum(1 for i in self)
"""Return number of cookies.
This function does not iterate self to avoid unnecessary expiration
checks.
"""
return sum(len(cookie.values()) for cookie in self._cookies.values())

def _do_expiration(self) -> None:
self.clear(lambda x: False)
Expand Down
23 changes: 22 additions & 1 deletion tests/test_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,28 @@ async def test_cookie_jar_clear_expired():
assert len(sut) == 0


async def test_cookie_jar_clear_domain():
async def test_cookie_jar_filter_cookies_expires():
"""Test that calling filter_cookies will expire stale cookies."""
jar = CookieJar()
assert len(jar) == 0

cookie = SimpleCookie()

cookie["foo"] = "bar"
cookie["foo"]["expires"] = "Tue, 1 Jan 1990 12:00:00 GMT"

with freeze_time("1980-01-01"):
jar.update_cookies(cookie)

assert len(jar) == 1

# filter_cookies should expire stale cookies
jar.filter_cookies(URL("http://any.com/"))

assert len(jar) == 0


async def test_cookie_jar_clear_domain() -> None:
sut = CookieJar()
cookie = SimpleCookie()
cookie["foo"] = "bar"
Expand Down

0 comments on commit 1a389a5

Please sign in to comment.