match path against route

This commit is contained in:
takashiidobe 2020-06-23 21:17:02 -04:00
parent 6e53e14348
commit 299a5e74f5
2 changed files with 11 additions and 44 deletions

View File

@ -140,7 +140,7 @@ impl HttpRequest {
/// Checks if a given path matches a route
#[inline]
pub fn match_name(&self) -> Option<String> {
self.0.rmap.match_name(&self.path())
self.0.rmap.match_name(self.path())
}
/// Request extensions
@ -478,14 +478,11 @@ mod tests {
assert!(rmap.has_resource("/index.html"));
let req = TestRequest::with_uri("/test")
.header(header::HOST, "www.rust-lang.org")
.rmap(rmap)
.to_http_request();
let route_name = req.0.rmap.match_name("index");
let req = TestRequest::default().rmap(rmap).to_http_request();
let route_name = req.0.rmap.match_name("/index.html");
assert_eq!(
route_name.unwrap(),
rdef.name().to_owned()
"index".to_owned()
);
}
@ -637,38 +634,6 @@ mod tests {
assert!(tracker.borrow().dropped);
}
#[actix_rt::test]
async fn test_path_routes() {
let mut srv = init_service(
App::new().service(
web::scope("/user/{id}")
.service(web::resource("/profile").route(web::get().to(
move |req: HttpRequest| {
assert_eq!(
req.0.rmap.match_name("/user/22/profile"),
Some(ResourceDef::new("/profile").name().to_owned())
);
HttpResponse::Ok().finish()
},
)))
.default_service(web::to(move |req: HttpRequest| {
assert!(req.match_name().is_none());
HttpResponse::Ok().finish()
})),
),
)
.await;
let req = TestRequest::get().uri("/user/22/profile").to_request();
let res = call_service(&mut srv, req).await;
assert_eq!(res.status(), StatusCode::OK);
let req = TestRequest::get().uri("/user/22/not-exist").to_request();
let res = call_service(&mut srv, req).await;
assert_eq!(res.status(), StatusCode::OK);
}
#[actix_rt::test]
async fn extract_path_pattern() {
let mut srv = init_service(

View File

@ -92,16 +92,18 @@ impl ResourceMap {
}
false
}
/// Returns the full resource pattern matched against a route or None if no match is
/// Returns the name of the route that matches the given path or None if no match is
/// found.
pub fn match_name(&self, path: &str) -> Option<String> {
let path = if path.is_empty() { "/" } else { path };
let route = self.named.get(path);
match route {
Some(route) => Some(route.name().to_string()),
None => None,
for (pattern, _) in &self.patterns {
if pattern.pattern() == path {
return Some(pattern.name().to_string());
}
}
None
}
/// Returns the full resource pattern matched against a path or None if no full match