Skip to content

Commit

Permalink
Default response.encoding to utf-8 if response body is None
Browse files Browse the repository at this point in the history
  • Loading branch information
JWCook committed Jul 26, 2021
1 parent ddfde11 commit b9fe119
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
@@ -1,5 +1,8 @@
# History

## 0.4.2 (2021-07-26)
* Fix handling of `CachedResponse.encoding` when the response body is `None`

## 0.4.1 (2021-07-09)
* Fix initialziation of `SQLiteBackend` so it can be created outside main event loop

Expand Down
9 changes: 5 additions & 4 deletions aiohttp_client_cache/response.py
Expand Up @@ -52,7 +52,7 @@ class CachedResponse:
status: int = attr.ib()
url: URL = attr.ib(converter=URL)
version: str = attr.ib()
_body: Any = attr.ib(default=None)
_body: Any = attr.ib(default=b'')
_links: LinkItems = attr.ib(factory=list)
cookies: SimpleCookie = attr.ib(default=None)
created_at: datetime = attr.ib(factory=datetime.utcnow)
Expand Down Expand Up @@ -80,11 +80,12 @@ async def from_client_response(cls, client_response: ClientResponse, expires: da
response.expires = expires
response.real_url = client_response.request_info.real_url

# The encoding may be unset even if the response has been read
# The encoding may be unset even if the response has been read, and
# get_encoding() does not handle certain edge cases like an empty response body
try:
response.encoding = client_response.get_encoding()
except RuntimeError:
pass
except (RuntimeError, TypeError):
response.encoding = 'utf-8'

if client_response.history:
response.history = (
Expand Down
11 changes: 11 additions & 0 deletions test/unit/test_response.py
Expand Up @@ -14,6 +14,7 @@ async def get_test_response(client_factory, url='/', **kwargs):
app.router.add_route('GET', '/valid_url', mock_handler)
app.router.add_route('GET', '/json', json_mock_handler)
app.router.add_route('GET', '/empty_content', empty_mock_handler)
app.router.add_route('GET', '/null_content', null_mock_handler)
client = await client_factory(app)
client_response = await client.get(url)

Expand All @@ -34,10 +35,15 @@ async def json_mock_handler(request):
return web.Response(body=b'{"key": "value"}')


# Note: Empty string vs null can trigger different corner cases
async def empty_mock_handler(request):
return web.Response(body=b' ')


async def null_mock_handler(request):
return web.Response(body=None)


async def test_basic_attrs(aiohttp_client):
response = await get_test_response(aiohttp_client)

Expand Down Expand Up @@ -130,6 +136,11 @@ async def test_json__empty_content(aiohttp_client):
assert await response.json() is None


async def test_json__null_content(aiohttp_client):
response = await get_test_response(aiohttp_client, '/null_content')
assert await response.json() is None


async def test_json__non_json_content(aiohttp_client):
response = await get_test_response(aiohttp_client)
with pytest.raises(ValueError):
Expand Down

0 comments on commit b9fe119

Please sign in to comment.