Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate cookie expiration calls in the CookieJar implementation #7784

Merged
merged 8 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading