From 5e501bb4c00ca0032da0a42fa40f5bafeb6c4c6a Mon Sep 17 00:00:00 2001 From: adhoc Date: Tue, 25 Mar 2025 15:38:04 +0100 Subject: [PATCH] add tests --- tests/test_form_default.py | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 tests/test_form_default.py 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