cargo fmt

This commit is contained in:
Yong Wen Chua 2018-10-31 10:30:10 +08:00
parent 0b73773692
commit 4dda14373a
No known key found for this signature in database
GPG Key ID: EDC57EEC439CF10B
12 changed files with 274 additions and 300 deletions

View File

@ -1,5 +1,6 @@
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use rocket::http::Method; use rocket::http::Method;

View File

@ -1,5 +1,6 @@
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use std::io::Cursor; use std::io::Cursor;
@ -48,10 +49,7 @@ fn main() {
}; };
rocket::ignite() rocket::ignite()
.mount( .mount("/", routes![responder, response])
"/",
routes![responder, response],
)
// Mount the routes to catch all the OPTIONS pre-flight requests // Mount the routes to catch all the OPTIONS pre-flight requests
.mount("/", rocket_cors::catch_all_options_routes()) .mount("/", rocket_cors::catch_all_options_routes())
// You can also manually mount an OPTIONS route that will be used instead // You can also manually mount an OPTIONS route that will be used instead

View File

@ -1,5 +1,6 @@
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use std::io::Cursor; use std::io::Cursor;
@ -66,15 +67,7 @@ fn cors_options() -> Cors {
fn main() { fn main() {
rocket::ignite() rocket::ignite()
.mount( .mount("/", routes![borrowed, response, owned, owned_options,])
"/",
routes![
borrowed,
response,
owned,
owned_options,
],
)
.mount("/", rocket_cors::catch_all_options_routes()) // mount the catch all routes .mount("/", rocket_cors::catch_all_options_routes()) // mount the catch all routes
.manage(cors_options()) .manage(cors_options())
.launch(); .launch();

View File

@ -4,7 +4,8 @@
//! `ping` route that you want to allow all Origins to access. //! `ping` route that you want to allow all Origins to access.
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use rocket::http::Method; use rocket::http::Method;
@ -59,14 +60,7 @@ fn cors_options_all() -> Cors {
fn main() { fn main() {
rocket::ignite() rocket::ignite()
.mount( .mount("/", routes![app, ping, ping_options,])
"/",
routes![
app,
ping,
ping_options,
],
)
.mount("/", rocket_cors::catch_all_options_routes()) // mount the catch all routes .mount("/", rocket_cors::catch_all_options_routes()) // mount the catch all routes
.manage(cors_options()) .manage(cors_options())
.launch(); .launch();

View File

@ -1,7 +1,7 @@
//! Fairing implementation //! Fairing implementation
use std::str::FromStr; use std::str::FromStr;
use rocket::http::{self, Header, Status, uri::Origin}; use rocket::http::{self, uri::Origin, Header, Status};
use rocket::{self, Outcome, Request}; use rocket::{self, Outcome, Request};
use {actual_request_response, origin, preflight_response, request_headers, validate, Cors, Error}; use {actual_request_response, origin, preflight_response, request_headers, validate, Cors, Error};
@ -44,7 +44,10 @@ pub(crate) fn fairing_error_route<'r>(
request: &'r Request, request: &'r Request,
_: rocket::Data, _: rocket::Data,
) -> rocket::handler::Outcome<'r> { ) -> rocket::handler::Outcome<'r> {
let status = request.get_param::<u16>(0).unwrap_or(Ok(0)).unwrap_or_else(|e| { let status = request
.get_param::<u16>(0)
.unwrap_or(Ok(0))
.unwrap_or_else(|e| {
error_!("Fairing Error Handling Route error: {:?}", e); error_!("Fairing Error Handling Route error: {:?}", e);
500 500
}); });

View File

@ -20,11 +20,7 @@ use url_serde;
#[derive(Eq, PartialEq, Clone, Debug, Hash)] #[derive(Eq, PartialEq, Clone, Debug, Hash)]
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
pub struct HeaderFieldName( pub struct HeaderFieldName(
#[cfg_attr( #[cfg_attr(feature = "serialization", serde(with = "unicase_serde::unicase"))] UniCase<String>,
feature = "serialization",
serde(with = "unicase_serde::unicase")
)]
UniCase<String>,
); );
impl Deref for HeaderFieldName { impl Deref for HeaderFieldName {

View File

@ -751,10 +751,7 @@ impl<T> AllOrSome<T> {
} }
impl AllOrSome<HashSet<Url>> { impl AllOrSome<HashSet<Url>> {
#[deprecated( #[deprecated(since = "0.1.3", note = "please use `AllowedOrigins::Some` instead")]
since = "0.1.3",
note = "please use `AllowedOrigins::Some` instead"
)]
/// New `AllOrSome` from a list of URL strings. /// New `AllOrSome` from a list of URL strings.
/// Returns a tuple where the first element is the struct `AllOrSome`, /// Returns a tuple where the first element is the struct `AllOrSome`,
/// and the second element /// and the second element
@ -1136,7 +1133,8 @@ impl Cors {
Method::Put, Method::Put,
Method::Patch, Method::Patch,
Method::Delete, Method::Delete,
].into_iter() ]
.into_iter()
.map(From::from) .map(From::from)
.collect() .collect()
} }
@ -2131,13 +2129,11 @@ mod tests {
// Build response and check built response header // Build response and check built response header
let response = response.response(response::Response::new()); let response = response.response(response::Response::new());
assert!( assert!(response
response
.headers() .headers()
.get("Access-Control-Max-Age") .get("Access-Control-Max-Age")
.next() .next()
.is_none() .is_none())
)
} }
#[test] #[test]
@ -2168,7 +2164,8 @@ mod tests {
validate_allowed_method( validate_allowed_method(
&FromStr::from_str(method).expect("not to fail"), &FromStr::from_str(method).expect("not to fail"),
&allowed_methods, &allowed_methods,
).unwrap() )
.unwrap()
} }
#[test] #[test]
@ -2214,7 +2211,8 @@ mod tests {
.map(|s| FromStr::from_str(*s).unwrap()) .map(|s| FromStr::from_str(*s).unwrap())
.collect(), .collect(),
), ),
).unwrap(); )
.unwrap();
} }
#[test] #[test]
@ -2257,13 +2255,11 @@ mod tests {
assert_eq!(expected_header, actual_header); assert_eq!(expected_header, actual_header);
// Check that `Access-Control-Max-Age` is removed // Check that `Access-Control-Max-Age` is removed
assert!( assert!(response
response
.headers() .headers()
.get("Access-Control-Max-Age") .get("Access-Control-Max-Age")
.next() .next()
.is_none() .is_none());
);
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -2305,8 +2301,9 @@ mod tests {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
@ -2338,8 +2335,9 @@ mod tests {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
@ -2363,8 +2361,9 @@ mod tests {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
@ -2394,8 +2393,9 @@ mod tests {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
@ -2416,8 +2416,9 @@ mod tests {
let origin_header = let origin_header =
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap()); Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
@ -2440,8 +2441,9 @@ mod tests {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Post, hyper::method::Method::Post,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
@ -2562,8 +2564,9 @@ mod tests {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
@ -2600,8 +2603,9 @@ mod tests {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
@ -2638,8 +2642,9 @@ mod tests {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);

View File

@ -1,7 +1,8 @@
//! This crate tests using `rocket_cors` using Fairings //! This crate tests using `rocket_cors` using Fairings
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
extern crate hyper; extern crate hyper;
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use std::str::FromStr; use std::str::FromStr;
@ -50,8 +51,9 @@ fn smoke_test() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -90,8 +92,9 @@ fn cors_options_check() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -154,8 +157,9 @@ fn cors_options_bad_origin() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -176,8 +180,9 @@ fn cors_options_missing_origin() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -188,12 +193,10 @@ fn cors_options_missing_origin() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::NotFound); assert_eq!(response.status(), Status::NotFound);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -205,8 +208,9 @@ fn cors_options_bad_request_method() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Post, hyper::method::Method::Post,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -217,12 +221,10 @@ fn cors_options_bad_request_method() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -245,12 +247,10 @@ fn cors_options_bad_request_header() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -264,12 +264,10 @@ fn cors_get_bad_origin() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
/// This test ensures that on a failing CORS request, the route (along with its side effects) /// This test ensures that on a failing CORS request, the route (along with its side effects)
@ -284,8 +282,9 @@ fn routes_failing_checks_are_not_executed() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -296,10 +295,8 @@ fn routes_failing_checks_are_not_executed() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }

View File

@ -1,7 +1,8 @@
//! This crate tests using `rocket_cors` using the per-route handling with request guard //! This crate tests using `rocket_cors` using the per-route handling with request guard
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
extern crate hyper; extern crate hyper;
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
extern crate rocket_cors as cors; extern crate rocket_cors as cors;
use std::str::FromStr; use std::str::FromStr;
@ -74,7 +75,10 @@ fn make_cors_options() -> cors::Cors {
fn make_rocket() -> rocket::Rocket { fn make_rocket() -> rocket::Rocket {
rocket::ignite() rocket::ignite()
.mount("/", routes![cors, panicking_route]) .mount("/", routes![cors, panicking_route])
.mount("/", routes![response, responder_string, responder_unit, state]) .mount(
"/",
routes![response, responder_string, responder_unit, state],
)
.mount("/", cors::catch_all_options_routes()) // mount the catch all routes .mount("/", cors::catch_all_options_routes()) // mount the catch all routes
.mount("/", routes![cors_manual, cors_manual_options]) // manual OPTIOONS routes .mount("/", routes![cors_manual, cors_manual_options]) // manual OPTIOONS routes
.manage(make_cors_options()) .manage(make_cors_options())
@ -92,8 +96,9 @@ fn smoke_test() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -134,8 +139,9 @@ fn cors_options_catch_all_check() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -165,8 +171,9 @@ fn cors_options_catch_all_check_other_routes() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -220,12 +227,10 @@ fn cors_get_no_origin() {
assert!(response.status().class().is_success()); assert!(response.status().class().is_success());
let body_str = response.body().and_then(|body| body.into_string()); let body_str = response.body().and_then(|body| body.into_string());
assert_eq!(body_str, Some("Hello CORS".to_string())); assert_eq!(body_str, Some("Hello CORS".to_string()));
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -238,8 +243,9 @@ fn cors_options_bad_origin() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -250,12 +256,10 @@ fn cors_options_bad_origin() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -266,8 +270,9 @@ fn cors_options_missing_origin() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -277,12 +282,10 @@ fn cors_options_missing_origin() {
let response = req.dispatch(); let response = req.dispatch();
assert!(response.status().class().is_success()); assert!(response.status().class().is_success());
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -295,8 +298,9 @@ fn cors_options_bad_request_method() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Post, hyper::method::Method::Post,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -307,12 +311,10 @@ fn cors_options_bad_request_method() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -336,12 +338,10 @@ fn cors_options_bad_request_header() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -356,12 +356,10 @@ fn cors_get_bad_origin() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
/// This test ensures that on a failing CORS request, the route (along with its side effects) /// This test ensures that on a failing CORS request, the route (along with its side effects)
@ -379,12 +377,10 @@ fn routes_failing_checks_are_not_executed() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
/// This test ensures that manually mounted CORS OPTIONS routes are used even in the presence of /// This test ensures that manually mounted CORS OPTIONS routes are used even in the presence of
@ -399,8 +395,9 @@ fn overridden_options_routes_are_used() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client

View File

@ -1,7 +1,8 @@
//! This crate tests that all the request headers are parsed correctly in the round trip //! This crate tests that all the request headers are parsed correctly in the round trip
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
extern crate hyper; extern crate hyper;
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use std::ops::Deref; use std::ops::Deref;

View File

@ -1,7 +1,8 @@
//! This crate tests using `rocket_cors` using manual mode //! This crate tests using `rocket_cors` using manual mode
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
extern crate hyper; extern crate hyper;
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use std::str::FromStr; use std::str::FromStr;
@ -109,8 +110,9 @@ fn smoke_test() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -149,8 +151,9 @@ fn cors_options_borrowed_check() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -213,8 +216,9 @@ fn cors_options_bad_origin() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -234,8 +238,9 @@ fn cors_options_missing_origin() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -245,12 +250,10 @@ fn cors_options_missing_origin() {
let response = req.dispatch(); let response = req.dispatch();
assert!(response.status().class().is_success()); assert!(response.status().class().is_success());
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -262,8 +265,9 @@ fn cors_options_bad_request_method() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Post, hyper::method::Method::Post,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -274,12 +278,10 @@ fn cors_options_bad_request_method() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -302,12 +304,10 @@ fn cors_options_bad_request_header() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -321,12 +321,10 @@ fn cors_get_bad_origin() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
/// This test ensures that on a failing CORS request, the route (along with its side effects) /// This test ensures that on a failing CORS request, the route (along with its side effects)
@ -341,8 +339,9 @@ fn routes_failing_checks_are_not_executed() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -353,12 +352,10 @@ fn routes_failing_checks_are_not_executed() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
/// Manual OPTIONS routes are called /// Manual OPTIONS routes are called
@ -372,8 +369,9 @@ fn cors_options_owned_check() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client

View File

@ -4,7 +4,8 @@
//! `ping` route that you want to allow all Origins to access. //! `ping` route that you want to allow all Origins to access.
#![feature(proc_macro_hygiene, decl_macro)] #![feature(proc_macro_hygiene, decl_macro)]
extern crate hyper; extern crate hyper;
#[macro_use] extern crate rocket; #[macro_use]
extern crate rocket;
extern crate rocket_cors; extern crate rocket_cors;
use std::str::FromStr; use std::str::FromStr;
@ -63,14 +64,7 @@ fn cors_options_all() -> Cors {
fn rocket() -> rocket::Rocket { fn rocket() -> rocket::Rocket {
rocket::ignite() rocket::ignite()
.mount( .mount("/", routes![app, ping, ping_options,])
"/",
routes![
app,
ping,
ping_options,
],
)
.mount("/", rocket_cors::catch_all_options_routes()) // mount the catch all routes .mount("/", rocket_cors::catch_all_options_routes()) // mount the catch all routes
.manage(cors_options()) .manage(cors_options())
} }
@ -85,8 +79,9 @@ fn smoke_test() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -125,8 +120,9 @@ fn cors_options_check() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -189,8 +185,9 @@ fn cors_options_bad_origin() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -210,8 +207,9 @@ fn cors_options_missing_origin() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Get, hyper::method::Method::Get,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -221,12 +219,10 @@ fn cors_options_missing_origin() {
let response = req.dispatch(); let response = req.dispatch();
assert!(response.status().class().is_success()); assert!(response.status().class().is_success());
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -238,8 +234,9 @@ fn cors_options_bad_request_method() {
let method_header = Header::from(hyper::header::AccessControlRequestMethod( let method_header = Header::from(hyper::header::AccessControlRequestMethod(
hyper::method::Method::Post, hyper::method::Method::Post,
)); ));
let request_headers = hyper::header::AccessControlRequestHeaders(vec![ let request_headers =
FromStr::from_str("Authorization").unwrap(), hyper::header::AccessControlRequestHeaders(vec![
FromStr::from_str("Authorization").unwrap()
]); ]);
let request_headers = Header::from(request_headers); let request_headers = Header::from(request_headers);
let req = client let req = client
@ -250,12 +247,10 @@ fn cors_options_bad_request_method() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -278,12 +273,10 @@ fn cors_options_bad_request_header() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
#[test] #[test]
@ -297,12 +290,10 @@ fn cors_get_bad_origin() {
let response = req.dispatch(); let response = req.dispatch();
assert_eq!(response.status(), Status::Forbidden); assert_eq!(response.status(), Status::Forbidden);
assert!( assert!(response
response
.headers() .headers()
.get_one("Access-Control-Allow-Origin") .get_one("Access-Control-Allow-Origin")
.is_none() .is_none());
);
} }
/// Tests that the `ping` route accepts other Origins /// Tests that the `ping` route accepts other Origins