Skip to content

Commit

Permalink
add test for bad encoding alternative handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bmuller committed Jan 21, 2017
1 parent 450e0fa commit 9c7f964
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ def text(self, encoding=None, errors='strict'):
if encoding is None:
encoding = self._get_encoding()

return self._content.decode(encoding, errors)
return self._content.decode(encoding, errors=errors)

@asyncio.coroutine
def json(self, *, encoding=None, loads=json.loads):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,29 @@ def side_effect(*args, **kwargs):
assert response._connection is None


@asyncio.coroutine
def test_text_bad_encoding(loop):
response = ClientResponse('get', URL('http://def-cl-resp.org'))
response._post_init(loop)

def side_effect(*args, **kwargs):
fut = helpers.create_future(loop)
fut.set_result('{"тестkey": "пройденvalue"}'.encode('cp1251'))
return fut

# lie about the encoding
response.headers = {
'Content-Type': 'application/json;charset=utf-8'}
content = response.content = mock.Mock()
content.read.side_effect = side_effect
with pytest.raises(UnicodeDecodeError):
yield from response.text()
# only the valid utf-8 characters will be returned
res = yield from response.text(errors='ignore')
assert res == '{"key": "value"}'
assert response._connection is None


@asyncio.coroutine
def test_text_custom_encoding(loop):
response = ClientResponse('get', URL('http://def-cl-resp.org'))
Expand Down

0 comments on commit 9c7f964

Please sign in to comment.