test thoroughly

This commit is contained in:
Ali MJ Al-Nasrawy 2021-06-03 01:12:39 +03:00
parent 48b1fc5e03
commit fa0092ffa1
1 changed files with 45 additions and 10 deletions

View File

@ -204,26 +204,61 @@ mod tests {
use super::*; use super::*;
use crate::{Path, ResourceDef}; use crate::{Path, ResourceDef};
const PROTECTED: &[u8] = b"%/+";
fn match_url(pattern: &'static str, url: impl AsRef<str>) -> Path<Url> {
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] #[test]
fn test_parse_url() { 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 path = match_url(re, "/user/2345/test");
let mut path = Path::new(Url::new(url));
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "2345"); assert_eq!(path.get("id").unwrap(), "2345");
let url = Uri::try_from("/user/qwe%25/test").unwrap(); // "%25" should never be decoded into '%' to gurantee the output is a valid
let mut path = Path::new(Url::new(url)); // percent-encoded format
assert!(re.match_path(&mut path)); let path = match_url(re, "/user/qwe%25/test");
assert_eq!(path.get("id").unwrap(), "qwe%25"); assert_eq!(path.get("id").unwrap(), "qwe%25");
let url = Uri::try_from("/user/qwe%25rty/test").unwrap(); let path = match_url(re, "/user/qwe%25rty/test");
let mut path = Path::new(Url::new(url));
assert!(re.match_path(&mut path));
assert_eq!(path.get("id").unwrap(), "qwe%25rty"); 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::<String>();
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::<String>();
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] #[test]
fn test_from_hex() { fn test_from_hex() {
let hex = b"0123456789abcdefABCDEF"; let hex = b"0123456789abcdefABCDEF";