Rocket/lib/tests/head_handling.rs

77 lines
2.0 KiB
Rust
Raw Normal View History

2016-12-16 13:17:16 +00:00
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate rocket;
use rocket::response::{status, content};
#[get("/empty")]
fn empty() -> status::NoContent {
status::NoContent
}
#[get("/")]
fn index() -> &'static str {
"Hello, world!"
}
#[head("/other")]
fn other() -> content::JSON<()> {
content::JSON(())
}
#[cfg(feature = "testing")]
mod tests {
use super::*;
2016-12-16 13:17:16 +00:00
use rocket::Route;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
use rocket::http::{Status, ContentType};
use rocket::response::Body;
2016-12-16 13:17:16 +00:00
fn routes() -> Vec<Route> {
routes![index, empty, other]
}
2016-12-16 13:17:16 +00:00
#[test]
fn auto_head() {
let rocket = rocket::ignite().mount("/", routes());
2016-12-16 13:17:16 +00:00
let mut req = MockRequest::new(Head, "/");
let mut response = req.dispatch_with(&rocket);
2016-12-16 13:17:16 +00:00
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-16 13:17:16 +00:00
assert_eq!(body.into_string(), Some("".to_string()));
} else {
panic!("Expected an empty body!")
}
2016-12-16 13:17:16 +00:00
let content_type: Vec<_> = response.header_values("Content-Type").collect();
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);
}
2016-12-16 13:17:16 +00:00
#[test]
fn user_head() {
let rocket = rocket::ignite().mount("/", routes());
let mut req = MockRequest::new(Head, "/other");
let response = req.dispatch_with(&rocket);
2016-12-16 13:17:16 +00:00
assert_eq!(response.status(), Status::Ok);
2016-12-16 13:17:16 +00:00
let content_type: Vec<_> = response.header_values("Content-Type").collect();
assert_eq!(content_type, vec![ContentType::JSON.to_string()]);
}
2016-12-16 13:17:16 +00:00
}