Skip to content
Open
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
1 change: 1 addition & 0 deletions httpie/output/formatters/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def __init__(self, **kwargs):
self.enabled = self.format_options['json']['format']

def format_body(self, body: str, mime: str) -> str:
mime = mime.lower()
maybe_json = [
'json',
'javascript',
Expand Down
38 changes: 38 additions & 0 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,41 @@ def test_response_mime_overwrite_incorrect():
# The provided Content-Type is simply ignored, and so no formatting is done.
r = http('--response-mime=incorrect/type', DUMMY_URL)
assert XML_DATA_RAW in r


class TestJSONFormatterMIME:
"""JSONFormatter must match the body MIME type case-insensitively (#1958)."""

@staticmethod
def make_formatter():
from httpie.output.formatters.json import JSONFormatter
return JSONFormatter(
format_options={'json': {'format': True, 'sort_keys': True, 'indent': 4}},
explicit_json=False,
)

@pytest.mark.parametrize(
'mime',
[
'application/json',
'APPLICATION/JSON',
'Application/Problem+JSON; charset=UTF-8',
]
)
def test_format_body_matches_mime_case_insensitively(self, mime: str):
formatted = self.make_formatter().format_body('{"a": 1}', mime)
assert '\n' in formatted
assert json.loads(formatted) == {'a': 1}

@pytest.mark.parametrize(
'mime',
[
'text/plain',
'application/xml',
]
)
def test_format_body_case_parity_for_non_json(self, mime: str):
# An upper-cased MIME type behaves exactly like its lower-case form.
body = '{"a": 1}'
formatter = self.make_formatter()
assert formatter.format_body(body, mime.upper()) == formatter.format_body(body, mime)
Loading