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
|
|
|
|
2017-01-05 20:51:00 +00:00
|
|
|
use rocket::Route;
|
2020-06-22 11:54:34 +00:00
|
|
|
use rocket::local::asynchronous::Client;
|
2017-01-05 20:51:00 +00:00
|
|
|
use rocket::http::{Status, ContentType};
|
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-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();
|
2020-06-22 11:54:34 +00:00
|
|
|
let response = client.head("/").dispatch().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()]);
|
2020-06-22 11:54:34 +00:00
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert_eq!(response.body().unwrap().known_size(), Some(13));
|
|
|
|
assert!(response.into_bytes().await.unwrap().is_empty());
|
2017-01-05 20:51:00 +00:00
|
|
|
|
2020-06-22 11:54:34 +00:00
|
|
|
let response = client.head("/empty").dispatch().await;
|
2017-01-05 20:51:00 +00:00
|
|
|
assert_eq!(response.status(), Status::NoContent);
|
2020-06-22 11:54:34 +00:00
|
|
|
assert!(response.into_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();
|
2020-06-22 11:54:34 +00:00
|
|
|
let response = client.head("/other").dispatch().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()]);
|
2020-06-22 11:54:34 +00:00
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert_eq!(response.body().unwrap().known_size(), Some(17));
|
|
|
|
assert!(response.into_bytes().await.unwrap().is_empty());
|
2017-01-05 20:51:00 +00:00
|
|
|
}
|
2016-12-16 13:17:16 +00:00
|
|
|
}
|