mirror of
https://github.com/fafhrd91/actix-net
synced 2025-02-21 14:04:49 +01:00
soft-disallow prefix resources with tail segments (#379)
This commit is contained in:
parent
b122a1ae1a
commit
0183b0f8cc
@ -1070,13 +1070,32 @@ impl ResourceDef {
|
|||||||
dyn_segment_count += 1;
|
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('*') {
|
if unprocessed.ends_with('*') {
|
||||||
// unnamed tail segment
|
// unnamed tail segment
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[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)]
|
#[cfg(test)]
|
||||||
panic!("tail segments must have names");
|
panic!("tail segments must have names");
|
||||||
} else if !has_tail_segment && !unprocessed.is_empty() {
|
} 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(["/"]));
|
||||||
assert_eq!(ResourceDef::new("/"), ResourceDef::new(vec!["/"]));
|
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(""));
|
||||||
assert_ne!(ResourceDef::new("/"), ResourceDef::prefix("/"));
|
assert_ne!(ResourceDef::new("/"), ResourceDef::prefix("/"));
|
||||||
@ -1774,11 +1792,11 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
#[should_panic]
|
||||||
fn invalid_unnamed_tail_segment() {
|
fn invalid_unnamed_tail_segment() {
|
||||||
ResourceDef::new(r"/*");
|
ResourceDef::new("/*");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
// #[should_panic] // TODO: consider if this should be allowed
|
#[should_panic]
|
||||||
fn prefix_plus_tail_match_is_allowed() {
|
fn prefix_plus_tail_match_is_allowed() {
|
||||||
ResourceDef::prefix("/user/{id}*");
|
ResourceDef::prefix("/user/{id}*");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user