mirror of
https://github.com/tokio-rs/axum.git
synced 2025-09-30 14:31:16 +00:00
Fix TSR redirecting to top-level inside nested Router (#2993)
Co-authored-by: David Mládek <david.mladek.cz@gmail.com>
This commit is contained in:
parent
65ad603701
commit
114369418d
@ -39,7 +39,7 @@ typed-header = ["dep:headers"]
|
|||||||
typed-routing = ["dep:axum-macros", "dep:percent-encoding", "dep:serde_html_form", "dep:form_urlencoded"]
|
typed-routing = ["dep:axum-macros", "dep:percent-encoding", "dep:serde_html_form", "dep:form_urlencoded"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum = { path = "../axum", version = "0.8.0-alpha.1", default-features = false }
|
axum = { path = "../axum", version = "0.8.0-alpha.1", default-features = false, features = ["original-uri"] }
|
||||||
axum-core = { path = "../axum-core", version = "0.5.0-alpha.1" }
|
axum-core = { path = "../axum-core", version = "0.5.0-alpha.1" }
|
||||||
bytes = "1.1.0"
|
bytes = "1.1.0"
|
||||||
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
|
futures-util = { version = "0.3", default-features = false, features = ["alloc"] }
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//! Additional types for defining routes.
|
//! Additional types for defining routes.
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::Request,
|
extract::{OriginalUri, Request},
|
||||||
response::{IntoResponse, Redirect, Response},
|
response::{IntoResponse, Redirect, Response},
|
||||||
routing::{any, MethodRouter},
|
routing::{any, MethodRouter},
|
||||||
Router,
|
Router,
|
||||||
@ -313,7 +313,7 @@ fn add_tsr_redirect_route<S>(router: Router<S>, path: &str) -> Router<S>
|
|||||||
where
|
where
|
||||||
S: Clone + Send + Sync + 'static,
|
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| {
|
let new_uri = map_path(uri, |path| {
|
||||||
path.strip_suffix('/')
|
path.strip_suffix('/')
|
||||||
.map(Cow::Borrowed)
|
.map(Cow::Borrowed)
|
||||||
@ -432,6 +432,22 @@ mod tests {
|
|||||||
assert_eq!(res.headers()["location"], "/foo?a=a");
|
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]
|
#[test]
|
||||||
#[should_panic = "Cannot add a trailing slash redirect route for `/`"]
|
#[should_panic = "Cannot add a trailing slash redirect route for `/`"]
|
||||||
fn tsr_at_root() {
|
fn tsr_at_root() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user