use axum::http::header::CONTENT_SECURITY_POLICY;
use axum::response::{Html, IntoResponse, Response};
pub const SWAGGER_HTML: &str = r##"<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Delonix Seiva — API reference (Swagger UI)</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui-bundle.js"></script>
<script>
window.onload = function () {
window.ui = SwaggerUIBundle({
url: "/openapi.json",
dom_id: "#swagger-ui",
deepLinking: true,
tryItOutEnabled: true,
});
};
</script>
</body>
</html>
"##;
pub const DOCS_HTML: &str = SWAGGER_HTML;
pub const REDOC_HTML: &str = r#"<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Delonix Seiva — API reference (ReDoc)</title>
</head>
<body>
<redoc spec-url="/openapi.json"></redoc>
<script src="https://cdn.jsdelivr.net/npm/redoc/bundles/redoc.standalone.js"></script>
</body>
</html>
"#;
pub const SCALAR_HTML: &str = r#"<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Delonix Seiva — API reference (Scalar)</title>
</head>
<body>
<script id="api-reference" data-url="/openapi.json"></script>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
</body>
</html>
"#;
pub const DOCS_CSP: &str = "default-src 'none'; \
script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; \
style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; \
img-src 'self' data: https://cdn.jsdelivr.net; \
font-src 'self' data: https://cdn.jsdelivr.net; \
connect-src 'self' https://cdn.jsdelivr.net; \
worker-src 'self' blob:; \
child-src 'self' blob:; \
frame-ancestors 'none'";
pub async fn docs_handler() -> Response {
([(CONTENT_SECURITY_POLICY, DOCS_CSP)], Html(SWAGGER_HTML)).into_response()
}
pub async fn redoc_handler() -> Response {
([(CONTENT_SECURITY_POLICY, DOCS_CSP)], Html(REDOC_HTML)).into_response()
}
pub async fn scalar_handler() -> Response {
([(CONTENT_SECURITY_POLICY, DOCS_CSP)], Html(SCALAR_HTML)).into_response()
}
#[cfg(test)]
mod tests {
use crate::{router, Health};
use axum::body::Body;
use axum::http::{Request, StatusCode};
use tower::ServiceExt;
async fn fetch(uri: &str) -> (StatusCode, String, String) {
let app = router(Health::starting());
let res = app
.oneshot(Request::builder().uri(uri).body(Body::empty()).unwrap())
.await
.unwrap();
let status = res.status();
let ctype = res
.headers()
.get("content-type")
.map(|v| v.to_str().unwrap().to_string())
.unwrap_or_default();
let csp = res
.headers()
.get("content-security-policy")
.map(|v| v.to_str().unwrap().to_string())
.unwrap_or_default();
let bytes = axum::body::to_bytes(res.into_body(), usize::MAX)
.await
.unwrap();
let body = String::from_utf8(bytes.to_vec()).unwrap();
assert_eq!(status, StatusCode::OK, "{uri} should be 200");
assert!(ctype.starts_with("text/html"), "{uri} should be html");
assert!(
body.contains("/openapi.json"),
"{uri} must point at the spec"
);
(status, body, csp)
}
#[tokio::test]
async fn docs_serves_swagger_ui() {
let (_s, body, _csp) = fetch("/docs").await;
assert!(
body.contains("swagger-ui") && body.contains("SwaggerUIBundle"),
"/docs must render Swagger UI (FastAPI parity)"
);
}
#[tokio::test]
async fn redoc_serves_redoc() {
let (_s, body, _csp) = fetch("/redoc").await;
assert!(body.contains("redoc"), "/redoc must render ReDoc");
}
#[tokio::test]
async fn scalar_serves_scalar() {
let (_s, body, _csp) = fetch("/scalar").await;
assert!(
body.contains("@scalar/api-reference"),
"/scalar must render Scalar"
);
}
#[tokio::test]
async fn docs_pages_carry_their_own_csp_not_the_blank_default() {
for uri in ["/docs", "/redoc", "/scalar"] {
let (_s, _body, csp) = fetch(uri).await;
assert!(
csp.contains("https://cdn.jsdelivr.net"),
"the {uri} CSP must allow the viewer CDN, got: {csp}"
);
assert_ne!(
csp, "default-src 'none'; frame-ancestors 'none'",
"the {uri} page must not inherit the API's blank-page CSP"
);
}
}
}