Fixup Tera templates example for master.

This commit is contained in:
Sergio Benitez 2017-11-17 12:23:51 -08:00
parent 7bda1b527b
commit 0d2585051d
3 changed files with 11 additions and 47 deletions

View File

@ -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<String>
items: Vec<&'static str>
}
#[get("/")]
@ -25,17 +27,13 @@ fn index() -> Redirect {
#[get("/hello/<name>")]
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() {

View File

@ -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/<name> 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));
});
}

View File

@ -1,7 +1,6 @@
{% extends "base" %}
{% block content %}
<h1>Hi {{name}}</h1>
<h3>Here are your items:</h3>
<ul>
@ -11,5 +10,4 @@
</ul>
<p>Try going to <a href="/hello/YourName">/hello/YourName</a></p>
{% endblock content %}