pythonasyncioapiasyncfastapiframeworkjsonjson-schemaopenapiopenapi3pydanticpython-typespython3redocreststarletteswaggerswagger-uiuvicornweb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.2 KiB
47 lines
1.2 KiB
from typing import Optional
|
|
|
|
from fastapi import FastAPI, File, Form
|
|
from starlette.testclient import TestClient
|
|
from typing_extensions import Annotated
|
|
|
|
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
|
|
|