|
1 | 1 | //! Additional types for defining routes. |
2 | 2 |
|
3 | 3 | use axum::{ |
4 | | - extract::Request, |
| 4 | + extract::{OriginalUri, Request}, |
5 | 5 | response::{IntoResponse, Redirect, Response}, |
6 | 6 | routing::{any, MethodRouter}, |
7 | 7 | Router, |
@@ -313,7 +313,7 @@ fn add_tsr_redirect_route<S>(router: Router<S>, path: &str) -> Router<S> |
313 | 313 | where |
314 | 314 | S: Clone + Send + Sync + 'static, |
315 | 315 | { |
316 | | - async fn redirect_handler(uri: Uri) -> Response { |
| 316 | + async fn redirect_handler(OriginalUri(uri): OriginalUri) -> Response { |
317 | 317 | let new_uri = map_path(uri, |path| { |
318 | 318 | path.strip_suffix('/') |
319 | 319 | .map(Cow::Borrowed) |
@@ -432,6 +432,22 @@ mod tests { |
432 | 432 | assert_eq!(res.headers()["location"], "/foo?a=a"); |
433 | 433 | } |
434 | 434 |
|
| 435 | + #[tokio::test] |
| 436 | + async fn tsr_works_in_nested_router() { |
| 437 | + let app = Router::new().nest( |
| 438 | + "/neko", |
| 439 | + Router::new().route_with_tsr("/nyan/", get(|| async {})), |
| 440 | + ); |
| 441 | + |
| 442 | + let client = TestClient::new(app); |
| 443 | + let res = client.get("/neko/nyan/").await; |
| 444 | + assert_eq!(res.status(), StatusCode::OK); |
| 445 | + |
| 446 | + let res = client.get("/neko/nyan").await; |
| 447 | + assert_eq!(res.status(), StatusCode::PERMANENT_REDIRECT); |
| 448 | + assert_eq!(res.headers()["location"], "/neko/nyan/"); |
| 449 | + } |
| 450 | + |
435 | 451 | #[test] |
436 | 452 | #[should_panic = "Cannot add a trailing slash redirect route for `/`"] |
437 | 453 | fn tsr_at_root() { |
|
0 commit comments