2019-07-13 20:55:09 +00:00
|
|
|
#![feature(proc_macro_hygiene, async_await)]
|
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
|
|
|
|
2018-10-31 10:51:45 +00:00
|
|
|
use rocket::{http::Status, response::content};
|
2016-12-16 13:17:16 +00:00
|
|
|
|
|
|
|
#[get("/empty")]
|
2018-10-31 10:51:45 +00:00
|
|
|
fn empty() -> Status {
|
|
|
|
Status::NoContent
|
2016-12-16 13:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
2019-07-13 20:55:09 +00:00
|
|
|
use futures::io::AsyncReadExt;
|
2018-01-19 19:21:56 +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
|
|
|
|
2019-07-13 20:55:09 +00:00
|
|
|
fn assert_empty_sized_body<T: futures::AsyncRead + Unpin>(body: Body<T>, expected_size: u64) {
|
2018-01-19 19:21:56 +00:00
|
|
|
match body {
|
|
|
|
Body::Sized(mut body, size) => {
|
|
|
|
let mut buffer = vec![];
|
2019-07-13 20:55:09 +00:00
|
|
|
futures::executor::block_on(async {
|
|
|
|
body.read_to_end(&mut buffer).await.unwrap();
|
|
|
|
});
|
2018-01-19 19:21:56 +00:00
|
|
|
assert_eq!(size, expected_size);
|
2019-07-13 20:55:09 +00:00
|
|
|
assert_eq!(buffer.len(), 0);
|
2018-01-19 19:21:56 +00:00
|
|
|
}
|
|
|
|
_ => 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);
|
2019-07-13 20:55:09 +00:00
|
|
|
assert!(response.body_bytes_wait().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
|
|
|
}
|