servme/tests/url_extract.rs

48 lines
1.5 KiB
Rust
Raw Normal View History

2025-01-30 01:27:21 +01:00
use servme::UrlExtract;
#[test]
fn param_str() {
let uri = "https://example.com/path?param1=value1&param2=value2&param3=value%203"
.parse()
.unwrap();
let extractor = UrlExtract::new(&uri);
assert_eq!(extractor.param_str("param1"), Some("value1".to_string()));
assert_eq!(extractor.param_str("param2"), Some("value2".to_string()));
assert_eq!(extractor.param_str("param3"), Some("value 3".to_string()));
assert_eq!(extractor.param_str("nonexistent"), None);
}
#[test]
fn param_i64() {
let uri = "https://example.com/path?param1=42&param2=invalid"
.parse()
.unwrap();
let extractor = UrlExtract::new(&uri);
assert_eq!(extractor.param_i64("param1"), Some(42));
assert_eq!(extractor.param_i64("param2"), None);
assert_eq!(extractor.param_i64("nonexistent"), None);
}
#[test]
fn param_f64() {
let uri = "https://example.com/path?param1=3.14&param2=invalid"
.parse()
.unwrap();
let extractor = UrlExtract::new(&uri);
assert_eq!(extractor.param_f64("param1"), Some(3.14));
assert_eq!(extractor.param_f64("param2"), None);
assert_eq!(extractor.param_f64("nonexistent"), None);
}
#[test]
fn url_i64() {
let uri = "https://example.com/path/42/rest".parse().unwrap();
assert_eq!(UrlExtract::url_i64(&uri, "/path/"), Some(42));
let uri_without_match = "https://example.com/otherpath/42/rest".parse().unwrap();
assert_eq!(UrlExtract::url_i64(&uri_without_match, "/path/"), None);
}