| 1 |
from typing import Annotated |
| 2 |
|
| 3 |
from fastapi import FastAPI, Form |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from pydantic import BaseModel, Field |
| 6 |
|
| 7 |
app = FastAPI() |
| 8 |
|
| 9 |
|
| 10 |
class FormModel(BaseModel): |
| 11 |
username: str |
| 12 |
lastname: str |
| 13 |
age: int | None = None |
| 14 |
tags: list[str] = ["foo", "bar"] |
| 15 |
alias_with: str = Field(alias="with", default="nothing") |
| 16 |
|
| 17 |
|
| 18 |
class FormModelExtraAllow(BaseModel): |
| 19 |
param: str |
| 20 |
|
| 21 |
model_config = {"extra": "allow"} |
| 22 |
|
| 23 |
|
| 24 |
@app.post("/form/") |
| 25 |
def post_form(user: Annotated[FormModel, Form()]): |
| 26 |
return user |
| 27 |
|
| 28 |
|
| 29 |
@app.post("/form-extra-allow/") |
| 30 |
def post_form_extra_allow(params: Annotated[FormModelExtraAllow, Form()]): |
| 31 |
return params |
| 32 |
|
| 33 |
|
| 34 |
client = TestClient(app) |
| 35 |
|
| 36 |
|
| 37 |
def test_send_all_data(): |
| 38 |
response = client.post( |
| 39 |
"/form/", |
| 40 |
data={ |
| 41 |
"username": "Rick", |
| 42 |
"lastname": "Sanchez", |
| 43 |
"age": "70", |
| 44 |
"tags": ["plumbus", "citadel"], |
| 45 |
"with": "something", |
| 46 |
}, |
| 47 |
) |
| 48 |
assert response.status_code == 200, response.text |
| 49 |
assert response.json() == { |
| 50 |
"username": "Rick", |
| 51 |
"lastname": "Sanchez", |
| 52 |
"age": 70, |
| 53 |
"tags": ["plumbus", "citadel"], |
| 54 |
"with": "something", |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
def test_defaults(): |
| 59 |
response = client.post("/form/", data={"username": "Rick", "lastname": "Sanchez"}) |
| 60 |
assert response.status_code == 200, response.text |
| 61 |
assert response.json() == { |
| 62 |
"username": "Rick", |
| 63 |
"lastname": "Sanchez", |
| 64 |
"age": None, |
| 65 |
"tags": ["foo", "bar"], |
| 66 |
"with": "nothing", |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
def test_invalid_data(): |
| 71 |
response = client.post( |
| 72 |
"/form/", |
| 73 |
data={ |
| 74 |
"username": "Rick", |
| 75 |
"lastname": "Sanchez", |
| 76 |
"age": "seventy", |
| 77 |
"tags": ["plumbus", "citadel"], |
| 78 |
}, |
| 79 |
) |
| 80 |
assert response.status_code == 422, response.text |
| 81 |
assert response.json() == { |
| 82 |
"detail": [ |
| 83 |
{ |
| 84 |
"type": "int_parsing", |
| 85 |
"loc": ["body", "age"], |
| 86 |
"msg": "Input should be a valid integer, unable to parse string as an integer", |
| 87 |
"input": "seventy", |
| 88 |
} |
| 89 |
] |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
def test_no_data(): |
| 94 |
response = client.post("/form/") |
| 95 |
assert response.status_code == 422, response.text |
| 96 |
assert response.json() == { |
| 97 |
"detail": [ |
| 98 |
{ |
| 99 |
"type": "missing", |
| 100 |
"loc": ["body", "username"], |
| 101 |
"msg": "Field required", |
| 102 |
"input": {"tags": ["foo", "bar"], "with": "nothing"}, |
| 103 |
}, |
| 104 |
{ |
| 105 |
"type": "missing", |
| 106 |
"loc": ["body", "lastname"], |
| 107 |
"msg": "Field required", |
| 108 |
"input": {"tags": ["foo", "bar"], "with": "nothing"}, |
| 109 |
}, |
| 110 |
] |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
def test_extra_param_single(): |
| 115 |
response = client.post( |
| 116 |
"/form-extra-allow/", |
| 117 |
data={ |
| 118 |
"param": "123", |
| 119 |
"extra_param": "456", |
| 120 |
}, |
| 121 |
) |
| 122 |
assert response.status_code == 200, response.text |
| 123 |
assert response.json() == { |
| 124 |
"param": "123", |
| 125 |
"extra_param": "456", |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
def test_extra_param_list(): |
| 130 |
response = client.post( |
| 131 |
"/form-extra-allow/", |
| 132 |
data={ |
| 133 |
"param": "123", |
| 134 |
"extra_params": ["456", "789"], |
| 135 |
}, |
| 136 |
) |
| 137 |
assert response.status_code == 200, response.text |
| 138 |
assert response.json() == { |
| 139 |
"param": "123", |
| 140 |
"extra_params": ["456", "789"], |
| 141 |
} |