Skip to content

Commit

Permalink
Merge pull request #111 from JWCook/fix-empty-cookies
Browse files Browse the repository at this point in the history
Fix issue with restoring empty session cookies
  • Loading branch information
JWCook committed Feb 13, 2022
2 parents dc051ef + 88e8351 commit bf4bc56
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion HISTORY.md
@@ -1,7 +1,8 @@
# History

## 0.6.1 (Unreleased)
## 0.6.1 (2022-02-13)
* Migrate to aioredis 2.0
* Fix issue with restoring empty session cookies

## 0.6.0 (2022-02-12)
* Add a `bulk_delete()` method for all backends to improve performance of `delete_expired_responses()`
Expand Down
2 changes: 1 addition & 1 deletion aiohttp_client_cache/response.py
Expand Up @@ -61,7 +61,7 @@ class CachedResponse:
_body: Any = attr.ib(default=b'')
_content: StreamReader = attr.ib(default=None)
_links: LinkItems = attr.ib(factory=list)
cookies: SimpleCookie = attr.ib(default=None)
cookies: SimpleCookie = attr.ib(factory=SimpleCookie)
created_at: datetime = attr.ib(factory=datetime.utcnow)
encoding: str = attr.ib(default='utf-8')
expires: Optional[datetime] = attr.ib(default=None)
Expand Down
9 changes: 6 additions & 3 deletions aiohttp_client_cache/session.py
Expand Up @@ -39,13 +39,16 @@ def __init__(
@copy_signature(ClientSession._request)
async def _request(self, method: str, str_or_url: StrOrURL, **kwargs) -> AnyResponse:
"""Wrapper around :py:meth:`.SessionClient._request` that adds caching"""
# Attempt to fetch cached response; if missing or expired, fetch new one
# Attempt to fetch cached response
response, actions = await self.cache.request(method, str_or_url, **kwargs)

# Restore any cached cookies to the session
if response:
self._cookie_jar.update_cookies(response.cookies, response.url)
self.cookie_jar.update_cookies(response.cookies or {}, response.url)
for redirect in response.history:
self._cookie_jar.update_cookies(redirect.cookies, redirect.url)
self.cookie_jar.update_cookies(redirect.cookies or {}, redirect.url)
return response
# If the response was missing or expired, send and cache a new request
else:
logger.debug(f'Cached response not found; making request to {str_or_url}')
new_response = await super()._request(method, str_or_url, **kwargs) # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion test/conftest.py
Expand Up @@ -33,7 +33,7 @@


# Configure logging for pytest session
basicConfig(level='INFO')
basicConfig(level='ERROR')
getLogger('aiohttp_client_cache').setLevel('DEBUG')


Expand Down
Empty file added test/unit/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions test/unit/test_session.py
Expand Up @@ -112,3 +112,16 @@ async def test_session__cookies(mock_request):
await session.get('http://test.url')
cookies = session.cookie_jar.filter_cookies('https://test.com')
assert cookies['test_cookie'].value == 'value'


@patch.object(ClientSession, '_request')
async def test_session__empty_cookies(mock_request):
"""Previous versions didn't set cookies if they were empty. Just make sure it doesn't explode."""
cache = MagicMock(spec=CacheBackend)
response = AsyncMock(is_expired=False, url=URL('https://test.com'), cookies=None)
cache.request.return_value = response, CacheActions()
session = CachedSession(cache=cache)

session.cookie_jar.clear()
await session.get('http://test.url')
assert not session.cookie_jar.filter_cookies('https://test.com')

0 comments on commit bf4bc56

Please sign in to comment.