Skip to content

Commit

Permalink
Allow users to override encoding when decoding requests (perhaps usin…
Browse files Browse the repository at this point in the history
…g chardet, as was previously done automatically, but removed to fix #1193). Dont catch exceptions that can't happen any more or at least should very rarely happen, and we can leave it to the user to handle them.
  • Loading branch information
cyberw committed Dec 22, 2019
1 parent 6f946dc commit 1c5aa68
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions locust/contrib/fasthttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,28 +246,20 @@ class FastResponse(CompatResponse):

_response = None

encoding = None
"""In some cases setting the encoding explicitly is needed. If so, do it before calling .text"""

@property
def text(self):
"""
Returns the text content of the response as a decoded string
(unicode on python2)
"""
try:
charset = self.headers.get('content-type', '').partition("charset=")[2]
content = unicode(self.content, charset or 'utf-8', errors='replace')
except (LookupError, TypeError):
# A LookupError is raised if the encoding was not found which could
# indicate a misspelling or similar mistake.
#
# A TypeError can be raised if encoding is None
#
# Fallback to decode without specifying encoding
if self.content is None:
content = None
else:
content = unicode(self.content, errors='replace')
return content

if self.content is None:
return None
self.encoding = self.encoding or self.headers.get('content-type', '').partition("charset=")[2] or 'utf-8'
return unicode(self.content, self.encoding, errors='replace')

def raise_for_status(self):
"""Raise any connection errors that occured during the request"""
if hasattr(self, 'error') and self.error:
Expand Down

0 comments on commit 1c5aa68

Please sign in to comment.