Fix displaying of status code without a status message. (#1301)

Co-authored-by: Jakub Roztocil <jakub@roztocil.co>
This commit is contained in:
Batuhan Taskaya 2022-03-03 19:28:04 +03:00 committed by GitHub
parent 6f77e144e4
commit 25bd817bb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 5 deletions

View File

@ -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)
[Whats new in HTTPie for Terminal 3.0 →](https://httpie.io/blog/httpie-3.0.0)

View File

@ -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,

View File

@ -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."""

View File

@ -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)

View File

@ -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