diff --git a/examples/handlebars_templates/Cargo.toml b/examples/handlebars_templates/Cargo.toml index d11180a3..0c32a3d7 100644 --- a/examples/handlebars_templates/Cargo.toml +++ b/examples/handlebars_templates/Cargo.toml @@ -15,3 +15,6 @@ serde_json = "*" path = "../../contrib" default-features = false features = ["handlebars_templates"] + +[dev-dependencies] +rocket = { path = "../../lib", features = ["testing"] } diff --git a/examples/handlebars_templates/src/main.rs b/examples/handlebars_templates/src/main.rs index 7f5a7063..3ae0a3c4 100644 --- a/examples/handlebars_templates/src/main.rs +++ b/examples/handlebars_templates/src/main.rs @@ -6,7 +6,9 @@ extern crate rocket; extern crate serde_json; #[macro_use] extern crate serde_derive; -use rocket::{Request}; +#[cfg(test)] mod tests; + +use rocket::Request; use rocket::response::Redirect; use rocket_contrib::Template; diff --git a/examples/handlebars_templates/src/tests.rs b/examples/handlebars_templates/src/tests.rs new file mode 100644 index 00000000..64567ba0 --- /dev/null +++ b/examples/handlebars_templates/src/tests.rs @@ -0,0 +1,69 @@ +use rocket; +use rocket::testing::MockRequest; +use rocket::http::Method::*; +use rocket::http::Status; +use rocket::Response; + +macro_rules! run_test { + ($req:expr, $test_fn:expr) => ({ + let rocket = rocket::ignite() + .mount("/", routes![super::index, super::get]) + .catch(errors![super::not_found]); + + $test_fn($req.dispatch_with(&rocket)); + }) +} + +#[test] +fn test_root() { + // Check that the redirect works. + for method in [Get, Head].iter() { + let mut req = MockRequest::new(*method, "/"); + run_test!(req, |mut response: Response| { + assert_eq!(response.status(), Status::SeeOther); + + assert!(response.body().is_none()); + + let location_headers: Vec<_> = response.header_values("Location").collect(); + let content_length: Vec<_> = response.header_values("Content-Length").collect(); + + assert_eq!(location_headers, vec!["/hello/Unknown"]); + assert_eq!(content_length, vec!["0"]); + }); + } + + // Check that other request methods are not accepted (and instead caught). + for method in [Post, Put, Delete, Options, Trace, Connect, Patch].iter() { + let mut req = MockRequest::new(*method, "/"); + run_test!(req, |mut response: Response| { + assert_eq!(response.status(), Status::NotFound); + + let body_string = response.body().and_then(|body| body.into_string()); + assert_eq!(body_string, Some("\n\n \n \n 404\n \n \n

404: Hey! There\'s nothing here.

\n The page at / does not exist!\n \n\n".to_string())); + }); + } +} + +#[test] +fn test_name() { + // Check that the /hello/ route works. + let mut req = MockRequest::new(Get, "/hello/Jack"); + run_test!(req, |mut response: Response| { + assert_eq!(response.status(), Status::Ok); + + let body_string = response.body().and_then(|body| body.into_string()); + assert_eq!(body_string, Some("\n\n \n \n Handlebars Demo\n \n \n

Hi Jack

\n

Here are your items:

\n \n\n

Try going to /hello/YourName

\n \n\n".to_string())); + }); +} + +#[test] +fn test_404() { + // Check that the error catcher works. + let mut req = MockRequest::new(Get, "/hello/"); + run_test!(req, |mut response: Response| { + assert_eq!(response.status(), Status::NotFound); + + let body_string = response.body().and_then(|body| body.into_string()); + assert_eq!(body_string, Some("\n\n \n \n 404\n \n \n

404: Hey! There\'s nothing here.

\n The page at /hello/ does not exist!\n \n\n".to_string())); + }); +}