Skip to content

Commit

Permalink
filter out empty string parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
CJGutz committed Aug 31, 2024
1 parent 1e37a1a commit 53bd85e
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions unchained/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ fn read_file_to_respond(file: &str) -> Response {

/// Checks if the route matches the path.
/// Finds path params in the request.
/// Removes all empty parameters.
fn compare_route_w_path_and_get_path_params(
route: &str,
req_path: &str,
Expand All @@ -124,17 +125,19 @@ fn compare_route_w_path_and_get_path_params(
.trim_end_matches('*')
.trim_end_matches('/')
.split('/')
.filter(|s| !s.is_empty())
.collect::<Vec<_>>();
let req_parts = req_path
.trim_start_matches('/')
.trim_end_matches('/')
.split('/');
.split('/')
.filter(|s| !s.is_empty());
let mut params = HashMap::new();
let mut match_route = true;
let last_is_star = route.ends_with('*');
for (count, req_part) in req_parts.clone().enumerate() {
let route_part = route_parts.get(count);
if route_part.is_none() {
let last_is_star = route.ends_with('*');
match_route = last_is_star;
break;
}
Expand Down Expand Up @@ -192,6 +195,7 @@ mod tests {
("/path/", "path"),
("path", "/path"),
("/path", "path"),
("/path///", "path"),
];
for (route, path) in route_paths {
let (matches, _) = compare_route_w_path_and_get_path_params(route, path);
Expand Down Expand Up @@ -227,4 +231,27 @@ mod tests {
assert!(!matches);
}
}

#[test]
fn test_catchall_route() {
let route = "*";
let path = "/some-path";
let (matches, _) = compare_route_w_path_and_get_path_params(route, path);
assert!(matches);
}

#[test]
fn test_root_route() {
let route_paths = vec![
("/", "/"),
("/*", "/"),
("", "/"),
("/", ""),
("", ""),
];
for (route, path) in route_paths {
let (matches, _) = compare_route_w_path_and_get_path_params(route, path);
assert!(matches);
}
}
}

0 comments on commit 53bd85e

Please sign in to comment.