#![cfg(test)]
use super::*;
use crate::request::HttpRequest;
use crate::response::HttpResponse;
#[test]
fn test_static_match() {
let route = Route::new("/echo/test");
assert!(route.is_match("/echo/test").is_some());
assert!(route.is_match("/echo/fail").is_none());
}
#[test]
fn test_parameter_match() {
let route = Route::new("/echo/{message}");
let params = route.is_match("/echo/hello").unwrap();
assert_eq!(params.get("message"), Some(&"hello".to_string()));
}
#[test]
fn test_wildcard_match() {
let route = Route::new("/echo/*");
assert!(route.is_match("/echo/anything").is_some());
assert!(route.is_match("/echo/multiple/segments").is_none());
}
#[test]
fn test_wildcard_deep_match() {
let route = Route::new("/echo/**");
let params = route.is_match("/echo/multiple/segments").unwrap();
assert_eq!(
params.get("wildcard_deep"),
Some(&"multiple/segments".to_string())
);
}
#[test]
fn test_route_addition() {
let route1 = Route::new("/echo");
let route2 = Route::new("/test");
let combined = route1 + route2;
assert!(combined.is_match("/echo/test").is_some());
}
#[test]
fn test_route_ordering() {
let route1 = Route::new("/echo");
let route2 = Route::new("/echo/{message}");
assert!(route1 < route2);
}
#[test]
fn test_route_iteration() {
let route = Route::new("/echo/{message}");
let segments: Vec<_> = route.into_iter().collect();
assert_eq!(segments.len(), 2);
}
#[test]
fn test_match_wildcard_path() {
let mut root = RouteNode::new();
root.insert(&Route::new("/wildcard/*"), |_req: HttpRequest| async move {
HttpResponse::new(200)
});
let route = Route::new("/wildcard/some_value");
assert!(root.match_path(&route).is_some());
}
#[test]
fn test_match_deep_wildcard_path() {
let mut root = RouteNode::new();
root.insert(
&Route::new("/wildcard_deep/**"),
|_req: HttpRequest| async move { HttpResponse::new(200) },
);
let route = Route::new("/wildcard_deep/any/level/of/segments");
assert!(root.match_path(&route).is_some());
}