From 8f3d256abde334659deeb0868caf0aea63d33eb7 Mon Sep 17 00:00:00 2001 From: nietus <102884486+nietus@users.noreply.github.com> Date: Tue, 6 Oct 2026 15:39:39 -0300 Subject: [PATCH] Fix case-insensitive XML media type formatting --- CHANGELOG.md | 4 ++++ docs/README.md | 2 +- httpie/output/formatters/xml.py | 2 +- tests/test_xml.py | 17 +++++++++++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0497ac3508..d4fc6b1159 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ This document records all notable changes to [HTTPie](https://httpie.io). This project adheres to [Semantic Versioning](https://semver.org/). +## Unreleased + +- Format XML responses with uppercase or mixed-case media types. + ## [3.2.4](https://github.com/httpie/cli/compare/3.2.3...3.2.4) (2024-11-01) - Fix default certs loading and unpin `requests`. ([#1596](https://github.com/httpie/cli/issues/1596)) diff --git a/docs/README.md b/docs/README.md index 51dff51aec..668ddbe46d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1892,7 +1892,7 @@ Formatting has the following effects: - HTTP headers are sorted by name. - JSON data is indented, sorted by keys, and unicode escapes are converted to the characters they represent. -- XML and XHTML data is indented. +- XML and XHTML data is indented. XML media types are matched case-insensitively. Please note that sometimes there might be changes made by formatters on the actual response body (e.g., collapsing empty tags on XML) but the end result will always be semantically indistinguishable. Some of diff --git a/httpie/output/formatters/xml.py b/httpie/output/formatters/xml.py index 6752ab0f57..1aa1741387 100644 --- a/httpie/output/formatters/xml.py +++ b/httpie/output/formatters/xml.py @@ -57,7 +57,7 @@ def __init__(self, **kwargs): self.enabled = self.format_options['xml']['format'] def format_body(self, body: str, mime: str): - if 'xml' not in mime: + if 'xml' not in mime.lower(): return body from xml.parsers.expat import ExpatError diff --git a/tests/test_xml.py b/tests/test_xml.py index a427b266e5..53fe90decf 100644 --- a/tests/test_xml.py +++ b/tests/test_xml.py @@ -97,3 +97,20 @@ def test_invalid_xml(file): # No formatting done, data is simply printed as-is. r = http(DUMMY_URL) assert xml_data in r + + +@pytest.mark.parametrize('content_type', [ + 'APPLICATION/XML', + 'Application/Atom+XML; charset=utf-8', +]) +@responses.activate +def test_xml_media_type_is_case_insensitive(content_type): + responses.add( + responses.GET, + DUMMY_URL, + body=XML_DATA_RAW, + content_type=content_type, + ) + + r = http('--pretty=format', '--body', DUMMY_URL) + assert XML_DATA_FORMATTED in r