Rocket/core/lib/tests/adhoc-uri-normalizer.rs
Sergio Benitez d24b5d4d6d Handle more cases in 'AdHoc::normalizer()'.
The compatibility normalizer previously missed or was overly egregious
in several cases. This commit resolves those issue. In particular:

  * Only request URIs that would not match any route are normalized.

  * Synthetic routes are added to the igniting `Rocket` so that requests
    with URIs of the form `/foo` match routes with URIs of the form
    `/foo/<b..>`, as they did prior to the trailing slash overhaul.

Tests are added for all of these cases.
2023-05-04 17:30:37 -07:00

80 lines
2.5 KiB
Rust

#[macro_use] extern crate rocket;
use std::path::PathBuf;
use rocket::local::blocking::Client;
use rocket::fairing::AdHoc;
#[get("/foo")]
fn foo() -> &'static str { "foo" }
#[get("/bar")]
fn not_bar() -> &'static str { "not_bar" }
#[get("/bar/")]
fn bar() -> &'static str { "bar" }
#[get("/foo/<_>/<_baz..>")]
fn baz(_baz: PathBuf) -> &'static str { "baz" }
#[get("/doggy/<_>/<_baz..>?doggy")]
fn doggy(_baz: PathBuf) -> &'static str { "doggy" }
#[test]
fn test_adhoc_normalizer_works_as_expected () {
let rocket = rocket::build()
.mount("/", routes![foo, bar, not_bar, baz, doggy])
.mount("/base", routes![foo, bar, not_bar, baz, doggy])
.attach(AdHoc::uri_normalizer());
let client = Client::debug(rocket).unwrap();
let response = client.get("/foo/").dispatch();
assert_eq!(response.into_string().unwrap(), "foo");
let response = client.get("/foo").dispatch();
assert_eq!(response.into_string().unwrap(), "foo");
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");
}