forked from rwf2/Rocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.rs
71 lines (61 loc) · 2.36 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use super::{rocket, TemplateContext};
use rocket::local::blocking::Client;
use rocket::http::Method::*;
use rocket::http::Status;
use rocket_contrib::templates::Template;
macro_rules! dispatch {
($method:expr, $path:expr, |$client:ident, $response:ident| $body:expr) => ({
let $client = Client::new(rocket()).unwrap();
let $response = $client.req($method, $path).dispatch();
$body
})
}
#[test]
fn test_root() {
// Check that the redirect works.
for method in &[Get, Head] {
dispatch!(*method, "/", |client, 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"]);
});
}
// Check that other request methods are not accepted (and instead caught).
for method in &[Post, Put, Delete, Options, Trace, Connect, Patch] {
dispatch!(*method, "/", |client, response| {
let mut map = std::collections::HashMap::new();
map.insert("path", "/");
//let expected = Template::show(client.cargo(), "error/404", &map).unwrap();
assert_eq!(response.status(), Status::MethodNotAllowed);
// FIND A MATCHING TEMPLATE TO HTTP 405 HERE
//assert_eq!(response.body_string(), Some(expected));
});
}
}
#[test]
fn test_name() {
// Check that the /hello/<name> route works.
dispatch!(Get, "/hello/Jack%20Daniels", |client, response| {
let context = TemplateContext {
title: "Hello",
name: Some("Jack Daniels".into()),
items: vec!["One", "Two", "Three"],
parent: "layout",
};
let expected = Template::show(client.cargo(), "index", &context).unwrap();
assert_eq!(response.status(), Status::Ok);
assert_eq!(response.into_string(), Some(expected));
});
}
#[test]
fn test_404() {
// Check that the error catcher works.
dispatch!(Get, "/hello/", |client, response| {
let mut map = std::collections::HashMap::new();
map.insert("path", "/hello/");
let expected = Template::show(client.cargo(), "error/404", &map).unwrap();
assert_eq!(response.status(), Status::NotFound);
assert_eq!(response.into_string(), Some(expected));
});
}