| 1 |
from fastapi import FastAPI, Request |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
from inline_snapshot import snapshot |
| 4 |
|
| 5 |
app = FastAPI(openapi_prefix="/api/v1") |
| 6 |
|
| 7 |
|
| 8 |
@app.get("/app") |
| 9 |
def read_main(request: Request): |
| 10 |
return {"message": "Hello World", "root_path": request.scope.get("root_path")} |
| 11 |
|
| 12 |
|
| 13 |
client = TestClient(app) |
| 14 |
|
| 15 |
|
| 16 |
def test_main(): |
| 17 |
response = client.get("/app") |
| 18 |
assert response.status_code == 200 |
| 19 |
assert response.json() == {"message": "Hello World", "root_path": "/api/v1"} |
| 20 |
|
| 21 |
|
| 22 |
def test_openapi(): |
| 23 |
response = client.get("/openapi.json") |
| 24 |
assert response.status_code == 200 |
| 25 |
assert response.json() == snapshot( |
| 26 |
{ |
| 27 |
"openapi": "3.1.0", |
| 28 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 29 |
"paths": { |
| 30 |
"/app": { |
| 31 |
"get": { |
| 32 |
"summary": "Read Main", |
| 33 |
"operationId": "read_main_app_get", |
| 34 |
"responses": { |
| 35 |
"200": { |
| 36 |
"description": "Successful Response", |
| 37 |
"content": {"application/json": {"schema": {}}}, |
| 38 |
} |
| 39 |
}, |
| 40 |
} |
| 41 |
} |
| 42 |
}, |
| 43 |
"servers": [{"url": "/api/v1"}], |
| 44 |
} |
| 45 |
) |