Rocket/lib/tests/head_handling.rs

73 lines
1.8 KiB
Rust
Raw Normal View History

2016-12-16 13:17:16 +00:00
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate rocket;
use rocket::Route;
use rocket::response::{status, content};
use rocket::http::ContentType;
#[get("/empty")]
fn empty() -> status::NoContent {
status::NoContent
}
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[head("/other")]
fn other() -> content::JSON<()> {
content::JSON(())
}
fn routes() -> Vec<Route> {
routes![index, empty, other]
}
use rocket::testing::MockRequest;
use rocket::http::Method::*;
use rocket::http::Status;
use rocket::response::Body;
#[test]
fn auto_head() {
let rocket = rocket::ignite().mount("/", routes());
let mut req = MockRequest::new(Head, "/");
let mut response = req.dispatch_with(&rocket);
assert_eq!(response.status(), Status::Ok);
if let Some(body) = response.body() {
match body {
Body::Sized(_, n) => assert_eq!(n, "Hello, world!".len() as u64),
_ => panic!("Expected a sized body!")
}
2016-12-17 17:18:30 +00:00
assert_eq!(body.into_string(), Some("".to_string()));
2016-12-16 13:17:16 +00:00
} else {
panic!("Expected an empty body!")
}
2016-12-20 07:29:20 +00:00
let content_type: Vec<_> = response.header_values("Content-Type").collect();
2016-12-16 13:17:16 +00:00
assert_eq!(content_type, vec![ContentType::Plain.to_string()]);
let mut req = MockRequest::new(Head, "/empty");
let response = req.dispatch_with(&rocket);
assert_eq!(response.status(), Status::NoContent);
}
#[test]
fn user_head() {
let rocket = rocket::ignite().mount("/", routes());
let mut req = MockRequest::new(Head, "/other");
let response = req.dispatch_with(&rocket);
assert_eq!(response.status(), Status::Ok);
2016-12-20 07:29:20 +00:00
let content_type: Vec<_> = response.header_values("Content-Type").collect();
2016-12-16 13:17:16 +00:00
assert_eq!(content_type, vec![ContentType::JSON.to_string()]);
}