From 047160334a4949146e99c5d9f325a6d7d8e11891 Mon Sep 17 00:00:00 2001 From: Anandesh Sharma Date: Fri, 27 Feb 2026 04:11:58 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20test=20coverage=20for=20stric?= =?UTF-8?q?t=5Fcontent=5Ftype=20with=20charset=20parameters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Verify that Content-Type headers with charset parameters (e.g., application/json; charset=utf-8) are correctly accepted by the strict_content_type parsing logic, including vendor JSON media types like application/vnd.api+json; charset=utf-8. Co-Authored-By: Claude Opus 4.6 --- tests/test_strict_content_type_charset.py | 55 +++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 tests/test_strict_content_type_charset.py diff --git a/tests/test_strict_content_type_charset.py b/tests/test_strict_content_type_charset.py new file mode 100644 index 0000000000..61f74994bc --- /dev/null +++ b/tests/test_strict_content_type_charset.py @@ -0,0 +1,55 @@ +from fastapi import FastAPI +from fastapi.testclient import TestClient + +app = FastAPI() + + +@app.post("/items/") +async def create_item(data: dict): + return data + + +client = TestClient(app) + + +def test_json_content_type_with_charset_utf8(): + """Content-Type: application/json; charset=utf-8 should be accepted.""" + response = client.post( + "/items/", + content='{"key": "value"}', + headers={"Content-Type": "application/json; charset=utf-8"}, + ) + assert response.status_code == 200 + assert response.json() == {"key": "value"} + + +def test_vendor_json_content_type_with_charset_utf8(): + """Content-Type: application/vnd.api+json; charset=utf-8 should be accepted.""" + response = client.post( + "/items/", + content='{"key": "value"}', + headers={"Content-Type": "application/vnd.api+json; charset=utf-8"}, + ) + assert response.status_code == 200 + assert response.json() == {"key": "value"} + + +def test_json_content_type_with_charset_iso_8859_1(): + """Content-Type: application/json; charset=iso-8859-1 should be accepted.""" + response = client.post( + "/items/", + content='{"key": "value"}', + headers={"Content-Type": "application/json; charset=iso-8859-1"}, + ) + assert response.status_code == 200 + assert response.json() == {"key": "value"} + + +def test_text_plain_content_type_is_rejected(): + """Content-Type: text/plain should be rejected with 422.""" + response = client.post( + "/items/", + content='{"key": "value"}', + headers={"Content-Type": "text/plain"}, + ) + assert response.status_code == 422