2017-07-14 05:54:34 +00:00
|
|
|
//! This crate tests that all the request headers are parsed correctly in the round trip
|
2018-10-31 02:25:10 +00:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
2018-11-21 04:17:40 +00:00
|
|
|
use hyper;
|
2017-07-14 05:54:34 +00:00
|
|
|
|
|
|
|
use std::ops::Deref;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
2017-07-17 06:28:54 +00:00
|
|
|
use rocket::http::Header;
|
2018-07-18 05:26:33 +00:00
|
|
|
use rocket::local::Client;
|
2019-03-12 06:17:52 +00:00
|
|
|
use rocket::response::Body;
|
|
|
|
use rocket::{get, routes};
|
2017-07-15 03:18:37 +00:00
|
|
|
use rocket_cors::headers::*;
|
2017-07-14 05:54:34 +00:00
|
|
|
|
|
|
|
#[get("/request_headers")]
|
|
|
|
fn request_headers(
|
|
|
|
origin: Origin,
|
|
|
|
method: AccessControlRequestMethod,
|
|
|
|
headers: AccessControlRequestHeaders,
|
|
|
|
) -> String {
|
|
|
|
let AccessControlRequestMethod(method) = method;
|
|
|
|
let AccessControlRequestHeaders(headers) = headers;
|
|
|
|
let mut headers = headers
|
|
|
|
.iter()
|
|
|
|
.map(|s| s.deref().to_string())
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
headers.sort();
|
|
|
|
format!("{}\n{}\n{}", origin, method, headers.join(", "))
|
|
|
|
}
|
|
|
|
|
2017-07-14 07:35:44 +00:00
|
|
|
/// Tests that all the request headers are parsed correcly in a HTTP request
|
2017-07-14 05:54:34 +00:00
|
|
|
#[test]
|
|
|
|
fn request_headers_round_trip_smoke_test() {
|
|
|
|
let rocket = rocket::ignite().mount("/", routes![request_headers]);
|
|
|
|
let client = Client::new(rocket).expect("A valid Rocket client");
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://foo.bar.xyz").unwrap());
|
2017-07-14 05:54:34 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
|
|
|
hyper::method::Method::Get,
|
|
|
|
));
|
|
|
|
let request_headers = hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("accept-language").unwrap(),
|
|
|
|
FromStr::from_str("X-Ping").unwrap(),
|
|
|
|
]);
|
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.get("/request_headers")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
let mut response = req.dispatch();
|
|
|
|
|
2017-07-17 06:28:54 +00:00
|
|
|
assert!(response.status().class().is_success());
|
2018-02-14 05:22:43 +00:00
|
|
|
let body_str = response
|
|
|
|
.body()
|
2019-03-12 06:17:52 +00:00
|
|
|
.and_then(Body::into_string)
|
2018-02-14 05:22:43 +00:00
|
|
|
.expect("Non-empty body");
|
2019-03-12 01:58:51 +00:00
|
|
|
let expected_body = r#"https://foo.bar.xyz
|
2017-07-14 05:54:34 +00:00
|
|
|
GET
|
|
|
|
X-Ping, accept-language"#;
|
|
|
|
assert_eq!(expected_body, body_str);
|
|
|
|
}
|