Merge pull request #4023 from rbdixon/http2_response_fix

HTTP2 response reason is None, render as '' in property.
This commit is contained in:
Thomas Kriechbaumer 2020-06-08 19:35:32 +02:00 committed by GitHub
commit bbc2cf331a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -122,10 +122,14 @@ class Response(message.Message):
def reason(self):
"""
HTTP Reason Phrase, e.g. "Not Found".
This is always :py:obj:`None` for HTTP2 requests, because HTTP2 responses do not contain a reason phrase.
HTTP2 responses do not contain a reason phrase and self.data.reason will be :py:obj:`None`.
When :py:obj:`None` return an empty reason phrase so that functions expecting a string work properly.
"""
# Encoding: http://stackoverflow.com/a/16674906/934719
return self.data.reason.decode("ISO-8859-1", "surrogateescape")
if self.data.reason is not None:
return self.data.reason.decode("ISO-8859-1", "surrogateescape")
else:
return ""
@reason.setter
def reason(self, reason):

View File

@ -77,6 +77,12 @@ class TestResponseCore:
resp.data.reason = b'cr\xe9e'
assert resp.reason == "crée"
# HTTP2 responses do not contain a reason phrase and self.data.reason will be None.
# This should render to an empty reason phrase so that functions
# expecting a string work properly.
resp.data.reason = None
assert resp.reason == ""
class TestResponseUtils:
"""