2017-07-14 05:54:34 +00:00
|
|
|
//! This crate tests that all the request headers are parsed correctly in the round trip
|
|
|
|
use std::ops::Deref;
|
|
|
|
|
2020-09-08 02:47:01 +00:00
|
|
|
use rocket::http::hyper;
|
2017-07-17 06:28:54 +00:00
|
|
|
use rocket::http::Header;
|
2020-09-08 02:47:01 +00:00
|
|
|
use rocket::local::blocking::Client;
|
2019-03-12 07:05:40 +00:00
|
|
|
use rocket::{get, routes};
|
2017-07-15 03:18:37 +00:00
|
|
|
use rocket_cors::headers::*;
|
2017-07-14 05:54:34 +00:00
|
|
|
|
2020-09-08 02:47:01 +00:00
|
|
|
static ORIGIN: hyper::HeaderName = hyper::header::ORIGIN;
|
|
|
|
static ACCESS_CONTROL_REQUEST_METHOD: hyper::HeaderName =
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_METHOD;
|
|
|
|
static ACCESS_CONTROL_REQUEST_HEADERS: hyper::HeaderName =
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_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() {
|
2021-04-26 07:36:37 +00:00
|
|
|
let rocket = rocket::build().mount("/", routes![request_headers]);
|
2020-10-29 14:42:09 +00:00
|
|
|
let client = Client::tracked(rocket).expect("A valid Rocket client");
|
2017-07-14 05:54:34 +00:00
|
|
|
|
2020-09-08 02:47:01 +00:00
|
|
|
let origin_header = Header::new(ORIGIN.as_str(), "https://foo.bar.xyz");
|
|
|
|
let method_header = Header::new(
|
|
|
|
ACCESS_CONTROL_REQUEST_METHOD.as_str(),
|
|
|
|
hyper::Method::GET.as_str(),
|
|
|
|
);
|
|
|
|
let request_headers = Header::new(
|
|
|
|
ACCESS_CONTROL_REQUEST_HEADERS.as_str(),
|
|
|
|
"accept-language, X-Ping",
|
|
|
|
);
|
2017-07-14 05:54:34 +00:00
|
|
|
let req = client
|
|
|
|
.get("/request_headers")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
2020-09-08 02:47:01 +00:00
|
|
|
let response = req.dispatch();
|
2017-07-14 05:54:34 +00:00
|
|
|
|
2017-07-17 06:28:54 +00:00
|
|
|
assert!(response.status().class().is_success());
|
2020-09-08 02:47:01 +00:00
|
|
|
let body_str = response.into_string();
|
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
|
2020-09-08 02:47:01 +00:00
|
|
|
X-Ping, accept-language"#
|
|
|
|
.to_string();
|
|
|
|
assert_eq!(body_str, Some(expected_body));
|
2017-07-14 05:54:34 +00:00
|
|
|
}
|