Browse Source

Add test coverage for strict_content_type with charset parameters

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 <[email protected]>
pull/15012/head
Anandesh Sharma 5 months ago
parent
commit
047160334a
  1. 55
      tests/test_strict_content_type_charset.py

55
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
Loading…
Cancel
Save