2017-07-26 06:37:02 +00:00
|
|
|
//! This crate tests using `rocket_cors` using manual mode
|
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-24 05:11:10 +00:00
|
|
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
use rocket::http::Method;
|
|
|
|
use rocket::http::{Header, Status};
|
|
|
|
use rocket::local::Client;
|
|
|
|
use rocket::response::Responder;
|
2018-07-18 05:26:33 +00:00
|
|
|
use rocket::State;
|
2017-07-24 05:11:10 +00:00
|
|
|
use rocket_cors::*;
|
|
|
|
|
|
|
|
/// Using a borrowed `Cors`
|
|
|
|
#[get("/")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn cors(options: State<'_, Cors>) -> impl Responder<'_> {
|
2018-02-14 05:22:43 +00:00
|
|
|
options
|
|
|
|
.inner()
|
|
|
|
.respond_borrowed(|guard| guard.responder("Hello CORS"))
|
2017-07-24 05:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/panic")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn panicking_route(options: State<'_, Cors>) -> impl Responder<'_> {
|
2017-07-24 05:11:10 +00:00
|
|
|
options.inner().respond_borrowed(|_| -> () {
|
|
|
|
panic!("This route will panic");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-24 07:45:53 +00:00
|
|
|
/// Respond with an owned option instead
|
|
|
|
#[options("/owned")]
|
|
|
|
fn owned_options<'r>() -> impl Responder<'r> {
|
2018-12-19 00:29:26 +00:00
|
|
|
let borrow = make_different_cors_options().to_cors()?;
|
2017-07-24 07:45:53 +00:00
|
|
|
|
|
|
|
borrow.respond_owned(|guard| guard.responder("Manual CORS Preflight"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Respond with an owned option instead
|
|
|
|
#[get("/owned")]
|
|
|
|
fn owned<'r>() -> impl Responder<'r> {
|
2018-12-19 00:29:26 +00:00
|
|
|
let borrow = make_different_cors_options().to_cors()?;
|
2017-07-24 07:45:53 +00:00
|
|
|
|
|
|
|
borrow.respond_owned(|guard| guard.responder("Hello CORS Owned"))
|
|
|
|
}
|
|
|
|
|
2017-07-24 05:11:10 +00:00
|
|
|
// The following routes tests that the routes can be compiled with manual CORS
|
|
|
|
|
|
|
|
/// `Responder` with String
|
|
|
|
#[get("/")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn responder_string(options: State<'_, Cors>) -> impl Responder<'_> {
|
2018-02-14 05:22:43 +00:00
|
|
|
options
|
|
|
|
.inner()
|
|
|
|
.respond_borrowed(|guard| guard.responder("Hello CORS".to_string()))
|
2017-07-24 05:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TestState;
|
|
|
|
/// Borrow something else from Rocket with lifetime `'r`
|
|
|
|
#[get("/")]
|
|
|
|
fn borrow<'r>(options: State<'r, Cors>, test_state: State<'r, TestState>) -> impl Responder<'r> {
|
|
|
|
let borrow = test_state.inner();
|
|
|
|
options.inner().respond_borrowed(move |guard| {
|
|
|
|
let _ = borrow;
|
|
|
|
guard.responder("Hello CORS".to_string())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-19 00:29:26 +00:00
|
|
|
fn make_cors_options() -> CorsOptions {
|
2018-12-19 03:08:30 +00:00
|
|
|
let allowed_origins = AllowedOrigins::some(&["https://www.acme.com"]);
|
2017-07-24 05:11:10 +00:00
|
|
|
|
2018-12-19 00:29:26 +00:00
|
|
|
CorsOptions {
|
2017-07-24 07:45:53 +00:00
|
|
|
allowed_origins: allowed_origins,
|
|
|
|
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
|
|
|
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
|
|
|
|
allow_credentials: true,
|
|
|
|
..Default::default()
|
|
|
|
}
|
2017-07-24 05:11:10 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 00:29:26 +00:00
|
|
|
fn make_different_cors_options() -> CorsOptions {
|
2018-12-19 03:08:30 +00:00
|
|
|
let allowed_origins = AllowedOrigins::some(&["https://www.example.com"]);
|
2017-07-24 05:11:10 +00:00
|
|
|
|
2018-12-19 00:29:26 +00:00
|
|
|
CorsOptions {
|
2017-07-24 05:11:10 +00:00
|
|
|
allowed_origins: allowed_origins,
|
|
|
|
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
|
|
|
allowed_headers: AllowedHeaders::some(&["Authorization", "Accept"]),
|
|
|
|
allow_credentials: true,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rocket() -> rocket::Rocket {
|
|
|
|
rocket::ignite()
|
2017-07-24 07:45:53 +00:00
|
|
|
.mount("/", routes![cors, panicking_route])
|
|
|
|
.mount("/", routes![owned, owned_options])
|
|
|
|
.mount("/", catch_all_options_routes()) // mount the catch all routes
|
2018-12-19 00:29:26 +00:00
|
|
|
.manage(make_cors_options().to_cors().expect("Not to fail"))
|
2017-07-24 05:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn smoke_test() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
|
|
|
// `Options` pre-flight checks
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
|
2017-07-24 05:11:10 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
|
|
|
hyper::method::Method::Get,
|
|
|
|
));
|
2018-10-31 02:30:10 +00:00
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("Authorization").unwrap()
|
|
|
|
]);
|
2017-07-24 05:11:10 +00:00
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
|
|
|
|
|
|
|
// "Actual" request
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
|
2017-07-24 05:11:10 +00:00
|
|
|
let authorization = Header::new("Authorization", "let me in");
|
|
|
|
let req = client.get("/").header(origin_header).header(authorization);
|
|
|
|
|
|
|
|
let mut response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
|
|
|
let body_str = response.body().and_then(|body| body.into_string());
|
|
|
|
assert_eq!(body_str, Some("Hello CORS".to_string()));
|
|
|
|
|
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
|
|
|
assert_eq!("https://www.acme.com", origin_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-07-24 07:45:53 +00:00
|
|
|
fn cors_options_borrowed_check() {
|
2017-07-24 05:11:10 +00:00
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
|
2017-07-24 05:11:10 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
|
|
|
hyper::method::Method::Get,
|
|
|
|
));
|
2018-10-31 02:30:10 +00:00
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("Authorization").unwrap()
|
|
|
|
]);
|
2017-07-24 05:11:10 +00:00
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
|
|
|
|
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
|
|
|
assert_eq!("https://www.acme.com", origin_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-07-24 07:45:53 +00:00
|
|
|
fn cors_get_borrowed_check() {
|
2017-07-24 05:11:10 +00:00
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
|
2017-07-24 05:11:10 +00:00
|
|
|
let authorization = Header::new("Authorization", "let me in");
|
|
|
|
let req = client.get("/").header(origin_header).header(authorization);
|
|
|
|
|
|
|
|
let mut response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
|
|
|
let body_str = response.body().and_then(|body| body.into_string());
|
|
|
|
assert_eq!(body_str, Some("Hello CORS".to_string()));
|
|
|
|
|
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
|
|
|
assert_eq!("https://www.acme.com", origin_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This test is to check that non CORS compliant requests to GET should still work. (i.e. curl)
|
|
|
|
#[test]
|
|
|
|
fn cors_get_no_origin() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
|
|
|
let authorization = Header::new("Authorization", "let me in");
|
|
|
|
let req = client.get("/").header(authorization);
|
|
|
|
|
|
|
|
let mut response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
|
|
|
let body_str = response.body().and_then(|body| body.into_string());
|
|
|
|
assert_eq!(body_str, Some("Hello CORS".to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_bad_origin() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.bad-origin.com").unwrap());
|
2017-07-24 05:11:10 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
|
|
|
hyper::method::Method::Get,
|
|
|
|
));
|
2018-10-31 02:30:10 +00:00
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("Authorization").unwrap()
|
|
|
|
]);
|
2017-07-24 05:11:10 +00:00
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Forbidden);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_missing_origin() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
|
|
|
hyper::method::Method::Get,
|
|
|
|
));
|
2018-10-31 02:30:10 +00:00
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("Authorization").unwrap()
|
|
|
|
]);
|
2017-07-24 05:11:10 +00:00
|
|
|
let request_headers = Header::from(request_headers);
|
2018-02-14 05:22:43 +00:00
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
2017-07-24 05:11:10 +00:00
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
2018-10-31 02:30:10 +00:00
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.is_none());
|
2017-07-24 05:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_bad_request_method() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
|
2017-07-24 05:11:10 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
|
|
|
hyper::method::Method::Post,
|
|
|
|
));
|
2018-10-31 02:30:10 +00:00
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("Authorization").unwrap()
|
|
|
|
]);
|
2017-07-24 05:11:10 +00:00
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Forbidden);
|
2018-10-31 02:30:10 +00:00
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.is_none());
|
2017-07-24 05:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_bad_request_header() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.acme.com").unwrap());
|
2017-07-24 05:11:10 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
|
|
|
hyper::method::Method::Get,
|
|
|
|
));
|
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![FromStr::from_str("Foobar").unwrap()]);
|
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Forbidden);
|
2018-10-31 02:30:10 +00:00
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.is_none());
|
2017-07-24 05:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_get_bad_origin() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.bad-origin.com").unwrap());
|
2017-07-24 05:11:10 +00:00
|
|
|
let authorization = Header::new("Authorization", "let me in");
|
|
|
|
let req = client.get("/").header(origin_header).header(authorization);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Forbidden);
|
2018-10-31 02:30:10 +00:00
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.is_none());
|
2017-07-24 05:11:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This test ensures that on a failing CORS request, the route (along with its side effects)
|
|
|
|
/// should never be executed.
|
|
|
|
/// The route used will panic if executed
|
|
|
|
#[test]
|
|
|
|
fn routes_failing_checks_are_not_executed() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.bad-origin.com").unwrap());
|
2017-07-24 05:11:10 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
|
|
|
hyper::method::Method::Get,
|
|
|
|
));
|
2018-10-31 02:30:10 +00:00
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("Authorization").unwrap()
|
|
|
|
]);
|
2017-07-24 05:11:10 +00:00
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.options("/panic")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Forbidden);
|
2018-10-31 02:30:10 +00:00
|
|
|
assert!(response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.is_none());
|
2017-07-24 05:11:10 +00:00
|
|
|
}
|
2017-07-24 07:45:53 +00:00
|
|
|
|
|
|
|
/// Manual OPTIONS routes are called
|
|
|
|
#[test]
|
|
|
|
fn cors_options_owned_check() {
|
|
|
|
let rocket = rocket();
|
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.example.com").unwrap());
|
2017-07-24 07:45:53 +00:00
|
|
|
let method_header = Header::from(hyper::header::AccessControlRequestMethod(
|
|
|
|
hyper::method::Method::Get,
|
|
|
|
));
|
2018-10-31 02:30:10 +00:00
|
|
|
let request_headers =
|
|
|
|
hyper::header::AccessControlRequestHeaders(vec![
|
|
|
|
FromStr::from_str("Authorization").unwrap()
|
|
|
|
]);
|
2017-07-24 07:45:53 +00:00
|
|
|
let request_headers = Header::from(request_headers);
|
|
|
|
let req = client
|
|
|
|
.options("/owned")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let mut response = req.dispatch();
|
|
|
|
let body_str = response.body().and_then(|body| body.into_string());
|
|
|
|
assert!(response.status().class().is_success());
|
|
|
|
assert_eq!(body_str, Some("Manual CORS Preflight".to_string()));
|
|
|
|
|
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
|
|
|
assert_eq!("https://www.example.com", origin_header);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Owned manual response works
|
|
|
|
#[test]
|
|
|
|
fn cors_get_owned_check() {
|
|
|
|
let client = Client::new(rocket()).unwrap();
|
|
|
|
|
2018-02-14 05:22:43 +00:00
|
|
|
let origin_header =
|
|
|
|
Header::from(hyper::header::Origin::from_str("https://www.example.com").unwrap());
|
2017-07-24 07:45:53 +00:00
|
|
|
let authorization = Header::new("Authorization", "let me in");
|
2018-02-14 05:22:43 +00:00
|
|
|
let req = client
|
|
|
|
.get("/owned")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(authorization);
|
2017-07-24 07:45:53 +00:00
|
|
|
|
|
|
|
let mut response = req.dispatch();
|
|
|
|
assert!(response.status().class().is_success());
|
|
|
|
let body_str = response.body().and_then(|body| body.into_string());
|
|
|
|
assert_eq!(body_str, Some("Hello CORS Owned".to_string()));
|
|
|
|
|
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
|
|
|
assert_eq!("https://www.example.com", origin_header);
|
|
|
|
}
|