2016-12-29 20:06:31 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use super::rocket;
|
|
|
|
use rocket::testing::MockRequest;
|
|
|
|
use rocket::http::*;
|
|
|
|
use rocket_contrib::Template;
|
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
const TEMPLATE_ROOT: &'static str = "templates/";
|
|
|
|
|
2016-12-29 20:06:31 +00:00
|
|
|
#[test]
|
|
|
|
fn test_submit() {
|
2017-05-19 10:29:08 +00:00
|
|
|
let rocket = rocket();
|
2016-12-29 20:06:31 +00:00
|
|
|
let mut request = MockRequest::new(Method::Post, "/submit")
|
|
|
|
.header(ContentType::Form)
|
|
|
|
.body("message=Hello from Rocket!");
|
|
|
|
let response = request.dispatch_with(&rocket);
|
2017-04-14 08:21:06 +00:00
|
|
|
let cookie_headers: Vec<_> = response.headers().get("Set-Cookie").collect();
|
|
|
|
let location_headers: Vec<_> = response.headers().get("Location").collect();
|
2016-12-29 20:06:31 +00:00
|
|
|
|
|
|
|
assert_eq!(response.status(), Status::SeeOther);
|
|
|
|
assert_eq!(cookie_headers, vec!["message=Hello%20from%20Rocket!".to_string()]);
|
|
|
|
assert_eq!(location_headers, vec!["/".to_string()]);
|
|
|
|
}
|
|
|
|
|
2017-01-27 07:08:15 +00:00
|
|
|
fn test_body(optional_cookie: Option<Cookie<'static>>, expected_body: String) {
|
2017-05-19 10:29:08 +00:00
|
|
|
let rocket = rocket();
|
2016-12-29 20:06:31 +00:00
|
|
|
let mut request = MockRequest::new(Method::Get, "/");
|
|
|
|
|
|
|
|
// Attach a cookie if one is given.
|
|
|
|
if let Some(cookie) = optional_cookie {
|
|
|
|
request = request.cookie(cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut response = request.dispatch_with(&rocket);
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
2017-04-14 08:59:28 +00:00
|
|
|
assert_eq!(response.body_string(), Some(expected_body));
|
2016-12-29 20:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_index() {
|
|
|
|
// Render the template with an empty context to test against.
|
|
|
|
let mut context: HashMap<&str, &str> = HashMap::new();
|
2017-05-19 10:29:08 +00:00
|
|
|
let template = Template::show(TEMPLATE_ROOT, "index", &context).unwrap();
|
2016-12-29 20:06:31 +00:00
|
|
|
|
|
|
|
// Test the route without sending the "message" cookie.
|
2017-05-19 10:29:08 +00:00
|
|
|
test_body(None, template);
|
2016-12-29 20:06:31 +00:00
|
|
|
|
|
|
|
// Render the template with a context that contains the message.
|
|
|
|
context.insert("message", "Hello from Rocket!");
|
|
|
|
|
|
|
|
// Test the route with the "message" cookie.
|
2017-01-27 07:08:15 +00:00
|
|
|
let cookie = Cookie::new("message", "Hello from Rocket!");
|
2017-05-19 10:29:08 +00:00
|
|
|
let template = Template::show(TEMPLATE_ROOT, "index", &context).unwrap();
|
|
|
|
test_body(Some(cookie), template);
|
2016-12-29 20:06:31 +00:00
|
|
|
}
|