mirror of
https://github.com/rwf2/Rocket.git
synced 2025-01-31 22:02:03 +00:00
Improve and fix panic in 'AdHoc::normalizer()'.
The normalizer now handles more cases.
This commit is contained in:
parent
311a82e3d7
commit
f1f533c1e5
@ -321,7 +321,7 @@ impl AdHoc {
|
|||||||
fn routes(&self, rocket: &Rocket<Orbit>) -> &[crate::Route] {
|
fn routes(&self, rocket: &Rocket<Orbit>) -> &[crate::Route] {
|
||||||
self.routes.get_or_set(|| {
|
self.routes.get_or_set(|| {
|
||||||
rocket.routes()
|
rocket.routes()
|
||||||
.filter(|r| r.uri.has_trailing_slash() || r.uri.metadata.dynamic_trail)
|
.filter(|r| r.uri.has_trailing_slash())
|
||||||
.cloned()
|
.cloned()
|
||||||
.collect()
|
.collect()
|
||||||
})
|
})
|
||||||
@ -353,7 +353,8 @@ impl AdHoc {
|
|||||||
let new_path = path.as_str()
|
let new_path = path.as_str()
|
||||||
.rsplit_once('/')
|
.rsplit_once('/')
|
||||||
.map(|(prefix, _)| prefix)
|
.map(|(prefix, _)| prefix)
|
||||||
.unwrap_or(path.as_str());
|
.filter(|path| !path.is_empty())
|
||||||
|
.unwrap_or("/");
|
||||||
|
|
||||||
let base = route.uri.base().as_str();
|
let base = route.uri.base().as_str();
|
||||||
let uri = match route.uri.unmounted().query() {
|
let uri = match route.uri.unmounted().query() {
|
||||||
@ -362,7 +363,7 @@ impl AdHoc {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let mut route = route.clone();
|
let mut route = route.clone();
|
||||||
route.uri = RouteUri::try_new(base, &uri).expect("valid => valid");
|
route.uri = RouteUri::try_new(base, &uri).ok()?;
|
||||||
route.name = route.name.map(|r| format!("{} [normalized]", r).into());
|
route.name = route.name.map(|r| format!("{} [normalized]", r).into());
|
||||||
Some(route)
|
Some(route)
|
||||||
})
|
})
|
||||||
|
@ -20,60 +20,47 @@ fn baz(_baz: PathBuf) -> &'static str { "baz" }
|
|||||||
#[get("/doggy/<_>/<_baz..>?doggy")]
|
#[get("/doggy/<_>/<_baz..>?doggy")]
|
||||||
fn doggy(_baz: PathBuf) -> &'static str { "doggy" }
|
fn doggy(_baz: PathBuf) -> &'static str { "doggy" }
|
||||||
|
|
||||||
|
#[get("/<_..>")]
|
||||||
|
fn rest() -> &'static str { "rest" }
|
||||||
|
|
||||||
|
macro_rules! assert_response {
|
||||||
|
($client:ident : $path:expr => $response:expr) => {
|
||||||
|
let response = $client.get($path).dispatch().into_string().unwrap();
|
||||||
|
assert_eq!(response, $response, "\nGET {}: got {} but expected {}",
|
||||||
|
$path, response, $response);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_adhoc_normalizer_works_as_expected () {
|
fn test_adhoc_normalizer_works_as_expected () {
|
||||||
let rocket = rocket::build()
|
let rocket = rocket::build()
|
||||||
.mount("/", routes![foo, bar, not_bar, baz, doggy])
|
.mount("/", routes![foo, bar, not_bar, baz, doggy])
|
||||||
.mount("/base", routes![foo, bar, not_bar, baz, doggy])
|
.mount("/base", routes![foo, bar, not_bar, baz, doggy, rest])
|
||||||
.attach(AdHoc::uri_normalizer());
|
.attach(AdHoc::uri_normalizer());
|
||||||
|
|
||||||
let client = Client::debug(rocket).unwrap();
|
let client = match Client::debug(rocket) {
|
||||||
|
Ok(client) => client,
|
||||||
|
Err(e) => { e.pretty_print(); panic!("failed to build client"); }
|
||||||
|
};
|
||||||
|
|
||||||
let response = client.get("/foo/").dispatch();
|
assert_response!(client: "/foo" => "foo");
|
||||||
assert_eq!(response.into_string().unwrap(), "foo");
|
assert_response!(client: "/foo/" => "foo");
|
||||||
|
assert_response!(client: "/bar/" => "bar");
|
||||||
|
assert_response!(client: "/bar" => "not_bar");
|
||||||
|
assert_response!(client: "/foo/bar" => "baz");
|
||||||
|
assert_response!(client: "/doggy/bar?doggy" => "doggy");
|
||||||
|
assert_response!(client: "/foo/bar/" => "baz");
|
||||||
|
assert_response!(client: "/foo/bar/baz" => "baz");
|
||||||
|
assert_response!(client: "/base/foo/" => "foo");
|
||||||
|
assert_response!(client: "/base/foo" => "foo");
|
||||||
|
assert_response!(client: "/base/bar/" => "bar");
|
||||||
|
assert_response!(client: "/base/bar" => "not_bar");
|
||||||
|
assert_response!(client: "/base/foo/bar" => "baz");
|
||||||
|
assert_response!(client: "/doggy/foo/bar?doggy" => "doggy");
|
||||||
|
assert_response!(client: "/base/foo/bar/" => "baz");
|
||||||
|
assert_response!(client: "/base/foo/bar/baz" => "baz");
|
||||||
|
|
||||||
let response = client.get("/foo").dispatch();
|
assert_response!(client: "/base/cat" => "rest");
|
||||||
assert_eq!(response.into_string().unwrap(), "foo");
|
assert_response!(client: "/base/cat/" => "rest");
|
||||||
|
assert_response!(client: "/base/cat/dog" => "rest");
|
||||||
let response = client.get("/bar/").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "bar");
|
|
||||||
|
|
||||||
let response = client.get("/bar").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "not_bar");
|
|
||||||
|
|
||||||
let response = client.get("/foo/bar").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "baz");
|
|
||||||
|
|
||||||
let response = client.get("/doggy/bar?doggy").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "doggy");
|
|
||||||
|
|
||||||
let response = client.get("/foo/bar/").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "baz");
|
|
||||||
|
|
||||||
let response = client.get("/foo/bar/baz").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "baz");
|
|
||||||
|
|
||||||
let response = client.get("/base/foo/").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "foo");
|
|
||||||
|
|
||||||
let response = client.get("/base/foo").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "foo");
|
|
||||||
|
|
||||||
let response = client.get("/base/bar/").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "bar");
|
|
||||||
|
|
||||||
let response = client.get("/base/bar").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "not_bar");
|
|
||||||
|
|
||||||
let response = client.get("/base/foo/bar").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "baz");
|
|
||||||
|
|
||||||
let response = client.get("/doggy/foo/bar?doggy").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "doggy");
|
|
||||||
|
|
||||||
let response = client.get("/base/foo/bar/").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "baz");
|
|
||||||
|
|
||||||
let response = client.get("/base/foo/bar/baz").dispatch();
|
|
||||||
assert_eq!(response.into_string().unwrap(), "baz");
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user