Skip to content

Commit

Permalink
Fix failing tests with responses ≥ 0.22.0
Browse files Browse the repository at this point in the history
Close #1461
Close #1467

Thanks, @alexshpilkin!
  • Loading branch information
jkbrzt committed Jan 15, 2023
1 parent f0563de commit e73c3e6
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions httpie/models.py
Expand Up @@ -16,7 +16,6 @@
from .compat import cached_property
from .utils import split_cookies, parse_content_type_header


ELAPSED_TIME_LABEL = 'Elapsed time'


Expand Down Expand Up @@ -67,27 +66,10 @@ def iter_body(self, chunk_size=1):
def iter_lines(self, chunk_size):
return ((line, b'\n') for line in self._orig.iter_lines(chunk_size))

# noinspection PyProtectedMember
@property
def headers(self):
try:
raw = self._orig.raw
if getattr(raw, '_original_response', None):
raw_version = raw._original_response.version
else:
raw_version = raw.version
except AttributeError:
# Assume HTTP/1.1
raw_version = 11
version = {
9: '0.9',
10: '1.0',
11: '1.1',
20: '2.0',
}[raw_version]

original = self._orig
status_line = f'HTTP/{version} {original.status_code} {original.reason}'
status_line = f'HTTP/{self.version} {original.status_code} {original.reason}'
headers = [status_line]
headers.extend(
': '.join(header)
Expand Down Expand Up @@ -117,6 +99,32 @@ def metadata(self) -> str:
for key, value in data.items()
)

@property
def version(self) -> str:
"""
Return the HTTP version used by the server, e.g. '1.1'.
Assume HTTP/1.1 if version is not available.
"""
mapping = {
9: '0.9',
10: '1.0',
11: '1.1',
20: '2.0',
}
fallback = 11
version = None
try:
raw = self._orig.raw
if getattr(raw, '_original_response', None):
version = raw._original_response.version
else:
version = raw.version
except AttributeError:
pass
return mapping[version or fallback]


class HTTPRequest(HTTPMessage):
"""A :class:`requests.models.Request` wrapper."""
Expand Down

0 comments on commit e73c3e6

Please sign in to comment.