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();
|
2017-12-29 03:53:15 +00:00
|
|
|
$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] {
|
2017-12-29 03:53:15 +00:00
|
|
|
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] {
|
2017-12-29 03:53:15 +00:00
|
|
|
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", "/");
|
2017-12-29 03:53:15 +00:00
|
|
|
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.
|
2017-12-29 03:53:15 +00:00
|
|
|
dispatch!(Get, "/hello/Jack", |client: &Client, mut response: LocalResponse| {
|
2017-05-16 22:14:46 +00:00
|
|
|
let context = super::TemplateContext {
|
2017-11-17 20:23:51 +00:00
|
|
|
name: "Jack".into(),
|
|
|
|
items: vec!["One", "Two", "Three"]
|
2017-05-16 22:14:46 +00:00
|
|
|
};
|
|
|
|
|
2017-12-29 03:53:15 +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.
|
2017-12-29 03:53:15 +00:00
|
|
|
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/");
|
|
|
|
|
2017-12-29 03:53:15 +00:00
|
|
|
let expected = Template::show(client.rocket(), "error/404", &map).unwrap();
|
2017-05-16 22:14:46 +00:00
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
2017-11-17 20:23:51 +00:00
|
|
|
assert_eq!(response.body_string(), Some(expected));
|
2017-05-16 22:14:46 +00:00
|
|
|
});
|
|
|
|
}
|