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
|
|
|
|
2020-06-19 13:01:10 +00:00
|
|
|
use tokio::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};
|
2020-06-19 13:01:10 +00:00
|
|
|
use rocket::response::ResponseBody;
|
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
|
|
|
|
2020-06-19 13:01:10 +00:00
|
|
|
async fn assert_empty_sized_body(body: &mut ResponseBody<'_>, expected_size: usize) {
|
|
|
|
let size = body.size().await.expect("sized body");
|
|
|
|
assert_eq!(size, expected_size);
|
|
|
|
|
|
|
|
let mut buffer = vec![];
|
|
|
|
body.as_reader().read_to_end(&mut buffer).await.unwrap();
|
|
|
|
assert_eq!(buffer.len(), 0);
|
2018-01-19 19:21:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|