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)

Co-authored-by: Sam Bull <git@sambull.org>
  • Loading branch information
bdraco and Dreamsorcerer committed Jan 21, 2024
1 parent 43a5bc5 commit e8339e7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
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 @@ -152,7 +152,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
21 changes: 21 additions & 0 deletions tests/test_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,27 @@ async def test_cookie_jar_clear_expired():
assert len(sut) == 0


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()
Expand Down

0 comments on commit e8339e7

Please sign in to comment.