From 25bd817bb2ea3dddbb9682766948495bb0b4efe3 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Thu, 3 Mar 2022 19:28:04 +0300 Subject: [PATCH] Fix displaying of status code without a status message. (#1301) Co-authored-by: Jakub Roztocil --- CHANGELOG.md | 2 ++ httpie/output/lexers/http.py | 2 +- tests/test_output.py | 27 +++++++++++++++++++++++++-- tests/utils/__init__.py | 6 ++++++ tests/utils/http_server.py | 16 ++++++++++++++-- 5 files changed, 48 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73186f6f..026916d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,10 @@ This project adheres to [Semantic Versioning](https://semver.org/). ## [3.0.3.dev0](https://github.com/httpie/httpie/compare/3.0.2...HEAD) (Unreleased) - Fixed escaping of integer indexes with multiple backslashes in the nested JSON builder. ([#1285](https://github.com/httpie/httpie/issues/1285)) +- Fixed displaying of status code without a status message on non-`auto` themes. ([#1300](https://github.com/httpie/httpie/issues/1300)) - Improved regulation of top-level arrays. ([#1292](https://github.com/httpie/httpie/commit/225dccb2186f14f871695b6c4e0bfbcdb2e3aa28)) + ## [3.0.2](https://github.com/httpie/httpie/compare/3.0.1...3.0.2) (2022-01-24) [What’s new in HTTPie for Terminal 3.0 →](https://httpie.io/blog/httpie-3.0.0) diff --git a/httpie/output/lexers/http.py b/httpie/output/lexers/http.py index f06a6853..aea82740 100644 --- a/httpie/output/lexers/http.py +++ b/httpie/output/lexers/http.py @@ -2,7 +2,7 @@ import re import pygments from httpie.output.lexers.common import precise -RE_STATUS_LINE = re.compile(r'(\d{3})( +)(.+)') +RE_STATUS_LINE = re.compile(r'(\d{3})( +)?(.+)?') STATUS_TYPES = { '1': pygments.token.Number.HTTP.INFO, diff --git a/tests/test_output.py b/tests/test_output.py index c68bfa9e..716ceb09 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -17,10 +17,15 @@ from httpie.cli.argtypes import ( ) from httpie.cli.definition import parser from httpie.encoding import UTF8 -from httpie.output.formatters.colors import get_lexer, PIE_STYLE_NAMES +from httpie.output.formatters.colors import get_lexer, PIE_STYLE_NAMES, BUNDLED_STYLES from httpie.status import ExitStatus from .fixtures import XML_DATA_RAW, XML_DATA_FORMATTED -from .utils import COLOR, CRLF, HTTP_OK, MockEnvironment, http, DUMMY_URL +from .utils import COLOR, CRLF, HTTP_OK, MockEnvironment, http, DUMMY_URL, strip_colors + + +# For ensuring test reproducibility, avoid using the unsorted +# BUNDLED_STYLES set. +SORTED_BUNDLED_STYLES = sorted(BUNDLED_STYLES) @pytest.mark.parametrize('stdout_isatty', [True, False]) @@ -234,6 +239,24 @@ def test_ensure_meta_is_colored(httpbin, style): assert COLOR in r +@pytest.mark.parametrize('style', SORTED_BUNDLED_STYLES) +@pytest.mark.parametrize('msg', [ + '', + ' ', + ' OK', + ' OK ', + ' CUSTOM ', +]) +def test_ensure_status_code_is_shown_on_all_themes(http_server, style, msg): + env = MockEnvironment(colors=256) + r = http('--style', style, + http_server + '/status/msg', + '--raw', msg, env=env) + + # Trailing space is stripped away. + assert 'HTTP/1.0 200' + msg.rstrip() in strip_colors(r) + + class TestPrettyOptions: """Test the --pretty handling.""" diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py index f8954565..2bd376ee 100644 --- a/tests/utils/__init__.py +++ b/tests/utils/__init__.py @@ -27,6 +27,8 @@ HTTPBIN_WITH_CHUNKED_SUPPORT = 'http://' + HTTPBIN_WITH_CHUNKED_SUPPORT_DOMAIN TESTS_ROOT = Path(__file__).parent.parent CRLF = '\r\n' COLOR = '\x1b[' +COLOR_RE = re.compile(r'\x1b\[\d+(;\d+)*?m', re.MULTILINE) + HTTP_OK = '200 OK' # noinspection GrazieInspection HTTP_OK_COLOR = ( @@ -38,6 +40,10 @@ HTTP_OK_COLOR = ( DUMMY_URL = 'http://this-should.never-resolve' # Note: URL never fetched +def strip_colors(colorized_msg: str) -> str: + return COLOR_RE.sub('', colorized_msg) + + def mk_config_dir() -> Path: dirname = tempfile.mkdtemp(prefix='httpie_config_') return Path(dirname) diff --git a/tests/utils/http_server.py b/tests/utils/http_server.py index fc8f2b07..0a96dd8b 100644 --- a/tests/utils/http_server.py +++ b/tests/utils/http_server.py @@ -18,14 +18,17 @@ class TestHandler(BaseHTTPRequestHandler): return func return inner - def do_GET(self): + def do_generic(self): parse_result = urlparse(self.path) - func = self.handlers['GET'].get(parse_result.path) + func = self.handlers[self.command].get(parse_result.path) if func is None: return self.send_error(HTTPStatus.NOT_FOUND) return func(self) + do_GET = do_generic + do_POST = do_generic + @TestHandler.handler('GET', '/headers') def get_headers(handler): @@ -73,6 +76,15 @@ def random_encoding(handler): handler.wfile.write('0\r\n\r\n'.encode('utf-8')) +@TestHandler.handler('POST', '/status/msg') +def status_custom_msg(handler): + content_len = int(handler.headers.get('content-length', 0)) + post_body = handler.rfile.read(content_len).decode() + + handler.send_response(200, post_body) + handler.end_headers() + + @pytest.fixture(scope="function") def http_server(): """A custom HTTP server implementation for our tests, that is