2020-06-22 11:54:34 +00:00
|
|
|
use rocket::local::asynchronous::Client;
|
2018-08-07 02:58:07 +00:00
|
|
|
use rocket::response::Responder;
|
|
|
|
use rocket::http::{Status, ContentType, Cookie};
|
|
|
|
|
|
|
|
#[derive(Responder)]
|
|
|
|
pub enum Foo<'r> {
|
|
|
|
First(String),
|
|
|
|
#[response(status = 500)]
|
|
|
|
Second(Vec<u8>),
|
|
|
|
#[response(status = 404, content_type = "html")]
|
|
|
|
Third {
|
|
|
|
responder: &'r str,
|
2019-06-13 01:59:25 +00:00
|
|
|
ct: rocket::http::ContentType,
|
2018-08-07 02:58:07 +00:00
|
|
|
},
|
|
|
|
#[response(status = 105)]
|
|
|
|
Fourth {
|
|
|
|
string: &'r str,
|
2019-06-13 01:59:25 +00:00
|
|
|
ct: rocket::http::ContentType,
|
2018-08-07 02:58:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn responder_foo() {
|
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_with(vec![]).await.expect("valid rocket");
|
2018-08-07 02:58:07 +00:00
|
|
|
let local_req = client.get("/");
|
|
|
|
let req = local_req.inner();
|
|
|
|
|
2021-04-28 08:51:51 +00:00
|
|
|
let mut r = Foo::First("hello".into())
|
2020-06-19 13:01:10 +00:00
|
|
|
.respond_to(req)
|
2018-08-07 02:58:07 +00:00
|
|
|
.expect("response okay");
|
|
|
|
|
2021-04-28 08:51:51 +00:00
|
|
|
assert_eq!(r.status(), Status::Ok);
|
|
|
|
assert_eq!(r.content_type(), Some(ContentType::Plain));
|
|
|
|
assert_eq!(r.body_mut().to_string().await.unwrap(), "hello");
|
2018-08-07 02:58:07 +00:00
|
|
|
|
2021-04-28 08:51:51 +00:00
|
|
|
let mut r = Foo::Second("just a test".into())
|
2020-06-19 13:01:10 +00:00
|
|
|
.respond_to(req)
|
2018-08-07 02:58:07 +00:00
|
|
|
.expect("response okay");
|
|
|
|
|
2021-04-28 08:51:51 +00:00
|
|
|
assert_eq!(r.status(), Status::InternalServerError);
|
|
|
|
assert_eq!(r.content_type(), Some(ContentType::Binary));
|
|
|
|
assert_eq!(r.body_mut().to_string().await.unwrap(), "just a test");
|
2018-08-07 02:58:07 +00:00
|
|
|
|
2021-04-28 08:51:51 +00:00
|
|
|
let mut r = Foo::Third { responder: "well, hi", ct: ContentType::JSON }
|
2020-06-19 13:01:10 +00:00
|
|
|
.respond_to(req)
|
2018-08-07 02:58:07 +00:00
|
|
|
.expect("response okay");
|
|
|
|
|
2021-04-28 08:51:51 +00:00
|
|
|
assert_eq!(r.status(), Status::NotFound);
|
|
|
|
assert_eq!(r.content_type(), Some(ContentType::HTML));
|
|
|
|
assert_eq!(r.body_mut().to_string().await.unwrap(), "well, hi");
|
2018-08-07 02:58:07 +00:00
|
|
|
|
2021-04-28 08:51:51 +00:00
|
|
|
let mut r = Foo::Fourth { string: "goodbye", ct: ContentType::JSON }
|
2020-06-19 13:01:10 +00:00
|
|
|
.respond_to(req)
|
2018-08-07 02:58:07 +00:00
|
|
|
.expect("response okay");
|
|
|
|
|
2021-04-28 08:51:51 +00:00
|
|
|
assert_eq!(r.status(), Status::raw(105));
|
|
|
|
assert_eq!(r.content_type(), Some(ContentType::JSON));
|
|
|
|
assert_eq!(r.body_mut().to_string().await.unwrap(), "goodbye");
|
2018-08-07 02:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Responder)]
|
|
|
|
#[response(content_type = "plain")]
|
|
|
|
pub struct Bar<'r> {
|
|
|
|
responder: Foo<'r>,
|
|
|
|
other: ContentType,
|
|
|
|
third: Cookie<'static>,
|
|
|
|
#[response(ignore)]
|
|
|
|
_yet_another: String,
|
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn responder_bar() {
|
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_with(vec![]).await.expect("valid rocket");
|
2018-08-07 02:58:07 +00:00
|
|
|
let local_req = client.get("/");
|
|
|
|
let req = local_req.inner();
|
|
|
|
|
2021-04-28 08:51:51 +00:00
|
|
|
let mut r = Bar {
|
2018-08-07 02:58:07 +00:00
|
|
|
responder: Foo::Second("foo foo".into()),
|
|
|
|
other: ContentType::HTML,
|
|
|
|
third: Cookie::new("cookie", "here!"),
|
|
|
|
_yet_another: "uh..hi?".into()
|
2020-06-19 13:01:10 +00:00
|
|
|
}.respond_to(req).expect("response okay");
|
2018-08-07 02:58:07 +00:00
|
|
|
|
2021-04-28 08:51:51 +00:00
|
|
|
assert_eq!(r.status(), Status::InternalServerError);
|
|
|
|
assert_eq!(r.content_type(), Some(ContentType::Plain));
|
|
|
|
assert_eq!(r.body_mut().to_string().await.unwrap(), "foo foo");
|
|
|
|
assert_eq!(r.headers().get_one("Set-Cookie"), Some("cookie=here!"));
|
2018-08-07 02:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Responder)]
|
|
|
|
#[response(content_type = "application/x-custom")]
|
|
|
|
pub struct Baz {
|
|
|
|
responder: &'static str,
|
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn responder_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_with(vec![]).await.expect("valid rocket");
|
2018-08-07 02:58:07 +00:00
|
|
|
let local_req = client.get("/");
|
|
|
|
let req = local_req.inner();
|
|
|
|
|
2021-04-28 08:51:51 +00:00
|
|
|
let mut r = Baz { responder: "just a custom" }
|
2020-06-19 13:01:10 +00:00
|
|
|
.respond_to(req)
|
2018-08-07 02:58:07 +00:00
|
|
|
.expect("response okay");
|
|
|
|
|
2021-04-28 08:51:51 +00:00
|
|
|
assert_eq!(r.status(), Status::Ok);
|
|
|
|
assert_eq!(r.content_type(), Some(ContentType::new("application", "x-custom")));
|
|
|
|
assert_eq!(r.body_mut().to_string().await.unwrap(), "just a custom");
|
2018-08-07 02:58:07 +00:00
|
|
|
}
|