1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-27 19:12:56 +01:00

soft-disallow prefix resources with tail segments (#379)

This commit is contained in:
Rob Ede 2021-08-06 18:48:49 +01:00 committed by GitHub
parent b122a1ae1a
commit 0183b0f8cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1070,13 +1070,32 @@ impl ResourceDef {
dyn_segment_count += 1;
}
if is_prefix && has_tail_segment {
// tail segments in prefixes have no defined semantics
#[cfg(not(test))]
log::warn!(
"Prefix resources should not have tail segments. \
Use `ResourceDef::new` constructor. \
This may become a panic in the future."
);
// panic in tests to make this case detectable
#[cfg(test)]
panic!("prefix resource definitions should not have tail segments");
}
if unprocessed.ends_with('*') {
// unnamed tail segment
#[cfg(not(test))]
log::warn!("tail segments must have names; consider `{{tail}}*`");
log::warn!(
"Tail segments must have names. \
Consider `.../{{tail}}*`. \
This may become a panic in the future."
);
// to this case detectable in tests
// panic in tests to make this case detectable
#[cfg(test)]
panic!("tail segments must have names");
} else if !has_tail_segment && !unprocessed.is_empty() {
@ -1197,7 +1216,6 @@ mod tests {
assert_eq!(ResourceDef::new("/"), ResourceDef::new(["/"]));
assert_eq!(ResourceDef::new("/"), ResourceDef::new(vec!["/"]));
assert_eq!(ResourceDef::new("/{id}*"), ResourceDef::prefix("/{id}*"));
assert_ne!(ResourceDef::new(""), ResourceDef::prefix(""));
assert_ne!(ResourceDef::new("/"), ResourceDef::prefix("/"));
@ -1774,11 +1792,11 @@ mod tests {
#[test]
#[should_panic]
fn invalid_unnamed_tail_segment() {
ResourceDef::new(r"/*");
ResourceDef::new("/*");
}
#[test]
// #[should_panic] // TODO: consider if this should be allowed
#[should_panic]
fn prefix_plus_tail_match_is_allowed() {
ResourceDef::prefix("/user/{id}*");
}