Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for self-hosted docs behind a proxy #11253

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Empty file.
1 change: 1 addition & 0 deletions tests/test_offline_docs/static/swagger.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.swagger-ui{color:#3b4151;}
3 changes: 3 additions & 0 deletions tests/test_offline_docs/static/swagger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function foo() {
return "foo"
}
58 changes: 58 additions & 0 deletions tests/test_offline_docs/test_root_path_with_static_mount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from fastapi import FastAPI
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.staticfiles import StaticFiles
from fastapi.testclient import TestClient

root_path = "/api"

app = FastAPI(
title="FastAPI",
root_path=root_path,
docs_url=None,
redoc_url=None,
)

app.mount(
"/static", StaticFiles(directory="tests/test_offline_docs/static"), name="static"
)


@app.get("/")
async def custom_swagger_ui_html():
"""
Sets up a localized version of the Swagger (OpenAPI) docs that can be run without assets from the Internet.
"""

return get_swagger_ui_html(
openapi_url="/openapi.json",
title=app.title,
swagger_js_url="/static/swagger.js",
swagger_css_url="/static/swagger.css",
)


client = TestClient(app)


def test_static_assets():
"""
Verify static assets can still be loaded properly even behind a proxy (root_path)
"""
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("/static/swagger.js")
assert response.status_code == 200

response = client.get("/static/swagger.css")
assert response.status_code == 200

assert "/static/swagger.js" in swagger_html
assert f"{root_path}/static/swagger.js" not in swagger_html
assert "/static/swagger.css" in swagger_html
assert f"{root_path}/static/swagger.css" not in swagger_html