diff --git a/examples/handlebars_templates/src/tests.rs b/examples/handlebars_templates/src/tests.rs index 64567ba0..d2f7b765 100644 --- a/examples/handlebars_templates/src/tests.rs +++ b/examples/handlebars_templates/src/tests.rs @@ -3,6 +3,7 @@ use rocket::testing::MockRequest; use rocket::http::Method::*; use rocket::http::Status; use rocket::Response; +use rocket_contrib::Template; macro_rules! run_test { ($req:expr, $test_fn:expr) => ({ @@ -17,29 +18,31 @@ macro_rules! run_test { #[test] fn test_root() { // Check that the redirect works. - for method in [Get, Head].iter() { - let mut req = MockRequest::new(*method, "/"); + for method in vec![Get, Head].into_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, "/"); + for method in vec![Post, Put, Delete, Options, Trace, Connect, Patch].into_iter() { + let mut req = MockRequest::new(method, "/"); run_test!(req, |mut response: Response| { assert_eq!(response.status(), Status::NotFound); + let mut map = ::std::collections::HashMap::new(); + map.insert("path", "/"); + let expected = Template::render("error/404", &map).to_string(); + 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())); + assert_eq!(body_string, Some(expected)); }); } } @@ -51,8 +54,14 @@ fn test_name() { run_test!(req, |mut response: Response| { assert_eq!(response.status(), Status::Ok); + let context = super::TemplateContext { + name: "Jack".to_string(), + items: vec!["One", "Two", "Three"].iter().map(|s| s.to_string()).collect() + }; + let expected = Template::render("index", &context).to_string(); + 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())); + assert_eq!(body_string, Some(expected)); }); } @@ -63,7 +72,11 @@ fn test_404() { run_test!(req, |mut response: Response| { assert_eq!(response.status(), Status::NotFound); + let mut map = ::std::collections::HashMap::new(); + map.insert("path", "/hello/"); + let expected = Template::render("error/404", &map).to_string(); + 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())); + assert_eq!(body_string, Some(expected)); }); }