diff --git a/tests/test_form_default.py b/tests/test_form_default.py new file mode 100644 index 000000000..1c42bb401 --- /dev/null +++ b/tests/test_form_default.py @@ -0,0 +1,46 @@ +from typing import Annotated, Optional + +from fastapi import FastAPI, File, Form +from starlette.testclient import TestClient + +app = FastAPI() +client = TestClient(app) + + +@app.post("/urlencoded") +async def post_url_encoded(age: Annotated[Optional[int], Form()] = None): + return age + + +def test_form_default_url_encoded(): + response = client.post("/urlencoded", data={"age": ""}) + assert response.status_code == 200 + assert response.text == "null" + + +@app.post("/multipart") +async def post_multi_part( + age: Annotated[Optional[int], Form()] = None, + file: Annotated[Optional[bytes], File()] = None, +): + assert file is None + assert age is None + + +def test_form_default_multi_part(): + response = client.post("/multipart", data={"age": ""}) + assert response.status_code == 200 + + +@app.post("/multipart-file-first") +async def post_multi_part_file_first( + file: Annotated[Optional[bytes], File()] = None, + age: Annotated[Optional[int], Form()] = None, +): + assert file is None + assert age is None + + +def test_form_default_multi_part_file_first(): + response = client.post("/multipart-file-first", data={"age": ""}) + assert response.status_code == 200