Skip to content

Commit

Permalink
Fix path normalization of index route
Browse files Browse the repository at this point in the history
Previously, `normalize_path` on `/` would result in an empty string, causing
routers to return 404 on the index route. Fix this by doing nothing if the
resultant string would be empty.

Fixes tower-rs#318.
  • Loading branch information
cassaundra committed Mar 24, 2023
1 parent 4ebe811 commit 87b44d8
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tower-http/src/normalize_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ fn remove_trailing_slash(uri: &mut Uri) {

let new_path = uri.path().trim_end_matches('/');

if new_path.is_empty() {
return;
}

let mut parts = uri.clone().into_parts();

let new_path_and_query = if let Some(path_and_query) = &parts.path_and_query {
Expand Down Expand Up @@ -164,6 +168,13 @@ mod tests {
assert_eq!(body, "/foo");
}

#[test]
fn is_noop_on_index() {
let mut uri = "/".parse::<Uri>().unwrap();
remove_trailing_slash(&mut uri);
assert_eq!(uri, "/");
}

#[test]
fn is_noop_if_no_trailing_slash() {
let mut uri = "/foo".parse::<Uri>().unwrap();
Expand Down

0 comments on commit 87b44d8

Please sign in to comment.