Coverage for tests / test_form_default.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-04-06 01:24 +0000

1from typing import Annotated 1abcd

2 

3from fastapi import FastAPI, File, Form 1abcd

4from starlette.testclient import TestClient 1abcd

5 

6app = FastAPI() 1abcd

7 

8 

9@app.post("/urlencoded") 1abcd

10async def post_url_encoded(age: Annotated[int | None, Form()] = None): 1abcd

11 return age 1efgh

12 

13 

14@app.post("/multipart") 1abcd

15async def post_multi_part( 1abcd

16 age: Annotated[int | None, Form()] = None, 

17 file: Annotated[bytes | None, File()] = None, 

18): 

19 return {"file": file, "age": age} 1ijkl

20 

21 

22client = TestClient(app) 1abcd

23 

24 

25def test_form_default_url_encoded(): 1abcd

26 response = client.post("/urlencoded", data={"age": ""}) 1efgh

27 assert response.status_code == 200 1efgh

28 assert response.text == "null" 1efgh

29 

30 

31def test_form_default_multi_part(): 1abcd

32 response = client.post("/multipart", data={"age": ""}) 1ijkl

33 assert response.status_code == 200 1ijkl

34 assert response.json() == {"file": None, "age": None} 1ijkl