From 75050069ab2187cb7beff537eb65e23178160ccf Mon Sep 17 00:00:00 2001 From: Robert Anthony Date: Tue, 24 Jun 2025 09:11:09 -0400 Subject: [PATCH] Update custom_docs_ui tests instead --- docs_src/custom_docs_ui/tutorial002.py | 12 +- tests/test_offline_docs/__init__.py | 0 tests/test_offline_docs/static/redoc.js | 3 - tests/test_offline_docs/static/swagger.css | 1 - tests/test_offline_docs/static/swagger.js | 3 - .../test_root_path_with_static_mount.py | 138 ------------------ .../test_custom_docs_ui/test_tutorial002.py | 18 ++- 7 files changed, 23 insertions(+), 152 deletions(-) delete mode 100644 tests/test_offline_docs/__init__.py delete mode 100644 tests/test_offline_docs/static/redoc.js delete mode 100644 tests/test_offline_docs/static/swagger.css delete mode 100644 tests/test_offline_docs/static/swagger.js delete mode 100644 tests/test_offline_docs/test_root_path_with_static_mount.py diff --git a/docs_src/custom_docs_ui/tutorial002.py b/docs_src/custom_docs_ui/tutorial002.py index 953098dd7..91df12710 100644 --- a/docs_src/custom_docs_ui/tutorial002.py +++ b/docs_src/custom_docs_ui/tutorial002.py @@ -1,3 +1,5 @@ +import os + from fastapi import FastAPI, Request from fastapi.openapi.docs import ( get_redoc_html, @@ -6,7 +8,9 @@ from fastapi.openapi.docs import ( ) from fastapi.staticfiles import StaticFiles -app = FastAPI(docs_url=None, redoc_url=None) +root_path = os.getenv("ROOT_PATH", "") + +app = FastAPI(docs_url=None, redoc_url=None, root_path=root_path) app.mount("/static", StaticFiles(directory="static"), name="static") @@ -15,9 +19,9 @@ app.mount("/static", StaticFiles(directory="static"), name="static") async def custom_swagger_ui_html(req: Request): root_path = req.scope.get("root_path", "").rstrip("/") return get_swagger_ui_html( - openapi_url=f"{root_path}/{app.openapi_url}", + openapi_url=f"{root_path}{app.openapi_url}", title=app.title + " - Swagger UI", - oauth2_redirect_url=f"{root_path}/{app.swagger_ui_oauth2_redirect_url}", + oauth2_redirect_url=f"{root_path}{app.swagger_ui_oauth2_redirect_url}", swagger_js_url=f"{root_path}/static/swagger-ui-bundle.js", swagger_css_url=f"{root_path}/static/swagger-ui.css", ) @@ -32,7 +36,7 @@ async def swagger_ui_redirect(): async def redoc_html(req: Request): root_path = req.scope.get("root_path", "").rstrip("/") return get_redoc_html( - openapi_url=f"{root_path}/{app.openapi_url}", + openapi_url=f"{root_path}{app.openapi_url}", title=app.title + " - ReDoc", redoc_js_url=f"{root_path}/static/redoc.standalone.js", ) diff --git a/tests/test_offline_docs/__init__.py b/tests/test_offline_docs/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/test_offline_docs/static/redoc.js b/tests/test_offline_docs/static/redoc.js deleted file mode 100644 index faba2abd5..000000000 --- a/tests/test_offline_docs/static/redoc.js +++ /dev/null @@ -1,3 +0,0 @@ -function bar() { - return "bar" -} diff --git a/tests/test_offline_docs/static/swagger.css b/tests/test_offline_docs/static/swagger.css deleted file mode 100644 index 310063de6..000000000 --- a/tests/test_offline_docs/static/swagger.css +++ /dev/null @@ -1 +0,0 @@ -.swagger-ui{color:#3b4151;} diff --git a/tests/test_offline_docs/static/swagger.js b/tests/test_offline_docs/static/swagger.js deleted file mode 100644 index 23c5b08a4..000000000 --- a/tests/test_offline_docs/static/swagger.js +++ /dev/null @@ -1,3 +0,0 @@ -function foo() { - return "foo" -} diff --git a/tests/test_offline_docs/test_root_path_with_static_mount.py b/tests/test_offline_docs/test_root_path_with_static_mount.py deleted file mode 100644 index 2def4e112..000000000 --- a/tests/test_offline_docs/test_root_path_with_static_mount.py +++ /dev/null @@ -1,138 +0,0 @@ -import pytest -from fastapi import FastAPI, Request -from fastapi.openapi.docs import get_redoc_html, get_swagger_ui_html -from fastapi.staticfiles import StaticFiles -from fastapi.testclient import TestClient - - -@pytest.mark.parametrize( - ["root_path", "using_test_client"], - [ - ("/api", True), - ("/api", False), - ("", True), - ("", False), - ], -) -def test_swagger_docs_with_static_assets( - root_path: str, - using_test_client: bool, -): - app_kwargs = {} - client_kwargs = {} - if not using_test_client: - app_kwargs = {"root_path": root_path} - if using_test_client: - client_kwargs = {"root_path": root_path} - - app = FastAPI( - title="FastAPI", - docs_url=None, - redoc_url=None, - **app_kwargs, - ) - - app.mount( - "/static", - StaticFiles(directory="tests/test_offline_docs/static"), - name="static", - ) - - @app.get("/") - async def custom_swagger_ui_html(req: Request): - """ - Sets up a localized version of the Swagger (OpenAPI) docs that can be run without assets from the Internet. - """ - root_path = req.scope.get("root_path", "").rstrip("/") - return get_swagger_ui_html( - openapi_url=f"{root_path}/openapi.json", - title=app.title, - swagger_js_url=f"{root_path}/static/swagger.js", - swagger_css_url=f"{root_path}/static/swagger.css", - ) - - client = TestClient(app, **client_kwargs) - - response = client.get("/") - assert response.status_code == 200 - assert response.headers["Content-Type"] == "text/html; charset=utf-8" - swagger_html = response.text - - response = client.get("/openapi.json") - assert response.status_code == 200 - - response = client.get(f"{root_path}/openapi.json") - assert response.status_code == 200 - - response = client.get(f"{root_path}/static/swagger.js") - assert response.status_code == 200 - - response = client.get(f"{root_path}/static/swagger.css") - assert response.status_code == 200 - - assert f"{root_path}/static/swagger.js" in swagger_html - assert f"{root_path}/static/swagger.css" in swagger_html - - -@pytest.mark.parametrize( - ["root_path", "using_test_client"], - [ - ("/api", True), - ("/api", False), - ("", True), - ("", False), - ], -) -def test_redoc_docs_with_static_assets( - root_path: str, - using_test_client: bool, -): - app_kwargs = {} - client_kwargs = {} - if not using_test_client: - app_kwargs = {"root_path": root_path} - if using_test_client: - client_kwargs = {"root_path": root_path} - - app = FastAPI( - title="FastAPI", - docs_url=None, - redoc_url=None, - **app_kwargs, - ) - - app.mount( - "/static", - StaticFiles(directory="tests/test_offline_docs/static"), - name="static", - ) - - @app.get("/") - async def custom_redoc_html(req: Request): - """ - Sets up a localized version of the Redoc docs that can be run without assets from the Internet. - """ - root_path = req.scope.get("root_path", "").rstrip("/") - return get_redoc_html( - openapi_url=f"{root_path}/openapi.json", - title=app.title, - redoc_js_url=f"{root_path}/static/redoc.js", - ) - - client = TestClient(app, **client_kwargs) - - response = client.get("/") - assert response.status_code == 200 - assert response.headers["Content-Type"] == "text/html; charset=utf-8" - redoc_html = response.text - - response = client.get("/openapi.json") - assert response.status_code == 200 - - response = client.get(f"{root_path}/openapi.json") - assert response.status_code == 200 - - response = client.get(f"{root_path}/static/redoc.js") - assert response.status_code == 200 - - assert f"{root_path}/static/redoc.js" in redoc_html diff --git a/tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py b/tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py index 712618807..19551173c 100644 --- a/tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py +++ b/tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py @@ -4,24 +4,33 @@ from pathlib import Path import pytest from fastapi.testclient import TestClient +root_path = "/api" + @pytest.fixture(scope="module") def client(): static_dir: Path = Path(os.getcwd()) / "static" print(static_dir) static_dir.mkdir(exist_ok=True) + os.environ["ROOT_PATH"] = root_path from docs_src.custom_docs_ui.tutorial002 import app with TestClient(app) as client: yield client + + os.environ.pop("ROOT_PATH", None) static_dir.rmdir() def test_swagger_ui_html(client: TestClient): response = client.get("/docs") assert response.status_code == 200, response.text - assert "/static/swagger-ui-bundle.js" in response.text - assert "/static/swagger-ui.css" in response.text + assert f"{root_path}/static/swagger-ui-bundle.js" in response.text + assert f"{root_path}/static/swagger-ui.css" in response.text + assert f"{root_path}/docs/oauth2-redirect" in response.text + + response = client.get(f"{root_path}/openapi.json") + assert response.status_code == 200 def test_swagger_ui_oauth2_redirect_html(client: TestClient): @@ -33,7 +42,10 @@ def test_swagger_ui_oauth2_redirect_html(client: TestClient): def test_redoc_html(client: TestClient): response = client.get("/redoc") assert response.status_code == 200, response.text - assert "/static/redoc.standalone.js" in response.text + assert f"{root_path}/static/redoc.standalone.js" in response.text + + response = client.get(f"{root_path}/openapi.json") + assert response.status_code == 200 def test_api(client: TestClient):