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(())
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:51:00 +00:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2016-12-16 13:17:16 +00:00
|
|
|
|
2017-01-05 20:51:00 +00:00
|
|
|
use rocket::Route;
|
2017-06-06 20:41:04 +00:00
|
|
|
use rocket::local::Client;
|
2017-01-05 20:51:00 +00:00
|
|
|
use rocket::http::{Status, ContentType};
|
|
|
|
use rocket::response::Body;
|
2016-12-16 13:17:16 +00:00
|
|
|
|
2017-01-05 20:51:00 +00:00
|
|
|
fn routes() -> Vec<Route> {
|
|
|
|
routes![index, empty, other]
|
|
|
|
}
|
2016-12-16 13:17:16 +00:00
|
|
|
|
2017-01-05 20:51:00 +00:00
|
|
|
#[test]
|
|
|
|
fn auto_head() {
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = Client::new(rocket::ignite().mount("/", routes())).unwrap();
|
|
|
|
let mut response = client.head("/").dispatch();
|
2017-01-05 20:51:00 +00:00
|
|
|
assert_eq!(response.status(), Status::Ok);
|
2017-06-06 20:41:04 +00:00
|
|
|
|
2017-01-05 20:51:00 +00:00
|
|
|
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
|
|
|
|
2017-01-05 20:51:00 +00:00
|
|
|
assert_eq!(body.into_string(), Some("".to_string()));
|
|
|
|
} else {
|
2017-06-06 20:41:04 +00:00
|
|
|
panic!("Expected a non-empty body!")
|
2017-01-05 20:51:00 +00:00
|
|
|
}
|
2016-12-16 13:17:16 +00:00
|
|
|
|
2017-04-14 08:21:06 +00:00
|
|
|
let content_type: Vec<_> = response.headers().get("Content-Type").collect();
|
2017-01-05 20:51:00 +00:00
|
|
|
assert_eq!(content_type, vec![ContentType::Plain.to_string()]);
|
|
|
|
|
2017-06-06 20:41:04 +00:00
|
|
|
let response = client.head("empty").dispatch();
|
2017-01-05 20:51:00 +00:00
|
|
|
assert_eq!(response.status(), Status::NoContent);
|
|
|
|
}
|
2016-12-16 13:17:16 +00:00
|
|
|
|
2017-01-05 20:51:00 +00:00
|
|
|
#[test]
|
|
|
|
fn user_head() {
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = Client::new(rocket::ignite().mount("/", routes())).unwrap();
|
|
|
|
let response = client.head("/other").dispatch();
|
2016-12-16 13:17:16 +00:00
|
|
|
|
2017-04-14 08:21:06 +00:00
|
|
|
let content_type: Vec<_> = response.headers().get("Content-Type").collect();
|
2017-06-06 20:41:04 +00:00
|
|
|
assert_eq!(response.status(), Status::Ok);
|
2017-01-05 20:51:00 +00:00
|
|
|
assert_eq!(content_type, vec![ContentType::JSON.to_string()]);
|
|
|
|
}
|
2016-12-16 13:17:16 +00:00
|
|
|
}
|