adding ISO-8859-1 fallback for reason decoding

This commit is contained in:
Nate Prewitt
2016-08-26 22:46:18 -06:00
parent 58d855e193
commit cd056cd621
2 changed files with 16 additions and 1 deletions
+4 -1
View File
@@ -848,7 +848,10 @@ class Response(object):
http_error_msg = ''
if isinstance(self.reason, bytes):
reason = self.reason.decode('utf-8', 'ignore')
try:
reason = self.reason.decode('utf-8')
except UnicodeDecodeError:
reason = self.reason.decode('iso-8859-1')
else:
reason = self.reason
+12
View File
@@ -1022,6 +1022,18 @@ class TestRequests:
r.encoding = None
assert not r.ok # old behaviour - crashes here
def test_response_reason_unicode_fallback(self):
# check raise_status falls back to ISO-8859-1
r = requests.Response()
r.url = 'some url'
reason = u'Komponenttia ei löydy'
r.reason = reason.encode('latin-1')
r.status_code = 500
r.encoding = None
with pytest.raises(requests.exceptions.HTTPError) as e:
r.raise_for_status()
assert reason in str(e)
def test_response_chunk_size_type(self):
"""Ensure that chunk_size is passed as None or an integer, otherwise
raise a TypeError.