diff --git a/axum-extra/Cargo.toml b/axum-extra/Cargo.toml index aed676d1..4b02d023 100644 --- a/axum-extra/Cargo.toml +++ b/axum-extra/Cargo.toml @@ -39,7 +39,7 @@ typed-header = ["dep:headers"] typed-routing = ["dep:axum-macros", "dep:percent-encoding", "dep:serde_html_form", "dep:form_urlencoded"] [dependencies] -axum = { path = "../axum", version = "0.7.7", default-features = false } +axum = { path = "../axum", version = "0.7.7", default-features = false, features = ["original-uri"] } axum-core = { path = "../axum-core", version = "0.4.5" } bytes = "1.1.0" futures-util = { version = "0.3", default-features = false, features = ["alloc"] } diff --git a/axum-extra/src/routing/mod.rs b/axum-extra/src/routing/mod.rs index 5fce9591..445c3a2b 100644 --- a/axum-extra/src/routing/mod.rs +++ b/axum-extra/src/routing/mod.rs @@ -1,7 +1,7 @@ //! Additional types for defining routes. use axum::{ - extract::Request, + extract::{OriginalUri, Request}, response::{IntoResponse, Redirect, Response}, routing::{any, MethodRouter}, Router, @@ -313,7 +313,7 @@ fn add_tsr_redirect_route(router: Router, path: &str) -> Router where S: Clone + Send + Sync + 'static, { - async fn redirect_handler(uri: Uri) -> Response { + async fn redirect_handler(OriginalUri(uri): OriginalUri) -> Response { let new_uri = map_path(uri, |path| { path.strip_suffix('/') .map(Cow::Borrowed) @@ -432,6 +432,22 @@ mod tests { assert_eq!(res.headers()["location"], "/foo?a=a"); } + #[tokio::test] + async fn tsr_works_in_nested_router() { + let app = Router::new().nest( + "/neko", + Router::new().route_with_tsr("/nyan/", get(|| async {})), + ); + + let client = TestClient::new(app); + let res = client.get("/neko/nyan/").await; + assert_eq!(res.status(), StatusCode::OK); + + let res = client.get("/neko/nyan").await; + assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT); + assert_eq!(res.headers()["location"], "/neko/nyan/"); + } + #[test] #[should_panic = "Cannot add a trailing slash redirect route for `/`"] fn tsr_at_root() {