Skip to content

Commit

Permalink
Fix regression due to a985d8c (#1155)
Browse files Browse the repository at this point in the history
  • Loading branch information
masnagam authored Oct 21, 2024
1 parent 155657f commit 5762763
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
2 changes: 2 additions & 0 deletions utoipa-swagger-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }

[dev-dependencies]
axum-test = "16.2.0"
similar = "2.5"
tokio = { version = "1", features = ["macros"] }
utoipa-swagger-ui = { path = ".", features = ["actix-web", "axum", "rocket"] }

[package.metadata.docs.rs]
Expand Down
65 changes: 61 additions & 4 deletions utoipa-swagger-ui/src/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,27 @@ where
let handler = routing::get(serve_swagger_ui).layer(Extension(Arc::new(config)));
let path: &str = swagger_ui.path.as_ref();

router
.route(path, handler.clone())
.route(&format!("{}/", path), handler.clone())
.route(&format!("{}/*rest", path), handler)
if path == "/" {
router
.route(path, handler.clone())
.route(&format!("{}*rest", path), handler)
} else {
let path = if path.ends_with('/') {
&path[..path.len() - 1]
} else {
path
};
debug_assert!(!path.is_empty());

let slash_path = format!("{}/", path);
router
.route(
path,
routing::get(|| async move { axum::response::Redirect::to(&slash_path) }),
)
.route(&format!("{}/", path), handler.clone())
.route(&format!("{}/*rest", path), handler)
}
}
}

Expand Down Expand Up @@ -96,3 +113,43 @@ async fn serve_swagger_ui(
Err(error) => (StatusCode::INTERNAL_SERVER_ERROR, error.to_string()).into_response(),
}
}

#[cfg(test)]
mod tests {
use super::*;
use axum_test::TestServer;

#[tokio::test]
async fn mount_onto_root() {
let app = Router::<()>::from(SwaggerUi::new("/"));
let server = TestServer::new(app).unwrap();
let response = server.get("/").await;
response.assert_status_ok();
let response = server.get("/swagger-ui.css").await;
response.assert_status_ok();
}

#[tokio::test]
async fn mount_onto_path_ends_with_slash() {
let app = Router::<()>::from(SwaggerUi::new("/swagger-ui/"));
let server = TestServer::new(app).unwrap();
let response = server.get("/swagger-ui").await;
response.assert_status_see_other();
let response = server.get("/swagger-ui/").await;
response.assert_status_ok();
let response = server.get("/swagger-ui/swagger-ui.css").await;
response.assert_status_ok();
}

#[tokio::test]
async fn mount_onto_path_not_end_with_slash() {
let app = Router::<()>::from(SwaggerUi::new("/swagger-ui"));
let server = TestServer::new(app).unwrap();
let response = server.get("/swagger-ui").await;
response.assert_status_see_other();
let response = server.get("/swagger-ui/").await;
response.assert_status_ok();
let response = server.get("/swagger-ui/swagger-ui.css").await;
response.assert_status_ok();
}
}

0 comments on commit 5762763

Please sign in to comment.