rocket_cors/tests/headers.rs

63 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
#![feature(proc_macro_hygiene, decl_macro)]
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;
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");
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();
assert!(response.status().class().is_success());
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);
}