Add tests for handlebars_templates example.

This commit is contained in:
FliegendeWurst 2017-01-03 12:12:27 +01:00 committed by Sergio Benitez
parent dec585dbd4
commit 99a17b42ae
3 changed files with 88 additions and 1 deletions

View File

@ -15,3 +15,6 @@ serde_json = "*"
path = "../../contrib"
default-features = false
features = ["handlebars_templates"]
[dev-dependencies]
rocket = { path = "../../lib", features = ["testing"] }

View File

@ -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;

View File

@ -0,0 +1,82 @@
use rocket;
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) => ({
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] {
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();
assert_eq!(location_headers, vec!["/hello/Unknown"]);
});
}
// Check that other request methods are not accepted (and instead caught).
for method in &[Post, Put, Delete, Options, Trace, Connect, Patch] {
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(expected));
});
}
}
#[test]
fn test_name() {
// Check that the /hello/<name> route works.
let mut req = MockRequest::new(Get, "/hello/Jack");
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(expected));
});
}
#[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 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(expected));
});
}