From 448ff10b9f597366e46a8e45b2ee7a8aa7a2dc66 Mon Sep 17 00:00:00 2001 From: xu91102 <169027046+xu91102@users.noreply.github.com> Date: Sun, 27 Sep 2026 00:18:05 +0800 Subject: [PATCH] Fix case-insensitive stable sorting of response headers --- httpie/output/formatters/headers.py | 2 +- tests/test_output.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/httpie/output/formatters/headers.py b/httpie/output/formatters/headers.py index 2188193902..1b701bee51 100644 --- a/httpie/output/formatters/headers.py +++ b/httpie/output/formatters/headers.py @@ -14,5 +14,5 @@ def format_headers(self, headers: str) -> str: """ lines = headers.splitlines() - headers = sorted(lines[1:], key=lambda h: h.split(':')[0]) + headers = sorted(lines[1:], key=lambda h: h.split(':')[0].lower()) return '\r\n'.join(lines[:1] + headers) diff --git a/tests/test_output.py b/tests/test_output.py index 2242177dbc..37dae73325 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -19,6 +19,7 @@ from httpie.cli.definition import parser from httpie.encoding import UTF8 from httpie.output.formatters.colors import get_lexer, PIE_STYLE_NAMES, BUNDLED_STYLES +from httpie.output.formatters.headers import HeadersFormatter 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, strip_colors @@ -378,6 +379,23 @@ def test_CRLF_formatted_request(self, httpbin): class TestFormatOptions: + def test_header_sort_retains_order_for_mixed_case_repeated_fields(self): + formatter = HeadersFormatter(format_options=PARSED_DEFAULT_FORMAT_OPTIONS) + headers = CRLF.join([ + 'HTTP/1.1 200 OK', + 'set-cookie: first=1', + 'X-Z: last', + 'Set-Cookie: second=2', + 'accept: text/plain', + ]) + assert formatter.format_headers(headers) == CRLF.join([ + 'HTTP/1.1 200 OK', + 'accept: text/plain', + 'set-cookie: first=1', + 'Set-Cookie: second=2', + 'X-Z: last', + ]) + def test_header_formatting_options(self): def get_headers(sort): return http(