diff --git a/actix-router/src/url.rs b/actix-router/src/url.rs index 1293b88e..d568e5af 100644 --- a/actix-router/src/url.rs +++ b/actix-router/src/url.rs @@ -204,26 +204,61 @@ mod tests { use super::*; use crate::{Path, ResourceDef}; + const PROTECTED: &[u8] = b"%/+"; + + fn match_url(pattern: &'static str, url: impl AsRef) -> Path { + let re = ResourceDef::new(pattern); + let uri = Uri::try_from(url.as_ref()).unwrap(); + let mut path = Path::new(Url::new(uri)); + assert!(re.match_path(&mut path)); + path + } + + fn percent_encode(data: &[u8]) -> String { + data.into_iter().map(|c| format!("%{:02X}", c)).collect() + } + #[test] fn test_parse_url() { - let re = ResourceDef::new("/user/{id}/test"); + let re = "/user/{id}/test"; - let url = Uri::try_from("/user/2345/test").unwrap(); - let mut path = Path::new(Url::new(url)); - assert!(re.match_path(&mut path)); + let path = match_url(re, "/user/2345/test"); assert_eq!(path.get("id").unwrap(), "2345"); - let url = Uri::try_from("/user/qwe%25/test").unwrap(); - let mut path = Path::new(Url::new(url)); - assert!(re.match_path(&mut path)); + // "%25" should never be decoded into '%' to gurantee the output is a valid + // percent-encoded format + let path = match_url(re, "/user/qwe%25/test"); assert_eq!(path.get("id").unwrap(), "qwe%25"); - let url = Uri::try_from("/user/qwe%25rty/test").unwrap(); - let mut path = Path::new(Url::new(url)); - assert!(re.match_path(&mut path)); + let path = match_url(re, "/user/qwe%25rty/test"); assert_eq!(path.get("id").unwrap(), "qwe%25rty"); } + #[test] + fn test_protected_chars() { + let encoded = percent_encode(PROTECTED); + let path = match_url("/user/{id}/test", format!("/user/{}/test", encoded)); + assert_eq!(path.get("id").unwrap(), &encoded); + } + + #[test] + fn test_non_protecteed_ascii() { + let nonprotected_ascii = ('\u{0}'..='\u{7F}') + .filter(|&c| c.is_ascii() && !PROTECTED.contains(&(c as u8))) + .collect::(); + let encoded = percent_encode(nonprotected_ascii.as_bytes()); + let path = match_url("/user/{id}/test", format!("/user/{}/test", encoded)); + assert_eq!(path.get("id").unwrap(), &nonprotected_ascii); + } + + #[test] + fn test_valid_utf8_multibyte() { + let test = ('\u{FF00}'..='\u{FFFF}').collect::(); + let encoded = percent_encode(test.as_bytes()); + let path = match_url("/a/{id}/b", format!("/a/{}/b", &encoded)); + assert_eq!(path.get("id").unwrap(), &test); + } + #[test] fn test_from_hex() { let hex = b"0123456789abcdefABCDEF";