From 0d2585051dd256dca9078d4e6f756e4c5c534112 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Fri, 17 Nov 2017 12:23:51 -0800 Subject: [PATCH] Fixup Tera templates example for master. --- examples/tera_templates/src/main.rs | 18 ++++----- examples/tera_templates/src/tests.rs | 38 ++----------------- .../tera_templates/templates/index.html.tera | 2 - 3 files changed, 11 insertions(+), 47 deletions(-) diff --git a/examples/tera_templates/src/main.rs b/examples/tera_templates/src/main.rs index 1f592577..66961dc7 100644 --- a/examples/tera_templates/src/main.rs +++ b/examples/tera_templates/src/main.rs @@ -1,4 +1,4 @@ -#![feature(plugin)] +#![feature(plugin, decl_macro)] #![plugin(rocket_codegen)] extern crate rocket_contrib; @@ -8,6 +8,8 @@ extern crate serde_json; #[cfg(test)] mod tests; +use std::collections::HashMap; + use rocket::Request; use rocket::response::Redirect; use rocket_contrib::Template; @@ -15,7 +17,7 @@ use rocket_contrib::Template; #[derive(Serialize)] struct TemplateContext { name: String, - items: Vec + items: Vec<&'static str> } #[get("/")] @@ -25,17 +27,13 @@ fn index() -> Redirect { #[get("/hello/")] fn get(name: String) -> Template { - let context = TemplateContext { - name: name, - items: vec!["One".to_owned(), "Two".to_owned(), "Three".to_owned()] - }; - + let context = TemplateContext { name, items: vec!["One", "Two", "Three"] }; Template::render("index", &context) } -#[error(404)] +#[catch(404)] fn not_found(req: &Request) -> Template { - let mut map = std::collections::HashMap::new(); + let mut map = HashMap::new(); map.insert("path", req.uri().as_str()); Template::render("error/404", &map) } @@ -44,7 +42,7 @@ fn rocket() -> rocket::Rocket { rocket::ignite() .mount("/", routes![index, get]) .attach(Template::fairing()) - .catch(errors![not_found]) + .catch(catchers![not_found]) } fn main() { diff --git a/examples/tera_templates/src/tests.rs b/examples/tera_templates/src/tests.rs index 22cb3566..373bcc2d 100644 --- a/examples/tera_templates/src/tests.rs +++ b/examples/tera_templates/src/tests.rs @@ -1,4 +1,3 @@ -use rocket; use super::rocket; use rocket::local::{Client, LocalResponse}; use rocket::http::Method::*; @@ -22,21 +21,6 @@ fn test_root() { assert_eq!(response.status(), Status::SeeOther); assert!(response.body().is_none()); - let location: Vec<_> = response.headers().get("Location").collect(); - assert_eq!(location, vec!["/hello/Unknown"]); - - let 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.headers().get("Location").collect(); - assert_eq!(location_headers, vec!["/hello/Unknown"]); - - dispatch!(*method, "/", |mut response: Response| { - assert_eq!(response.status(), Status::SeeOther); - assert!(response.body().is_none()); - let location: Vec<_> = response.headers().get("Location").collect(); assert_eq!(location, vec!["/hello/Unknown"]); }); @@ -51,15 +35,6 @@ fn test_root() { assert_eq!(response.status(), Status::NotFound); assert_eq!(response.body_string(), Some(expected)); - - let req = MockRequest::new(*method, "/"); - run_test!(req, |mut response: Response| { - let mut map = ::std::collections::HashMap::new(); - map.insert("path", "/"); - let expected = Template::show(TEMPLATE_ROOT, "error/404", &map).unwrap(); - - assert_eq!(response.status(), Status::NotFound); - assert_eq!(response.body_string(), Some(expected_body)); }); } } @@ -69,13 +44,12 @@ fn test_name() { // Check that the /hello/ route works. dispatch!(Get, "/hello/Jack", |mut response: LocalResponse| { let context = super::TemplateContext { - name: "Jack".to_string(), - items: vec!["One", "Two", "Three"].iter().map(|s| s.to_string()).collect() + name: "Jack".into(), + items: vec!["One", "Two", "Three"] }; let expected = Template::show(TEMPLATE_ROOT, "index", &context).unwrap(); assert_eq!(response.status(), Status::Ok); - let expected = Template::render("index", &context).to_string(); assert_eq!(response.body_string(), Some(expected)); }); } @@ -89,12 +63,6 @@ fn test_404() { let expected = Template::show(TEMPLATE_ROOT, "error/404", &map).unwrap(); assert_eq!(response.status(), Status::NotFound); - - dispatch!(Get, "/hello/", |mut response: Response| { - let mut map = ::std::collections::HashMap::new(); - map.insert("path", "/hello/"); - - let expected = Template::show(TEMPLATE_ROOT, "error/404", &map).unwrap(); - assert_eq!(response.status(), Status::NotFound); + assert_eq!(response.body_string(), Some(expected)); }); } diff --git a/examples/tera_templates/templates/index.html.tera b/examples/tera_templates/templates/index.html.tera index bc3cea18..d0c46826 100644 --- a/examples/tera_templates/templates/index.html.tera +++ b/examples/tera_templates/templates/index.html.tera @@ -1,7 +1,6 @@ {% extends "base" %} {% block content %} -

Hi {{name}}

Here are your items:

    @@ -11,5 +10,4 @@

Try going to /hello/YourName

- {% endblock content %}