| 1 |
from fastapi.testclient import TestClient |
| 2 |
from inline_snapshot import snapshot |
| 3 |
|
| 4 |
from docs_src.response_model.tutorial003_02_py310 import app |
| 5 |
|
| 6 |
client = TestClient(app) |
| 7 |
|
| 8 |
|
| 9 |
def test_get_portal(): |
| 10 |
response = client.get("/portal") |
| 11 |
assert response.status_code == 200, response.text |
| 12 |
assert response.json() == {"message": "Here's your interdimensional portal."} |
| 13 |
|
| 14 |
|
| 15 |
def test_get_redirect(): |
| 16 |
response = client.get("/portal", params={"teleport": True}, follow_redirects=False) |
| 17 |
assert response.status_code == 307, response.text |
| 18 |
assert response.headers["location"] == "https://www.youtube.com/watch?v=dQw4w9WgXcQ" |
| 19 |
|
| 20 |
|
| 21 |
def test_openapi_schema(): |
| 22 |
response = client.get("/openapi.json") |
| 23 |
assert response.status_code == 200, response.text |
| 24 |
assert response.json() == snapshot( |
| 25 |
{ |
| 26 |
"openapi": "3.1.0", |
| 27 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 28 |
"paths": { |
| 29 |
"/portal": { |
| 30 |
"get": { |
| 31 |
"summary": "Get Portal", |
| 32 |
"operationId": "get_portal_portal_get", |
| 33 |
"parameters": [ |
| 34 |
{ |
| 35 |
"required": False, |
| 36 |
"schema": { |
| 37 |
"title": "Teleport", |
| 38 |
"type": "boolean", |
| 39 |
"default": False, |
| 40 |
}, |
| 41 |
"name": "teleport", |
| 42 |
"in": "query", |
| 43 |
} |
| 44 |
], |
| 45 |
"responses": { |
| 46 |
"200": { |
| 47 |
"description": "Successful Response", |
| 48 |
"content": {"application/json": {"schema": {}}}, |
| 49 |
}, |
| 50 |
"422": { |
| 51 |
"description": "Validation Error", |
| 52 |
"content": { |
| 53 |
"application/json": { |
| 54 |
"schema": { |
| 55 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 56 |
} |
| 57 |
} |
| 58 |
}, |
| 59 |
}, |
| 60 |
}, |
| 61 |
} |
| 62 |
} |
| 63 |
}, |
| 64 |
"components": { |
| 65 |
"schemas": { |
| 66 |
"HTTPValidationError": { |
| 67 |
"title": "HTTPValidationError", |
| 68 |
"type": "object", |
| 69 |
"properties": { |
| 70 |
"detail": { |
| 71 |
"title": "Detail", |
| 72 |
"type": "array", |
| 73 |
"items": { |
| 74 |
"$ref": "#/components/schemas/ValidationError" |
| 75 |
}, |
| 76 |
} |
| 77 |
}, |
| 78 |
}, |
| 79 |
"ValidationError": { |
| 80 |
"title": "ValidationError", |
| 81 |
"required": ["loc", "msg", "type"], |
| 82 |
"type": "object", |
| 83 |
"properties": { |
| 84 |
"loc": { |
| 85 |
"title": "Location", |
| 86 |
"type": "array", |
| 87 |
"items": { |
| 88 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 89 |
}, |
| 90 |
}, |
| 91 |
"msg": {"title": "Message", "type": "string"}, |
| 92 |
"type": {"title": "Error Type", "type": "string"}, |
| 93 |
"input": {"title": "Input"}, |
| 94 |
"ctx": {"title": "Context", "type": "object"}, |
| 95 |
}, |
| 96 |
}, |
| 97 |
} |
| 98 |
}, |
| 99 |
} |
| 100 |
) |