Repository navigation
add a flag to prevent cookies being restored from a cache hit - #427
pratheekrebala wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Thank you. The proposed feature is justified, and its core implementation is correct.
While I was reviewing this, I also found a related pre-existing defect I am going to address in the next PR before we publish a patch release. The test suggestion below would be welcome if you handle it here, but I am okay with adding an integration test myself in a follow-up PR. Please re-request a review once ready.
Test coverage
The new test verifies that a mocked cache hit leaves the cookie jar empty when restore_cookies=False, including a cached redirect. That is useful, but narrow.
Before merging, I would strengthen the tests around the public API and actual HTTP behavior:
- Real backend configuration: Construct
CacheBackend(restore_cookies=False)rather than manually setting the property on aMagicMock. - Cookie preservation: Start with an existing
sid=new, retrieve a cached response containingsid=old, and verify thatsid=newremains. - Fresh responses: Verify that disabling restoration does not prevent a genuine HTTP response from updating the cookie jar.
- Refresh behavior: Verify that
refresh=True, including an HTTP 304 response, does not replay old cached cookies. - Default compatibility: Confirm explicitly that the default configuration still restores cookies.
The third test is particularly important. The flag must distinguish between replaying cookies from the cache and accepting fresh cookies from an HTTP server.
| modifying the active session's cookies, use `restore_cookies=False`: | ||
|
|
||
| ```python | ||
| >>> cache = SQLiteCache(restore_cookies=False) |
There was a problem hiding this comment.
SQLiteCache is the lower-level storage implementation, not the session cache backend. Its constructor also requires a filename, so the example cannot run as written. Use SQLiteBackend instead:
from aiohttp_client_cache import CachedSession, SQLiteBackend
cache = SQLiteBackend(restore_cookies=False)
async with CachedSession(cache=cache) as session:
...
We're working on migrating away from
requests-cacheand we got bit by a subtle bug in how the two packages handle cookie restoration.Unlike the previous package, the
aiohttpversion seems to unconditionally restore cookies when we get a cache hit. This ends up invalidating login sessions after a cache hit.aiohttp-client-cache/aiohttp_client_cache/session.py
Lines 104 to 114 in dbb6e18
I added a small flag to allow users to revert back to the previous behavior. The default behavior here should be unchanged.
Thank you for looking into this!