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;
|
2018-10-31 02:30:10 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rocket;
|
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;
|
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()
|
|
|
|
.and_then(|body| body.into_string())
|
|
|
|
.expect("Non-empty body");
|
2018-12-19 03:08:30 +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);
|
|
|
|
}
|