mirror of https://github.com/fafhrd91/actix-web
add method to match a path to a route
This commit is contained in:
parent
fa28175a74
commit
570ed075a8
|
@ -4,7 +4,7 @@ use std::{fmt, net};
|
||||||
|
|
||||||
use actix_http::http::{HeaderMap, Method, Uri, Version};
|
use actix_http::http::{HeaderMap, Method, Uri, Version};
|
||||||
use actix_http::{Error, Extensions, HttpMessage, Message, Payload, RequestHead};
|
use actix_http::{Error, Extensions, HttpMessage, Message, Payload, RequestHead};
|
||||||
use actix_router::{Path, Url};
|
use actix_router::{Path, Url, ResourceDef};
|
||||||
use futures_util::future::{ok, Ready};
|
use futures_util::future::{ok, Ready};
|
||||||
use tinyvec::TinyVec;
|
use tinyvec::TinyVec;
|
||||||
|
|
||||||
|
@ -135,6 +135,11 @@ impl HttpRequest {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn match_pattern(&self) -> Option<String> {
|
pub fn match_pattern(&self) -> Option<String> {
|
||||||
self.0.rmap.match_pattern(self.path())
|
self.0.rmap.match_pattern(self.path())
|
||||||
|
|
||||||
|
/// Checks if a given path matches a route
|
||||||
|
#[inline]
|
||||||
|
pub fn match_name(&self) -> Option<&ResourceDef> {
|
||||||
|
self.0.rmap.match_name(&self.path())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Request extensions
|
/// Request extensions
|
||||||
|
@ -462,6 +467,27 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_match_name() {
|
||||||
|
let mut rdef = ResourceDef::new("/index.html");
|
||||||
|
*rdef.name_mut() = "index".to_string();
|
||||||
|
|
||||||
|
let mut rmap = ResourceMap::new(ResourceDef::new(""));
|
||||||
|
rmap.add(&mut rdef, None);
|
||||||
|
|
||||||
|
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.resource_map().match_name("index").unwrap().name();
|
||||||
|
assert_eq!(
|
||||||
|
route_name,
|
||||||
|
rdef.name()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_url_for_external() {
|
fn test_url_for_external() {
|
||||||
let mut rdef = ResourceDef::new("https://youtube.com/watch/{video_id}");
|
let mut rdef = ResourceDef::new("https://youtube.com/watch/{video_id}");
|
||||||
|
|
|
@ -92,6 +92,12 @@ impl ResourceMap {
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
/// Returns the full resource pattern matched against a route or None if no match is
|
||||||
|
/// found.
|
||||||
|
pub fn match_name(&self, path: &str) -> Option<&ResourceDef> {
|
||||||
|
let path = if path.is_empty() { "/" } else { path };
|
||||||
|
self.named.get(path)
|
||||||
|
}
|
||||||
|
|
||||||
/// 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
|
||||||
/// is possible.
|
/// is possible.
|
||||||
|
|
Loading…
Reference in New Issue