From 1b4547dc2d5512c13194f0bb8e9c9bdc198996f3 Mon Sep 17 00:00:00 2001 From: Charan Rathore <180254320+charan-rathore@users.noreply.github.com> Date: Sun, 18 Oct 2026 11:57:53 +0530 Subject: [PATCH] Fix header sorting to be case-insensitive HeadersFormatter sorted header lines with a case-sensitive key, so uppercase names sorted before lowercase ones instead of following alphabetical order. Compare case-insensitively while keeping the original text untouched. Fixes #1960. Signed-off-by: Charan Rathore <180254320+charan-rathore@users.noreply.github.com> --- httpie/output/formatters/headers.py | 2 +- tests/test_output.py | 8 ++++++++ 2 files changed, 9 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..2adf92812e 100644 --- a/tests/test_output.py +++ b/tests/test_output.py @@ -392,6 +392,14 @@ def get_headers(sort): assert f'XXX: foo{CRLF}ZZZ: foo' in r_sorted assert f'ZZZ: foo{CRLF}XXX: foo' in r_unsorted + def test_header_sorting_is_case_insensitive(self): + r = http( + '--offline', '--print=H', + '--format-options', 'headers.sort:true', + 'example.org', 'Zebra:foo', 'apple:foo', + ) + assert r.index('apple: foo') < r.index('Zebra: foo') + @pytest.mark.parametrize( 'options, expected_json', [