Browse Source

Update custom_docs_ui tests instead

pull/13825/head
Robert Anthony 3 weeks ago
parent
commit
75050069ab
  1. 12
      docs_src/custom_docs_ui/tutorial002.py
  2. 0
      tests/test_offline_docs/__init__.py
  3. 3
      tests/test_offline_docs/static/redoc.js
  4. 1
      tests/test_offline_docs/static/swagger.css
  5. 3
      tests/test_offline_docs/static/swagger.js
  6. 138
      tests/test_offline_docs/test_root_path_with_static_mount.py
  7. 18
      tests/test_tutorial/test_custom_docs_ui/test_tutorial002.py

12
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",
)

0
tests/test_offline_docs/__init__.py

3
tests/test_offline_docs/static/redoc.js

@ -1,3 +0,0 @@
function bar() {
return "bar"
}

1
tests/test_offline_docs/static/swagger.css

@ -1 +0,0 @@
.swagger-ui{color:#3b4151;}

3
tests/test_offline_docs/static/swagger.js

@ -1,3 +0,0 @@
function foo() {
return "foo"
}

138
tests/test_offline_docs/test_root_path_with_static_mount.py

@ -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

18
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):

Loading…
Cancel
Save