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.
45 lines
1.1 KiB
45 lines
1.1 KiB
from typing import Annotated
|
|
|
|
from fastapi import FastAPI, File, Form
|
|
from starlette.testclient import TestClient
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.post("/urlencoded")
|
|
async def post_url_encoded(age: Annotated[int | None, Form()] = None):
|
|
return age
|
|
|
|
|
|
@app.post("/multipart")
|
|
async def post_multi_part(
|
|
age: Annotated[int | None, Form()] = None,
|
|
file: Annotated[bytes | None, File()] = None,
|
|
):
|
|
return {"file": file, "age": age}
|
|
|
|
|
|
@app.post("/urlencoded-required")
|
|
async def post_url_encoded_required(name: Annotated[str, Form()]):
|
|
return name
|
|
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
def test_form_default_url_encoded():
|
|
response = client.post("/urlencoded", data={"age": ""})
|
|
assert response.status_code == 200
|
|
assert response.text == "null"
|
|
|
|
|
|
def test_form_default_multi_part():
|
|
response = client.post("/multipart", data={"age": ""})
|
|
assert response.status_code == 200
|
|
assert response.json() == {"file": None, "age": None}
|
|
|
|
|
|
def test_required_form_field_with_empty_string():
|
|
response = client.post("/urlencoded-required", data={"name": ""})
|
|
assert response.status_code == 200
|
|
assert response.text == ""
|
|
|