2018-10-06 04:56:46 +00:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
2016-12-16 13:17:16 +00:00
|
|
|
|
2018-06-28 15:55:15 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
2016-12-16 13:17:16 +00:00
|
|
|
|
|
|
|
use rocket::response::{status, content};
|
|
|
|
|
|
|
|
#[get("/empty")]
|
|
|
|
fn empty() -> status::NoContent {
|
|
|
|
status::NoContent
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
fn index() -> &'static str {
|
|
|
|
"Hello, world!"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[head("/other")]
|
2018-01-19 19:21:56 +00:00
|
|
|
fn other() -> content::Json<&'static str> {
|
|
|
|
content::Json("{ 'hi': 'hello' }")
|
2016-12-16 13:17:16 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 19:21:56 +00:00
|
|
|
mod head_handling_tests {
|
2017-01-05 20:51:00 +00:00
|
|
|
use super::*;
|
2016-12-16 13:17:16 +00:00
|
|
|
|
2018-01-19 19:21:56 +00:00
|
|
|
use std::io::Read;
|
|
|
|
|
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
|
|
|
|
2018-01-19 19:21:56 +00:00
|
|
|
fn assert_empty_sized_body<T: Read>(body: Body<T>, expected_size: u64) {
|
|
|
|
match body {
|
|
|
|
Body::Sized(mut body, size) => {
|
|
|
|
let mut buffer = vec![];
|
|
|
|
let n = body.read_to_end(&mut buffer).unwrap();
|
|
|
|
assert_eq!(size, expected_size);
|
|
|
|
assert_eq!(n, 0);
|
|
|
|
}
|
|
|
|
_ => panic!("Expected a sized body.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2018-01-19 19:21:56 +00:00
|
|
|
assert_empty_sized_body(response.body().unwrap(), 13);
|
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()]);
|
|
|
|
|
2018-01-19 19:21:56 +00:00
|
|
|
let mut response = client.head("/empty").dispatch();
|
2017-01-05 20:51:00 +00:00
|
|
|
assert_eq!(response.status(), Status::NoContent);
|
2018-01-19 19:21:56 +00:00
|
|
|
assert!(response.body_bytes().is_none());
|
2017-01-05 20:51:00 +00:00
|
|
|
}
|
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();
|
2018-01-19 19:21:56 +00:00
|
|
|
let mut response = client.head("/other").dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert_empty_sized_body(response.body().unwrap(), 17);
|
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::JSON.to_string()]);
|
|
|
|
}
|
2016-12-16 13:17:16 +00:00
|
|
|
}
|