Browse Source
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
1 changed files with 55 additions and 0 deletions
@ -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…
Reference in new issue