2016-12-29 20:06:31 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use super::rocket;
|
2017-06-06 20:41:04 +00:00
|
|
|
use rocket::local::Client;
|
2016-12-29 20:06:31 +00:00
|
|
|
use rocket::http::*;
|
2018-10-07 00:24:11 +00:00
|
|
|
use rocket_contrib::templates::Template;
|
2016-12-29 20:06:31 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_submit() {
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
let response = client.post("/submit")
|
2016-12-29 20:06:31 +00:00
|
|
|
.header(ContentType::Form)
|
2017-06-06 20:41:04 +00:00
|
|
|
.body("message=Hello from Rocket!")
|
|
|
|
.dispatch();
|
|
|
|
|
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) {
|
2016-12-29 20:06:31 +00:00
|
|
|
// Attach a cookie if one is given.
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
let mut response = match optional_cookie {
|
|
|
|
Some(cookie) => client.get("/").cookie(cookie).dispatch(),
|
|
|
|
None => client.get("/").dispatch(),
|
|
|
|
};
|
2016-12-29 20:06:31 +00:00
|
|
|
|
|
|
|
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() {
|
2017-12-29 03:53:15 +00:00
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2017-06-06 20:41:04 +00:00
|
|
|
// Render the template with an empty context.
|
2016-12-29 20:06:31 +00:00
|
|
|
let mut context: HashMap<&str, &str> = HashMap::new();
|
2017-12-29 03:53:15 +00:00
|
|
|
let template = Template::show(client.rocket(), "index", &context).unwrap();
|
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!");
|
2017-12-29 03:53:15 +00:00
|
|
|
let template = Template::show(client.rocket(), "index", &context).unwrap();
|
2017-06-06 20:41:04 +00:00
|
|
|
test_body(Some(Cookie::new("message", "Hello from Rocket!")), template);
|
2016-12-29 20:06:31 +00:00
|
|
|
}
|