Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion httpie/output/lexers/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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,
Expand Down
27 changes: 25 additions & 2 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
)
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])
Expand Down Expand Up @@ -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."""

Expand Down
6 changes: 6 additions & 0 deletions tests/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
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 = (
Expand All @@ -38,6 +40,10 @@
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)
Expand Down
16 changes: 14 additions & 2 deletions tests/utils/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ def inner(func):
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):
Expand Down Expand Up @@ -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
Expand Down