From e73c3e6c249b89496b4f81fa20bb449911da79f1 Mon Sep 17 00:00:00 2001 From: Jakub Roztocil Date: Sun, 15 Jan 2023 17:43:17 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20failing=20tests=20with=20responses=20?= =?UTF-8?q?=E2=89=A5=200.22.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Close #1461 Close #1467 Thanks, @alexshpilkin! --- httpie/models.py | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/httpie/models.py b/httpie/models.py index d97b55e3..6fe02d5f 100644 --- a/httpie/models.py +++ b/httpie/models.py @@ -16,7 +16,6 @@ from .cli.constants import ( from .compat import cached_property from .utils import split_cookies, parse_content_type_header - ELAPSED_TIME_LABEL = 'Elapsed time' @@ -67,27 +66,10 @@ class HTTPResponse(HTTPMessage): 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) @@ -117,6 +99,32 @@ class HTTPResponse(HTTPMessage): 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."""