Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nesting Router at / leading to double slash in the path #691

Merged
merged 4 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion axum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- None.
- **fixed:** Fix using incorrect path prefix when nesting `Router`s at `/` ([#691])
- **fixed:** Make `nest("", service)` work and mean the same as `nest("/", service)` ([#691])

[#691]: https://github.com/tokio-rs/axum/pull/691

# 0.4.3 (21. December, 2021)

Expand Down
13 changes: 8 additions & 5 deletions axum/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,14 @@ where
}

#[doc = include_str!("../docs/routing/nest.md")]
pub fn nest<T>(mut self, path: &str, svc: T) -> Self
pub fn nest<T>(mut self, mut path: &str, svc: T) -> Self
where
T: Service<Request<B>, Response = Response, Error = Infallible> + Clone + Send + 'static,
T::Future: Send + 'static,
{
if path.is_empty() {
panic!("Invalid route: empty path");
// nesting at `""` and `"/"` should mean the same thing
path = "/";
}

if path.contains('*') {
Expand Down Expand Up @@ -201,10 +202,12 @@ where

for (id, nested_path) in node.route_id_to_path {
let route = routes.remove(&id).unwrap();
let full_path = if &*nested_path == "/" {
path.to_owned()
let full_path: Cow<str> = if &*nested_path == "/" {
path.into()
} else if path == "/" {
(&*nested_path).into()
} else {
format!("{}{}", path, nested_path)
format!("{}{}", path, nested_path).into()
};
self = match route {
Endpoint::MethodRouter(method_router) => self.route(
Expand Down
7 changes: 0 additions & 7 deletions axum/src/routing/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,6 @@ async fn empty_route() {
TestClient::new(app);
}

#[tokio::test]
#[should_panic(expected = "Invalid route: empty path")]
async fn empty_route_nested() {
let app = Router::new().nest("", get(|| async {}));
TestClient::new(app);
}

#[tokio::test]
async fn middleware_still_run_for_unmatched_requests() {
#[derive(Clone)]
Expand Down
38 changes: 37 additions & 1 deletion axum/src/routing/tests/nest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,43 @@ async fn wrong_method_nest() {
}

#[tokio::test]
async fn nesting_at_root() {
async fn nesting_router_at_root() {
let nested = Router::new().route("/foo", get(|uri: Uri| async move { uri.to_string() }));
let app = Router::new().nest("/", nested);

let client = TestClient::new(app);

let res = client.get("/").send().await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);

let res = client.get("/foo").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "/foo");

let res = client.get("/foo/bar").send().await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn nesting_router_at_empty_path() {
let nested = Router::new().route("/foo", get(|uri: Uri| async move { uri.to_string() }));
let app = Router::new().nest("", nested);

let client = TestClient::new(app);

let res = client.get("/").send().await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);

let res = client.get("/foo").send().await;
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await, "/foo");

let res = client.get("/foo/bar").send().await;
assert_eq!(res.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn nesting_handler_at_root() {
let app = Router::new().nest("/", get(|uri: Uri| async move { uri.to_string() }));

let client = TestClient::new(app);
Expand Down