From c4a3b9dbdf4ed1749439502c8673caa946422e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Sun, 31 Oct 2021 08:23:00 +0200 Subject: [PATCH] Make JSON media type matching case insensitive Per RFC 2045, section 2 and appendix A. --- CHANGES/6181.bugfix | 1 + aiohttp/helpers.py | 2 +- tests/test_helpers.py | 9 +++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 CHANGES/6181.bugfix 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"