rocket_cors/tests/headers.rs

62 lines
2.0 KiB
Rust
Raw Normal View History

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;
use rocket::http::hyper;
use rocket::http::Header;
use rocket::local::blocking::Client;
use rocket::{get, routes};
2017-07-15 03:18:37 +00:00
use rocket_cors::headers::*;
2017-07-14 05:54:34 +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() {
let rocket = rocket::build().mount("/", routes![request_headers]);
let client = Client::tracked(rocket).expect("A valid Rocket client");
2017-07-14 05:54:34 +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);
let response = req.dispatch();
2017-07-14 05:54:34 +00:00
assert!(response.status().class().is_success());
let body_str = response.into_string();
let expected_body = r#"https://foo.bar.xyz
2017-07-14 05:54:34 +00:00
GET
X-Ping, accept-language"#
.to_string();
assert_eq!(body_str, Some(expected_body));
2017-07-14 05:54:34 +00:00
}