2018-09-20 04:14:30 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
use rocket::local::blocking::Client;
|
2018-09-20 04:14:30 +00:00
|
|
|
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" }
|
|
|
|
|
2018-10-05 04:44:42 +00:00
|
|
|
#[get("/", format = "binary", rank = 2)]
|
2018-09-20 04:14:30 +00:00
|
|
|
fn binary() -> &'static str { "binary" }
|
|
|
|
|
2018-10-05 04:44:42 +00:00
|
|
|
#[get("/", rank = 3)]
|
2018-09-20 04:14:30 +00:00
|
|
|
fn other() -> &'static str { "other" }
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_formats() {
|
2021-04-08 08:07:52 +00:00
|
|
|
let rocket = rocket::build()
|
2018-09-20 04:14:30 +00:00
|
|
|
.mount("/", routes![json, xml, json_long, msgpack_long, msgpack,
|
|
|
|
plain, binary, other]);
|
|
|
|
|
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();
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.post("/").header(ContentType::JSON).dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "json");
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.post("/").header(ContentType::MsgPack).dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "msgpack_long");
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.post("/").header(ContentType::XML).dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "xml");
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.get("/").header(Accept::Plain).dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "plain");
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.get("/").header(Accept::Binary).dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "binary");
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.get("/").header(ContentType::JSON).dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "plain");
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.get("/").dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "plain");
|
2018-10-05 04:44:42 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.put("/").header(ContentType::HTML).dispatch();
|
2018-10-05 04:44:42 +00:00
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
2018-09-20 04:14:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test custom formats.
|
|
|
|
|
2024-03-22 03:24:53 +00:00
|
|
|
#[suppress(unknown_format)]
|
2018-09-20 04:14:30 +00:00
|
|
|
#[get("/", format = "application/foo")]
|
|
|
|
fn get_foo() -> &'static str { "get_foo" }
|
|
|
|
|
2024-03-22 03:24:53 +00:00
|
|
|
#[suppress(unknown_format)]
|
2018-09-20 04:14:30 +00:00
|
|
|
#[post("/", format = "application/foo")]
|
|
|
|
fn post_foo() -> &'static str { "post_foo" }
|
|
|
|
|
2024-03-22 03:24:53 +00:00
|
|
|
#[suppress(unknown_format)]
|
2018-10-05 04:44:42 +00:00
|
|
|
#[get("/", format = "bar/baz", rank = 2)]
|
2018-09-20 04:14:30 +00:00
|
|
|
fn get_bar_baz() -> &'static str { "get_bar_baz" }
|
|
|
|
|
2024-03-22 03:24:53 +00:00
|
|
|
#[suppress(unknown_format)]
|
2018-09-20 04:14:30 +00:00
|
|
|
#[put("/", format = "bar/baz")]
|
|
|
|
fn put_bar_baz() -> &'static str { "put_bar_baz" }
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_custom_formats() {
|
2021-04-08 08:07:52 +00:00
|
|
|
let rocket = rocket::build()
|
2018-09-20 04:14:30 +00:00
|
|
|
.mount("/", routes![get_foo, post_foo, get_bar_baz, put_bar_baz]);
|
|
|
|
|
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();
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2024-03-20 07:00:33 +00:00
|
|
|
let foo_a = Accept::new([MediaType::new("application", "foo")]);
|
2018-09-20 04:14:30 +00:00
|
|
|
let foo_ct = ContentType::new("application", "foo");
|
|
|
|
let bar_baz_ct = ContentType::new("bar", "baz");
|
2024-03-20 07:00:33 +00:00
|
|
|
let bar_baz_a = Accept::new([MediaType::new("bar", "baz")]);
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.get("/").header(foo_a).dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "get_foo");
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.post("/").header(foo_ct).dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "post_foo");
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.get("/").header(bar_baz_a).dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "get_bar_baz");
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.put("/").header(bar_baz_ct).dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "put_bar_baz");
|
2018-09-20 04:14:30 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.get("/").dispatch();
|
|
|
|
assert_eq!(response.into_string().unwrap(), "get_foo");
|
2018-10-05 04:44:42 +00:00
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.put("/").header(ContentType::HTML).dispatch();
|
2018-10-05 04:44:42 +00:00
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.post("/").header(ContentType::HTML).dispatch();
|
2018-09-20 04:14:30 +00:00
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
}
|