mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-11 17:19:22 -04:00
✨ Allow disabling docs UIs by disabling OpenAPI (#1421)
* ✨ Allow disabling docs UIs by disabling openapi_url * 📝 Add docs for disabling OpenAPI and docs in prod or other environments * ✅ Add tests for disabling OpenAPI and docs
This commit is contained in:
committed by
GitHub
parent
cfb72eec5a
commit
409264960e
@@ -0,0 +1,46 @@
|
||||
import importlib
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from conditional_openapi import tutorial001
|
||||
|
||||
openapi_schema = {
|
||||
"openapi": "3.0.2",
|
||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
||||
"paths": {
|
||||
"/": {
|
||||
"get": {
|
||||
"summary": "Root",
|
||||
"operationId": "root__get",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {"application/json": {"schema": {}}},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_default_openapi():
|
||||
client = TestClient(tutorial001.app)
|
||||
response = client.get("/openapi.json")
|
||||
assert response.json() == openapi_schema
|
||||
response = client.get("/docs")
|
||||
assert response.status_code == 200, response.text
|
||||
response = client.get("/redoc")
|
||||
assert response.status_code == 200, response.text
|
||||
|
||||
|
||||
def test_disable_openapi(monkeypatch):
|
||||
monkeypatch.setenv("OPENAPI_URL", "")
|
||||
importlib.reload(tutorial001)
|
||||
client = TestClient(tutorial001.app)
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 404, response.text
|
||||
response = client.get("/docs")
|
||||
assert response.status_code == 404, response.text
|
||||
response = client.get("/redoc")
|
||||
assert response.status_code == 404, response.text
|
||||
Reference in New Issue
Block a user