| 1 |
from typing import Annotated |
| 2 |
|
| 3 |
from fastapi import FastAPI, Form |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from inline_snapshot import snapshot |
| 6 |
|
| 7 |
app = FastAPI() |
| 8 |
|
| 9 |
|
| 10 |
@app.post("/form/") |
| 11 |
def post_form(username: Annotated[str, Form()]): |
| 12 |
return username |
| 13 |
|
| 14 |
|
| 15 |
client = TestClient(app) |
| 16 |
|
| 17 |
|
| 18 |
def test_single_form_field(): |
| 19 |
response = client.post("/form/", data={"username": "Rick"}) |
| 20 |
assert response.status_code == 200, response.text |
| 21 |
assert response.json() == "Rick" |
| 22 |
|
| 23 |
|
| 24 |
def test_openapi_schema(): |
| 25 |
response = client.get("/openapi.json") |
| 26 |
assert response.status_code == 200, response.text |
| 27 |
assert response.json() == snapshot( |
| 28 |
{ |
| 29 |
"openapi": "3.1.0", |
| 30 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 31 |
"paths": { |
| 32 |
"/form/": { |
| 33 |
"post": { |
| 34 |
"summary": "Post Form", |
| 35 |
"operationId": "post_form_form__post", |
| 36 |
"requestBody": { |
| 37 |
"content": { |
| 38 |
"application/x-www-form-urlencoded": { |
| 39 |
"schema": { |
| 40 |
"$ref": "#/components/schemas/Body_post_form_form__post" |
| 41 |
} |
| 42 |
} |
| 43 |
}, |
| 44 |
"required": True, |
| 45 |
}, |
| 46 |
"responses": { |
| 47 |
"200": { |
| 48 |
"description": "Successful Response", |
| 49 |
"content": {"application/json": {"schema": {}}}, |
| 50 |
}, |
| 51 |
"422": { |
| 52 |
"description": "Validation Error", |
| 53 |
"content": { |
| 54 |
"application/json": { |
| 55 |
"schema": { |
| 56 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 57 |
} |
| 58 |
} |
| 59 |
}, |
| 60 |
}, |
| 61 |
}, |
| 62 |
} |
| 63 |
} |
| 64 |
}, |
| 65 |
"components": { |
| 66 |
"schemas": { |
| 67 |
"Body_post_form_form__post": { |
| 68 |
"properties": { |
| 69 |
"username": {"type": "string", "title": "Username"} |
| 70 |
}, |
| 71 |
"type": "object", |
| 72 |
"required": ["username"], |
| 73 |
"title": "Body_post_form_form__post", |
| 74 |
}, |
| 75 |
"HTTPValidationError": { |
| 76 |
"properties": { |
| 77 |
"detail": { |
| 78 |
"items": { |
| 79 |
"$ref": "#/components/schemas/ValidationError" |
| 80 |
}, |
| 81 |
"type": "array", |
| 82 |
"title": "Detail", |
| 83 |
} |
| 84 |
}, |
| 85 |
"type": "object", |
| 86 |
"title": "HTTPValidationError", |
| 87 |
}, |
| 88 |
"ValidationError": { |
| 89 |
"properties": { |
| 90 |
"ctx": {"title": "Context", "type": "object"}, |
| 91 |
"input": {"title": "Input"}, |
| 92 |
"loc": { |
| 93 |
"items": { |
| 94 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 95 |
}, |
| 96 |
"type": "array", |
| 97 |
"title": "Location", |
| 98 |
}, |
| 99 |
"msg": {"type": "string", "title": "Message"}, |
| 100 |
"type": {"type": "string", "title": "Error Type"}, |
| 101 |
}, |
| 102 |
"type": "object", |
| 103 |
"required": ["loc", "msg", "type"], |
| 104 |
"title": "ValidationError", |
| 105 |
}, |
| 106 |
} |
| 107 |
}, |
| 108 |
} |
| 109 |
) |