mirror of https://github.com/fafhrd91/actix-web
match path against route
This commit is contained in:
parent
6e53e14348
commit
299a5e74f5
|
@ -140,7 +140,7 @@ impl HttpRequest {
|
||||||
/// Checks if a given path matches a route
|
/// Checks if a given path matches a route
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn match_name(&self) -> Option<String> {
|
pub fn match_name(&self) -> Option<String> {
|
||||||
self.0.rmap.match_name(&self.path())
|
self.0.rmap.match_name(self.path())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Request extensions
|
/// Request extensions
|
||||||
|
@ -478,14 +478,11 @@ mod tests {
|
||||||
|
|
||||||
assert!(rmap.has_resource("/index.html"));
|
assert!(rmap.has_resource("/index.html"));
|
||||||
|
|
||||||
let req = TestRequest::with_uri("/test")
|
let req = TestRequest::default().rmap(rmap).to_http_request();
|
||||||
.header(header::HOST, "www.rust-lang.org")
|
let route_name = req.0.rmap.match_name("/index.html");
|
||||||
.rmap(rmap)
|
|
||||||
.to_http_request();
|
|
||||||
let route_name = req.0.rmap.match_name("index");
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
route_name.unwrap(),
|
route_name.unwrap(),
|
||||||
rdef.name().to_owned()
|
"index".to_owned()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -637,38 +634,6 @@ mod tests {
|
||||||
assert!(tracker.borrow().dropped);
|
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]
|
#[actix_rt::test]
|
||||||
async fn extract_path_pattern() {
|
async fn extract_path_pattern() {
|
||||||
let mut srv = init_service(
|
let mut srv = init_service(
|
||||||
|
|
12
src/rmap.rs
12
src/rmap.rs
|
@ -92,16 +92,18 @@ impl ResourceMap {
|
||||||
}
|
}
|
||||||
false
|
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.
|
/// found.
|
||||||
pub fn match_name(&self, path: &str) -> Option<String> {
|
pub fn match_name(&self, path: &str) -> Option<String> {
|
||||||
let path = if path.is_empty() { "/" } else { path };
|
let path = if path.is_empty() { "/" } else { path };
|
||||||
let route = self.named.get(path);
|
|
||||||
|
|
||||||
match route {
|
for (pattern, _) in &self.patterns {
|
||||||
Some(route) => Some(route.name().to_string()),
|
if pattern.pattern() == path {
|
||||||
None => None,
|
return Some(pattern.name().to_string());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the full resource pattern matched against a path or None if no full match
|
/// Returns the full resource pattern matched against a path or None if no full match
|
||||||
|
|
Loading…
Reference in New Issue