2019-08-20 23:53:00 +00:00
|
|
|
#![feature(proc_macro_hygiene)]
|
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-12-11 00:34:23 +00:00
|
|
|
use tokio::io::{AsyncRead, 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-09-22 22:48:08 +00:00
|
|
|
async fn assert_empty_sized_body<T: 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-08-24 17:27:10 +00:00
|
|
|
body.read_to_end(&mut buffer).await.unwrap();
|
2018-01-19 19:21:56 +00:00
|
|
|
assert_eq!(size, expected_size);
|
2019-06-30 16:45:17 +00:00
|
|
|
assert_eq!(buffer.len(), 0);
|
2018-01-19 19:21:56 +00:00
|
|
|
}
|
|
|
|
_ => panic!("Expected a sized body.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn auto_head() {
|
2020-06-14 15:57:53 +00:00
|
|
|
let client = Client::new(rocket::ignite().mount("/", routes())).await.unwrap();
|
2019-08-24 17:27:10 +00:00
|
|
|
let mut response = client.head("/").dispatch().await;
|
2017-01-05 20:51:00 +00:00
|
|
|
assert_eq!(response.status(), Status::Ok);
|
2019-08-24 17:27:10 +00:00
|
|
|
assert_empty_sized_body(response.body().unwrap(), 13).await;
|
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()]);
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
let mut response = client.head("/empty").dispatch().await;
|
2017-01-05 20:51:00 +00:00
|
|
|
assert_eq!(response.status(), Status::NoContent);
|
2019-08-24 17:27:10 +00:00
|
|
|
assert!(response.body_bytes().await.is_none());
|
2017-01-05 20:51:00 +00:00
|
|
|
}
|
2016-12-16 13:17:16 +00:00
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn user_head() {
|
2020-06-14 15:57:53 +00:00
|
|
|
let client = Client::new(rocket::ignite().mount("/", routes())).await.unwrap();
|
2019-08-24 17:27:10 +00:00
|
|
|
let mut response = client.head("/other").dispatch().await;
|
2018-01-19 19:21:56 +00:00
|
|
|
assert_eq!(response.status(), Status::Ok);
|
2019-08-24 17:27:10 +00:00
|
|
|
assert_empty_sized_body(response.body().unwrap(), 17).await;
|
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
|
|
|
}
|