| 1 |
from fastapi import FastAPI |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
from inline_snapshot import snapshot |
| 4 |
|
| 5 |
app = FastAPI( |
| 6 |
servers=[ |
| 7 |
{"url": "/", "description": "Default, relative server"}, |
| 8 |
{ |
| 9 |
"url": "http://staging.localhost.tiangolo.com:8000", |
| 10 |
"description": "Staging but actually localhost still", |
| 11 |
}, |
| 12 |
{"url": "https://prod.example.com"}, |
| 13 |
] |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
@app.get("/foo") |
| 18 |
def foo(): |
| 19 |
return {"message": "Hello World"} |
| 20 |
|
| 21 |
|
| 22 |
client = TestClient(app) |
| 23 |
|
| 24 |
|
| 25 |
def test_app(): |
| 26 |
response = client.get("/foo") |
| 27 |
assert response.status_code == 200, response.text |
| 28 |
|
| 29 |
|
| 30 |
def test_openapi_schema(): |
| 31 |
response = client.get("/openapi.json") |
| 32 |
assert response.status_code == 200, response.text |
| 33 |
assert response.json() == snapshot( |
| 34 |
{ |
| 35 |
"openapi": "3.1.0", |
| 36 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 37 |
"servers": [ |
| 38 |
{"url": "/", "description": "Default, relative server"}, |
| 39 |
{ |
| 40 |
"url": "http://staging.localhost.tiangolo.com:8000", |
| 41 |
"description": "Staging but actually localhost still", |
| 42 |
}, |
| 43 |
{"url": "https://prod.example.com"}, |
| 44 |
], |
| 45 |
"paths": { |
| 46 |
"/foo": { |
| 47 |
"get": { |
| 48 |
"summary": "Foo", |
| 49 |
"operationId": "foo_foo_get", |
| 50 |
"responses": { |
| 51 |
"200": { |
| 52 |
"description": "Successful Response", |
| 53 |
"content": {"application/json": {"schema": {}}}, |
| 54 |
} |
| 55 |
}, |
| 56 |
} |
| 57 |
} |
| 58 |
}, |
| 59 |
} |
| 60 |
) |