2020-06-22 11:54:34 +00:00
|
|
|
use rocket::local::asynchronous::Client;
|
2018-08-07 02:58:07 +00:00
|
|
|
use rocket::http::{Status, ContentType, Cookie};
|
2021-05-26 09:26:11 +00:00
|
|
|
use rocket::response::Responder;
|
2021-07-01 13:02:40 +00:00
|
|
|
use rocket::serde::json::Json;
|
|
|
|
use rocket::http::Accept;
|
2018-08-07 02:58:07 +00:00
|
|
|
|
|
|
|
#[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 11:54:06 +00:00
|
|
|
assert_eq!(r.status().code, 105);
|
2021-04-28 08:51:51 +00:00
|
|
|
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
|
|
|
}
|
2021-05-26 09:26:11 +00:00
|
|
|
|
2021-06-29 10:20:40 +00:00
|
|
|
// The bounds `Json<T>: Responder, E: Responder` will be added to the generated
|
2021-05-26 09:26:11 +00:00
|
|
|
// implementation. This would fail to compile otherwise.
|
|
|
|
#[derive(Responder)]
|
2021-07-01 13:02:40 +00:00
|
|
|
enum MyResult<'a, T, E, H1, H2> {
|
2021-05-26 09:26:11 +00:00
|
|
|
Ok(Json<T>),
|
|
|
|
#[response(status = 404)]
|
2021-07-01 13:02:40 +00:00
|
|
|
Err(E, H1, H2),
|
2021-05-26 09:26:11 +00:00
|
|
|
#[response(status = 500)]
|
|
|
|
Other(&'a str),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rocket::async_test]
|
|
|
|
async fn generic_responder() {
|
|
|
|
let client = Client::debug_with(vec![]).await.expect("valid rocket");
|
|
|
|
let local_req = client.get("/");
|
|
|
|
let req = local_req.inner();
|
|
|
|
|
2024-05-20 18:39:14 +00:00
|
|
|
let v: MyResult<'_, _, (), ContentType, Cookie<'static>> = MyResult::Ok(Json("hi"));
|
2021-05-26 09:26:11 +00:00
|
|
|
let mut r = v.respond_to(req).unwrap();
|
|
|
|
assert_eq!(r.status(), Status::Ok);
|
|
|
|
assert_eq!(r.content_type().unwrap(), ContentType::JSON);
|
|
|
|
assert_eq!(r.body_mut().to_string().await.unwrap(), "\"hi\"");
|
|
|
|
|
2024-05-20 18:39:14 +00:00
|
|
|
let bytes = &[7, 13, 23];
|
|
|
|
let v: MyResult<'_, (), &[u8], _, _> = MyResult::Err(bytes, ContentType::JPEG, Accept::Text);
|
2021-05-26 09:26:11 +00:00
|
|
|
let mut r = v.respond_to(req).unwrap();
|
|
|
|
assert_eq!(r.status(), Status::NotFound);
|
|
|
|
assert_eq!(r.content_type().unwrap(), ContentType::JPEG);
|
2024-05-20 18:39:14 +00:00
|
|
|
assert_eq!(r.body_mut().to_bytes().await.unwrap(), bytes);
|
2021-05-26 09:26:11 +00:00
|
|
|
|
2024-05-20 18:39:14 +00:00
|
|
|
let v: MyResult<'_, (), &[u8], ContentType, Accept> = MyResult::Other("beep beep");
|
2021-05-26 09:26:11 +00:00
|
|
|
let mut r = v.respond_to(req).unwrap();
|
|
|
|
assert_eq!(r.status(), Status::InternalServerError);
|
|
|
|
assert_eq!(r.content_type().unwrap(), ContentType::Text);
|
|
|
|
assert_eq!(r.body_mut().to_string().await.unwrap(), "beep beep");
|
|
|
|
}
|