Rocket/core/codegen/tests/route-raw.rs
Sergio Benitez fa3e0334c1 Overhaul URI types, parsers, 'uri!' macro.
This commit entirely rewrites Rocket's URI parsing routines and
overhauls the 'uri!' macro resolving all known issues and removing any
potential limitations for compile-time URI creation. This commit:

  * Introduces a new 'Reference' URI variant for URI-references.
  * Modifies 'Redirect' to accept 'TryFrom<Reference>'.
  * Introduces a new 'Asterisk' URI variant for parity.
  * Allows creation of any URI type from a string literal via 'uri!'.
  * Enables dynamic/static prefixing/suffixing of route URIs in 'uri!'.
  * Unifies 'Segments' and 'QuerySegments' into one generic 'Segments'.
  * Consolidates URI formatting types/traits into a 'uri::fmt' module.
  * Makes APIs more symmetric across URI types.

It also includes the following less-relevant changes:

  * Implements 'FromParam' for a single-segment 'PathBuf'.
  * Adds 'FileName::is_safe()'.
  * No longer reparses upstream request URIs.

Resolves #842.
Resolves #853.
Resolves #998.
2021-05-19 18:47:11 -07:00

44 lines
1.2 KiB
Rust

#[macro_use] extern crate rocket;
use rocket::local::blocking::Client;
// Test that raw idents can be used for route parameter names
#[get("/<enum>?<type>")]
fn get(r#enum: String, r#type: i32) -> String {
format!("{} is {}", r#enum, r#type)
}
#[get("/swap/<raw>/<bare>")]
fn swap(r#raw: String, bare: String) -> String {
format!("{}, {}", raw, bare)
}
#[catch(400)]
fn catch(r#raw: &rocket::Request) -> String {
format!("{}", raw.method())
}
#[test]
fn test_raw_ident() {
let rocket = rocket::build()
.mount("/", routes![get, swap])
.register("/", catchers![catch]);
let client = Client::debug(rocket).unwrap();
let response = client.get("/example?type=1").dispatch();
assert_eq!(response.into_string().unwrap(), "example is 1");
let uri_named = uri!(get(r#enum = "test_named", r#type = 1));
assert_eq!(uri_named.to_string(), "/test_named?type=1");
let uri_unnamed = uri!(get("test_unnamed", 2));
assert_eq!(uri_unnamed.to_string(), "/test_unnamed?type=2");
let uri_raws = uri!(swap(r#raw = "1", r#bare = "2"));
assert_eq!(uri_raws.to_string(), "/swap/1/2");
let uri_bare = uri!(swap(raw = "1", bare = "2"));
assert_eq!(uri_bare.to_string(), "/swap/1/2");
}