| 1 |
import importlib |
| 2 |
|
| 3 |
from fastapi.testclient import TestClient |
| 4 |
from inline_snapshot import snapshot |
| 5 |
|
| 6 |
|
| 7 |
def get_client() -> TestClient: |
| 8 |
from docs_src.conditional_openapi import tutorial001_py310 |
| 9 |
|
| 10 |
importlib.reload(tutorial001_py310) |
| 11 |
|
| 12 |
client = TestClient(tutorial001_py310.app) |
| 13 |
return client |
| 14 |
|
| 15 |
|
| 16 |
def test_disable_openapi(monkeypatch): |
| 17 |
monkeypatch.setenv("OPENAPI_URL", "") |
| 18 |
# Load the client after setting the env var |
| 19 |
client = get_client() |
| 20 |
response = client.get("/openapi.json") |
| 21 |
assert response.status_code == 404, response.text |
| 22 |
response = client.get("/docs") |
| 23 |
assert response.status_code == 404, response.text |
| 24 |
response = client.get("/redoc") |
| 25 |
assert response.status_code == 404, response.text |
| 26 |
|
| 27 |
|
| 28 |
def test_root(): |
| 29 |
client = get_client() |
| 30 |
response = client.get("/") |
| 31 |
assert response.status_code == 200 |
| 32 |
assert response.json() == {"message": "Hello World"} |
| 33 |
|
| 34 |
|
| 35 |
def test_default_openapi(): |
| 36 |
client = get_client() |
| 37 |
response = client.get("/docs") |
| 38 |
assert response.status_code == 200, response.text |
| 39 |
response = client.get("/redoc") |
| 40 |
assert response.status_code == 200, response.text |
| 41 |
response = client.get("/openapi.json") |
| 42 |
assert response.json() == snapshot( |
| 43 |
{ |
| 44 |
"openapi": "3.1.0", |
| 45 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 46 |
"paths": { |
| 47 |
"/": { |
| 48 |
"get": { |
| 49 |
"summary": "Root", |
| 50 |
"operationId": "root__get", |
| 51 |
"responses": { |
| 52 |
"200": { |
| 53 |
"description": "Successful Response", |
| 54 |
"content": {"application/json": {"schema": {}}}, |
| 55 |
} |
| 56 |
}, |
| 57 |
} |
| 58 |
} |
| 59 |
}, |
| 60 |
} |
| 61 |
) |