mirror of
https://github.com/rwf2/Rocket.git
synced 2025-01-02 15:52:41 +00:00
4e06ee64aa
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.
118 lines
3.9 KiB
Rust
118 lines
3.9 KiB
Rust
#[macro_use] extern crate rocket;
|
|
|
|
use rocket::local::blocking::Client;
|
|
use rocket::http::{ContentType, MediaType, Accept, Status};
|
|
|
|
// Test that known formats work as expected, including not colliding.
|
|
|
|
#[post("/", format = "json")]
|
|
fn json() -> &'static str { "json" }
|
|
|
|
#[post("/", format = "xml")]
|
|
fn xml() -> &'static str { "xml" }
|
|
|
|
// Unreachable. Written for codegen.
|
|
#[post("/", format = "application/json", rank = 2)]
|
|
fn json_long() -> &'static str { "json_long" }
|
|
|
|
#[post("/", format = "application/msgpack")]
|
|
fn msgpack_long() -> &'static str { "msgpack_long" }
|
|
|
|
// Unreachable. Written for codegen.
|
|
#[post("/", format = "msgpack", rank = 2)]
|
|
fn msgpack() -> &'static str { "msgpack" }
|
|
|
|
#[get("/", format = "plain")]
|
|
fn plain() -> &'static str { "plain" }
|
|
|
|
#[get("/", format = "binary", rank = 2)]
|
|
fn binary() -> &'static str { "binary" }
|
|
|
|
#[get("/", rank = 3)]
|
|
fn other() -> &'static str { "other" }
|
|
|
|
#[test]
|
|
fn test_formats() {
|
|
let rocket = rocket::ignite()
|
|
.mount("/", routes![json, xml, json_long, msgpack_long, msgpack,
|
|
plain, binary, other]);
|
|
|
|
let client = Client::debug(rocket).unwrap();
|
|
|
|
let response = client.post("/").header(ContentType::JSON).dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "json");
|
|
|
|
let response = client.post("/").header(ContentType::MsgPack).dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "msgpack_long");
|
|
|
|
let response = client.post("/").header(ContentType::XML).dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "xml");
|
|
|
|
let response = client.get("/").header(Accept::Plain).dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "plain");
|
|
|
|
let response = client.get("/").header(Accept::Binary).dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "binary");
|
|
|
|
let response = client.get("/").header(ContentType::JSON).dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "plain");
|
|
|
|
let response = client.get("/").dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "plain");
|
|
|
|
let response = client.put("/").header(ContentType::HTML).dispatch();
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
}
|
|
|
|
// Test custom formats.
|
|
|
|
// TODO: #[rocket(allow(unknown_format))]
|
|
#[get("/", format = "application/foo")]
|
|
fn get_foo() -> &'static str { "get_foo" }
|
|
|
|
// TODO: #[rocket(allow(unknown_format))]
|
|
#[post("/", format = "application/foo")]
|
|
fn post_foo() -> &'static str { "post_foo" }
|
|
|
|
// TODO: #[rocket(allow(unknown_format))]
|
|
#[get("/", format = "bar/baz", rank = 2)]
|
|
fn get_bar_baz() -> &'static str { "get_bar_baz" }
|
|
|
|
// TODO: #[rocket(allow(unknown_format))]
|
|
#[put("/", format = "bar/baz")]
|
|
fn put_bar_baz() -> &'static str { "put_bar_baz" }
|
|
|
|
#[test]
|
|
fn test_custom_formats() {
|
|
let rocket = rocket::ignite()
|
|
.mount("/", routes![get_foo, post_foo, get_bar_baz, put_bar_baz]);
|
|
|
|
let client = Client::debug(rocket).unwrap();
|
|
|
|
let foo_a = Accept::new(&[MediaType::new("application", "foo").into()]);
|
|
let foo_ct = ContentType::new("application", "foo");
|
|
let bar_baz_ct = ContentType::new("bar", "baz");
|
|
let bar_baz_a = Accept::new(&[MediaType::new("bar", "baz").into()]);
|
|
|
|
let response = client.get("/").header(foo_a).dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "get_foo");
|
|
|
|
let response = client.post("/").header(foo_ct).dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "post_foo");
|
|
|
|
let response = client.get("/").header(bar_baz_a).dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "get_bar_baz");
|
|
|
|
let response = client.put("/").header(bar_baz_ct).dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "put_bar_baz");
|
|
|
|
let response = client.get("/").dispatch();
|
|
assert_eq!(response.into_string().unwrap(), "get_foo");
|
|
|
|
let response = client.put("/").header(ContentType::HTML).dispatch();
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
let response = client.post("/").header(ContentType::HTML).dispatch();
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
}
|