Rocket/core/codegen/tests/route-raw.rs

44 lines
1.2 KiB
Rust
Raw Normal View History

#[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::ignite()
.mount("/", routes![get, swap])
.register("/", catchers![catch]);
Test 'secret_key' validation, now on pre-launch. Prior to this commit, it was not possible to test Rocket crates in production mode without setting a global secret key or bypassing secret key checking - the testing script did the latter. The consequence is that it became impossible to test secret key related failures because the tests passed regardless. This commit undoes this. As a consequence, all tests are now aware of the difference between debug and release configurations, the latter of which validates 'secret_key' by default. New 'Client::debug()' and 'Client::debug_with()' simplify creating an instance of 'Client' with configuration in debug mode to avoid undesired test failures. The summary of changes in this commit are: * Config 'secret_key' success and failure are now tested. * 'secret_key' validation was moved to pre-launch from 'Config:from()'. * 'Config::from()' only extracts the config. * Added 'Config::try_from()' for non-panicking extraction. * 'Config' now knows the profile it was extracted from. * The 'Config' provider sets a profile of 'Config.profile'. * 'Rocket', 'Client', 'Fairings', implement 'Debug'. * 'fairing::Info' implements 'Copy', 'Clone'. * 'Fairings' keeps track of, logs attach fairings. * 'Rocket::reconfigure()' was added to allow modifying a config. Internally, the testing script was refactored to properly test the codebase with the new changes. In particular, it no longer sets a rustc 'cfg' to avoid secret-key checking. Resolves #1543. Fixes #1564.
2021-03-09 08:07:43 +00:00
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");
}