Rocket/core/codegen/tests/responder.rs

110 lines
3.3 KiB
Rust
Raw Normal View History

use rocket::local::asynchronous::Client;
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,
},
#[response(status = 105)]
Fourth {
string: &'r str,
2019-06-13 01:59:25 +00:00
ct: rocket::http::ContentType,
},
}
#[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");
let local_req = client.get("/");
let req = local_req.inner();
let mut r = Foo::First("hello".into())
.respond_to(req)
.expect("response okay");
assert_eq!(r.status(), Status::Ok);
assert_eq!(r.content_type(), Some(ContentType::Plain));
assert_eq!(r.body_mut().to_string().await.unwrap(), "hello");
let mut r = Foo::Second("just a test".into())
.respond_to(req)
.expect("response okay");
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");
let mut r = Foo::Third { responder: "well, hi", ct: ContentType::JSON }
.respond_to(req)
.expect("response okay");
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");
let mut r = Foo::Fourth { string: "goodbye", ct: ContentType::JSON }
.respond_to(req)
.expect("response okay");
assert_eq!(r.status().code, 105);
assert_eq!(r.content_type(), Some(ContentType::JSON));
assert_eq!(r.body_mut().to_string().await.unwrap(), "goodbye");
}
#[derive(Responder)]
#[response(content_type = "plain")]
pub struct Bar<'r> {
responder: Foo<'r>,
other: ContentType,
third: Cookie<'static>,
#[response(ignore)]
_yet_another: String,
}
#[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");
let local_req = client.get("/");
let req = local_req.inner();
let mut r = Bar {
responder: Foo::Second("foo foo".into()),
other: ContentType::HTML,
third: Cookie::new("cookie", "here!"),
_yet_another: "uh..hi?".into()
}.respond_to(req).expect("response okay");
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!"));
}
#[derive(Responder)]
#[response(content_type = "application/x-custom")]
pub struct Baz {
responder: &'static str,
}
#[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");
let local_req = client.get("/");
let req = local_req.inner();
let mut r = Baz { responder: "just a custom" }
.respond_to(req)
.expect("response okay");
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");
}