fix: do not match incomplete static segments (closes #2916) (#2973)

This commit is contained in:
Greg Johnston 2024-09-14 16:20:45 -04:00 committed by GitHub
parent 6166f6edbd
commit 1fce8931ab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 1 deletions

View file

@ -104,6 +104,11 @@ impl<T: AsPath> PossibleRouteMatch for StaticSegment<T> {
}
}
// if we still have remaining, unmatched characters in this segment, it was not a match
if this.next().is_some() {
return None;
}
// build the match object
// the remaining is built from the path in, with the slice moved
// by the length of this match

View file

@ -156,6 +156,7 @@ mod tests {
matching::MatchParams, MatchInterface, PathSegment, StaticSegment,
WildcardSegment,
};
use either_of::Either;
use tachys::renderer::dom::Dom;
#[test]
@ -164,8 +165,11 @@ mod tests {
Routes::<_, Dom>::new(NestedRoute::new(StaticSegment("/"), || ()));
let matched = routes.match_route("/");
assert!(matched.is_some());
// this case seems like it should match, but implementing it interferes with
// handling trailing slash requirements accurately -- paths for the root are "/",
// not "", in any case
let matched = routes.match_route("");
assert!(matched.is_some());
assert!(matched.is_none());
let (base, paths) = routes.generate_routes();
assert_eq!(base, None);
let paths = paths.into_iter().map(|g| g.segments).collect::<Vec<_>>();
@ -204,6 +208,16 @@ mod tests {
);
}
#[test]
pub fn does_not_match_route_unless_full_param_matches() {
let routes = Routes::<_, Dom>::new((
NestedRoute::new(StaticSegment("/property-api"), || ()),
NestedRoute::new(StaticSegment("/property"), || ()),
));
let matched = routes.match_route("/property").unwrap();
assert!(matches!(matched, Either::Right(_)));
}
#[test]
pub fn does_not_match_incomplete_route() {
let routes: Routes<_, Dom> =