Rocket/examples/tera_templates/src/tests.rs

67 lines
2.3 KiB
Rust
Raw Normal View History

2017-05-16 22:14:46 +00:00
use super::rocket;
use rocket::local::{Client, LocalResponse};
use rocket::http::Method::*;
use rocket::http::Status;
use rocket_contrib::Template;
macro_rules! dispatch {
($method:expr, $path:expr, $test_fn:expr) => ({
let client = Client::new(rocket()).unwrap();
$test_fn(&client, client.req($method, $path).dispatch());
2017-05-16 22:14:46 +00:00
})
}
#[test]
fn test_root() {
// Check that the redirect works.
for method in &[Get, Head] {
dispatch!(*method, "/", |_: &Client, mut response: LocalResponse| {
2017-05-16 22:14:46 +00:00
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"]);
});
}
// Check that other request methods are not accepted (and instead caught).
for method in &[Post, Put, Delete, Options, Trace, Connect, Patch] {
dispatch!(*method, "/", |client: &Client, mut response: LocalResponse| {
2017-05-16 22:14:46 +00:00
let mut map = ::std::collections::HashMap::new();
map.insert("path", "/");
let expected = Template::show(client.rocket(), "error/404", &map).unwrap();
2017-05-16 22:14:46 +00:00
assert_eq!(response.status(), Status::NotFound);
assert_eq!(response.body_string(), Some(expected));
});
}
}
#[test]
fn test_name() {
// Check that the /hello/<name> route works.
dispatch!(Get, "/hello/Jack", |client: &Client, mut response: LocalResponse| {
2017-05-16 22:14:46 +00:00
let context = super::TemplateContext {
name: "Jack".into(),
items: vec!["One", "Two", "Three"]
2017-05-16 22:14:46 +00:00
};
let expected = Template::show(client.rocket(), "index", &context).unwrap();
2017-05-16 22:14:46 +00:00
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.body_string(), Some(expected));
});
}
#[test]
fn test_404() {
// Check that the error catcher works.
dispatch!(Get, "/hello/", |client: &Client, mut response: LocalResponse| {
2017-05-16 22:14:46 +00:00
let mut map = ::std::collections::HashMap::new();
map.insert("path", "/hello/");
let expected = Template::show(client.rocket(), "error/404", &map).unwrap();
2017-05-16 22:14:46 +00:00
assert_eq!(response.status(), Status::NotFound);
assert_eq!(response.body_string(), Some(expected));
2017-05-16 22:14:46 +00:00
});
}