mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-08 00:00:24 +02:00
Fix inconsistent double slash when nesting routes (#824)
This commit is contained in:
@@ -215,6 +215,8 @@ where
|
||||
path.into()
|
||||
} else if path == "/" {
|
||||
(&*nested_path).into()
|
||||
} else if let Some(path) = path.strip_suffix('/') {
|
||||
format!("{}{}", path, nested_path).into()
|
||||
} else {
|
||||
format!("{}{}", path, nested_path).into()
|
||||
};
|
||||
|
||||
@@ -345,3 +345,37 @@ async fn outer_middleware_still_see_whole_url() {
|
||||
);
|
||||
assert_eq!(client.get("/one/two").send().await.text().await, "/one/two");
|
||||
}
|
||||
|
||||
macro_rules! nested_route_test {
|
||||
(
|
||||
$name:ident,
|
||||
nest = $nested_path:literal,
|
||||
route = $route_path:literal,
|
||||
expected = $expected_path:literal $(,)?
|
||||
) => {
|
||||
#[tokio::test]
|
||||
async fn $name() {
|
||||
let inner = Router::new().route($route_path, get(|| async {}));
|
||||
let app = Router::new().nest($nested_path, inner);
|
||||
let client = TestClient::new(app);
|
||||
assert_eq!(
|
||||
client.get($expected_path).send().await.status(),
|
||||
StatusCode::OK
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// test cases taken from https://github.com/tokio-rs/axum/issues/714#issuecomment-1058144460
|
||||
nested_route_test!(nest_1, nest = "", route = "/", expected = "/");
|
||||
nested_route_test!(nest_2, nest = "", route = "/a", expected = "/a");
|
||||
nested_route_test!(nest_3, nest = "", route = "/a/", expected = "/a/");
|
||||
nested_route_test!(nest_4, nest = "/", route = "/", expected = "/");
|
||||
nested_route_test!(nest_5, nest = "/", route = "/a", expected = "/a");
|
||||
nested_route_test!(nest_6, nest = "/", route = "/a/", expected = "/a/");
|
||||
nested_route_test!(nest_7, nest = "/a", route = "/", expected = "/a");
|
||||
nested_route_test!(nest_8, nest = "/a", route = "/a", expected = "/a/a");
|
||||
nested_route_test!(nest_9, nest = "/a", route = "/a/", expected = "/a/a/");
|
||||
nested_route_test!(nest_11, nest = "/a/", route = "/", expected = "/a/");
|
||||
nested_route_test!(nest_12, nest = "/a/", route = "/a", expected = "/a/a");
|
||||
nested_route_test!(nest_13, nest = "/a/", route = "/a/", expected = "/a/a/");
|
||||
|
||||
Reference in New Issue
Block a user