diff --git a/CHANGES/6181.bugfix b/CHANGES/6181.bugfix new file mode 100644 index 00000000000..70696784fa7 --- /dev/null +++ b/CHANGES/6181.bugfix @@ -0,0 +1 @@ +Make JSON media type matching case insensitive per RFC 2045. diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 3cb94fd8902..92c0700ac06 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -124,7 +124,7 @@ def iscoroutinefunction(func: Any) -> bool: return asyncio.iscoroutinefunction(func) -json_re = re.compile(r"(?:application/|[\w.-]+/[\w.+-]+?\+)json") +json_re = re.compile(r"(?:application/|[\w.-]+/[\w.+-]+?\+)json", re.IGNORECASE) class BasicAuth(namedtuple("BasicAuth", ["login", "password", "encoding"])): diff --git a/tests/test_helpers.py b/tests/test_helpers.py index dae39015b9a..33c1d292545 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -747,6 +747,15 @@ def test_is_expected_content_type_non_application_json_private_suffix(): ) +def test_is_expected_content_type_json_non_lowercase(): + """Per RFC 2045, media type matching is case insensitive.""" + expected_ct = "application/json" + response_ct = "Application/JSON" + assert is_expected_content_type( + response_content_type=response_ct, expected_content_type=expected_ct + ) + + def test_is_expected_content_type_non_json_match_exact(): expected_ct = "text/javascript" response_ct = "text/javascript"