2017-07-26 06:37:02 +00:00
|
|
|
//! This crate tests using `rocket_cors` using the per-route handling with request guard
|
2018-10-31 02:25:10 +00:00
|
|
|
#![feature(proc_macro_hygiene, decl_macro)]
|
2018-11-21 04:17:40 +00:00
|
|
|
use rocket_cors as cors;
|
2017-07-14 04:26:47 +00:00
|
|
|
|
2020-08-11 07:43:51 +00:00
|
|
|
use rocket::http::hyper;
|
2017-07-14 04:26:47 +00:00
|
|
|
use rocket::http::Method;
|
|
|
|
use rocket::http::{Header, Status};
|
2020-08-11 08:36:40 +00:00
|
|
|
use rocket::local::blocking::Client;
|
2019-03-12 07:05:40 +00:00
|
|
|
use rocket::{get, options, routes};
|
2018-07-18 05:26:33 +00:00
|
|
|
use rocket::{Response, State};
|
2017-07-14 04:26:47 +00:00
|
|
|
|
|
|
|
#[get("/")]
|
2020-08-11 08:36:40 +00:00
|
|
|
fn cors(cors: cors::Guard<'_>) -> cors::Responder<'_, '_, &str> {
|
2017-07-17 06:28:54 +00:00
|
|
|
cors.responder("Hello CORS")
|
2017-07-14 04:26:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 06:41:08 +00:00
|
|
|
#[get("/panic")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn panicking_route(_cors: cors::Guard<'_>) {
|
2017-07-17 06:41:08 +00:00
|
|
|
panic!("This route will panic");
|
|
|
|
}
|
|
|
|
|
2017-07-24 07:45:53 +00:00
|
|
|
/// Manually specify our own OPTIONS route
|
|
|
|
#[options("/manual")]
|
2020-08-11 08:36:40 +00:00
|
|
|
fn cors_manual_options(cors: cors::Guard<'_>) -> cors::Responder<'_, '_, &str> {
|
2017-07-24 07:45:53 +00:00
|
|
|
cors.responder("Manual CORS Preflight")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Manually specify our own OPTIONS route
|
|
|
|
#[get("/manual")]
|
2020-08-11 08:36:40 +00:00
|
|
|
fn cors_manual(cors: cors::Guard<'_>) -> cors::Responder<'_, '_, &str> {
|
2017-07-24 07:45:53 +00:00
|
|
|
cors.responder("Hello CORS")
|
|
|
|
}
|
2017-07-17 06:28:54 +00:00
|
|
|
|
|
|
|
/// Using a `Response` instead of a `Responder`
|
2017-07-24 07:45:53 +00:00
|
|
|
#[get("/response")]
|
2018-11-21 04:17:40 +00:00
|
|
|
fn response(cors: cors::Guard<'_>) -> Response<'_> {
|
2017-07-17 06:28:54 +00:00
|
|
|
cors.response(Response::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `Responder` with String
|
2017-07-24 07:45:53 +00:00
|
|
|
#[get("/responder/string")]
|
2020-08-11 08:36:40 +00:00
|
|
|
fn responder_string(cors: cors::Guard<'_>) -> cors::Responder<'_, 'static, String> {
|
2017-07-17 06:28:54 +00:00
|
|
|
cors.responder("Hello CORS".to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `Responder` with 'static ()
|
2017-07-24 07:45:53 +00:00
|
|
|
#[get("/responder/unit")]
|
2020-08-11 08:36:40 +00:00
|
|
|
fn responder_unit(cors: cors::Guard<'_>) -> cors::Responder<'_, 'static, ()> {
|
2017-07-17 06:28:54 +00:00
|
|
|
cors.responder(())
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SomeState;
|
|
|
|
/// Borrow `SomeState` from Rocket
|
2017-07-24 07:45:53 +00:00
|
|
|
#[get("/state")]
|
2020-08-11 08:36:40 +00:00
|
|
|
fn state<'r, 'o: 'r>(
|
|
|
|
cors: cors::Guard<'r>,
|
|
|
|
_state: State<'r, SomeState>,
|
|
|
|
) -> cors::Responder<'r, 'o, &'r str> {
|
2017-07-17 06:28:54 +00:00
|
|
|
cors.responder("hmm")
|
|
|
|
}
|
|
|
|
|
2018-12-19 00:29:26 +00:00
|
|
|
fn make_cors() -> cors::Cors {
|
2019-03-12 07:05:40 +00:00
|
|
|
let allowed_origins = cors::AllowedOrigins::some_exact(&["https://www.acme.com"]);
|
2017-07-14 04:26:47 +00:00
|
|
|
|
2018-12-19 00:29:26 +00:00
|
|
|
cors::CorsOptions {
|
2019-03-12 07:05:40 +00:00
|
|
|
allowed_origins,
|
2017-07-17 08:22:45 +00:00
|
|
|
allowed_methods: vec![Method::Get].into_iter().map(From::from).collect(),
|
2017-07-19 04:25:56 +00:00
|
|
|
allowed_headers: cors::AllowedHeaders::some(&["Authorization", "Accept"]),
|
2017-07-14 04:26:47 +00:00
|
|
|
allow_credentials: true,
|
|
|
|
..Default::default()
|
|
|
|
}
|
2018-12-19 00:29:26 +00:00
|
|
|
.to_cors()
|
|
|
|
.expect("To not fail")
|
2017-07-14 04:26:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 06:41:08 +00:00
|
|
|
fn make_rocket() -> rocket::Rocket {
|
|
|
|
rocket::ignite()
|
2017-07-24 07:45:53 +00:00
|
|
|
.mount("/", routes![cors, panicking_route])
|
2018-10-31 02:30:10 +00:00
|
|
|
.mount(
|
|
|
|
"/",
|
|
|
|
routes![response, responder_string, responder_unit, state],
|
|
|
|
)
|
2017-07-24 07:45:53 +00:00
|
|
|
.mount("/", cors::catch_all_options_routes()) // mount the catch all routes
|
|
|
|
.mount("/", routes![cors_manual, cors_manual_options]) // manual OPTIOONS routes
|
2018-12-19 00:29:26 +00:00
|
|
|
.manage(make_cors())
|
2017-07-24 07:45:53 +00:00
|
|
|
.manage(SomeState)
|
2017-07-17 06:41:08 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 04:26:47 +00:00
|
|
|
#[test]
|
|
|
|
fn smoke_test() {
|
2017-07-17 06:41:08 +00:00
|
|
|
let rocket = make_rocket();
|
2017-07-14 04:26:47 +00:00
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
|
|
|
// `Options` pre-flight checks
|
2020-08-11 08:36:40 +00:00
|
|
|
let origin_header = Header::new(hyper::header::ORIGIN.as_str(), "https://www.acme.com");
|
|
|
|
let method_header = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_METHOD.as_str(),
|
|
|
|
hyper::Method::GET.as_str(),
|
|
|
|
);
|
|
|
|
let request_headers = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_HEADERS.as_str(),
|
|
|
|
"Authorization",
|
|
|
|
);
|
2017-07-14 04:26:47 +00:00
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
2017-07-17 06:28:54 +00:00
|
|
|
assert!(response.status().class().is_success());
|
2017-07-14 04:26:47 +00:00
|
|
|
|
|
|
|
// "Actual" request
|
2020-08-11 08:36:40 +00:00
|
|
|
let origin_header = Header::new(hyper::header::ORIGIN.as_str(), "https://www.acme.com");
|
2017-07-14 04:26:47 +00:00
|
|
|
let authorization = Header::new("Authorization", "let me in");
|
|
|
|
let req = client.get("/").header(origin_header).header(authorization);
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let response = req.dispatch();
|
2017-07-17 06:28:54 +00:00
|
|
|
assert!(response.status().class().is_success());
|
2017-07-19 01:51:31 +00:00
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
2017-07-19 08:26:44 +00:00
|
|
|
assert_eq!("https://www.acme.com", origin_header);
|
2020-08-11 08:36:40 +00:00
|
|
|
let body_str = response.into_string();
|
|
|
|
assert_eq!(body_str, Some("Hello CORS".to_string()));
|
2017-07-14 04:26:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 07:45:53 +00:00
|
|
|
/// Check the "catch all" OPTIONS route works for `/`
|
2017-07-14 04:26:47 +00:00
|
|
|
#[test]
|
2017-07-24 07:45:53 +00:00
|
|
|
fn cors_options_catch_all_check() {
|
2017-07-17 06:41:08 +00:00
|
|
|
let rocket = make_rocket();
|
2017-07-14 04:26:47 +00:00
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let origin_header = Header::new(hyper::header::ORIGIN.as_str(), "https://www.acme.com");
|
|
|
|
let method_header = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_METHOD.as_str(),
|
|
|
|
hyper::Method::GET.as_str(),
|
|
|
|
);
|
|
|
|
let request_headers = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_HEADERS.as_str(),
|
|
|
|
"Authorization",
|
|
|
|
);
|
2017-07-14 04:26:47 +00:00
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
|
|
|
let response = req.dispatch();
|
2017-07-17 06:28:54 +00:00
|
|
|
assert!(response.status().class().is_success());
|
2017-07-19 01:51:31 +00:00
|
|
|
|
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
2017-07-19 08:26:44 +00:00
|
|
|
assert_eq!("https://www.acme.com", origin_header);
|
2017-07-14 04:26:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 07:45:53 +00:00
|
|
|
/// Check the "catch all" OPTIONS route works for other routes
|
|
|
|
#[test]
|
|
|
|
fn cors_options_catch_all_check_other_routes() {
|
|
|
|
let rocket = make_rocket();
|
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let origin_header = Header::new(hyper::header::ORIGIN.as_str(), "https://www.acme.com");
|
|
|
|
let method_header = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_METHOD.as_str(),
|
|
|
|
hyper::Method::GET.as_str(),
|
|
|
|
);
|
|
|
|
let request_headers = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_HEADERS.as_str(),
|
|
|
|
"Authorization",
|
|
|
|
);
|
2017-07-24 07:45:53 +00:00
|
|
|
let req = client
|
|
|
|
.options("/response/unit")
|
|
|
|
.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);
|
|
|
|
}
|
|
|
|
|
2017-07-14 04:26:47 +00:00
|
|
|
#[test]
|
|
|
|
fn cors_get_check() {
|
2017-07-17 06:41:08 +00:00
|
|
|
let rocket = make_rocket();
|
2017-07-14 04:26:47 +00:00
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let origin_header = Header::new(hyper::header::ORIGIN.as_str(), "https://www.acme.com");
|
2017-07-14 04:26:47 +00:00
|
|
|
let authorization = Header::new("Authorization", "let me in");
|
|
|
|
let req = client.get("/").header(origin_header).header(authorization);
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let response = req.dispatch();
|
2017-07-17 06:28:54 +00:00
|
|
|
assert!(response.status().class().is_success());
|
2017-07-19 01:51:31 +00:00
|
|
|
let origin_header = response
|
|
|
|
.headers()
|
|
|
|
.get_one("Access-Control-Allow-Origin")
|
|
|
|
.expect("to exist");
|
2017-07-19 08:26:44 +00:00
|
|
|
assert_eq!("https://www.acme.com", origin_header);
|
2020-08-11 08:36:40 +00:00
|
|
|
let body_str = response.into_string();
|
|
|
|
assert_eq!(body_str, Some("Hello CORS".to_string()));
|
2017-07-14 04:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This test is to check that non CORS compliant requests to GET should still work. (i.e. curl)
|
|
|
|
#[test]
|
|
|
|
fn cors_get_no_origin() {
|
2017-07-17 06:41:08 +00:00
|
|
|
let rocket = make_rocket();
|
2017-07-14 04:26:47 +00:00
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
|
|
|
let authorization = Header::new("Authorization", "let me in");
|
|
|
|
let req = client.get("/").header(authorization);
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let response = req.dispatch();
|
2017-07-17 06:28:54 +00:00
|
|
|
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());
|
2020-08-11 08:36:40 +00:00
|
|
|
let body_str = response.into_string();
|
|
|
|
assert_eq!(body_str, Some("Hello CORS".to_string()));
|
2017-07-14 04:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_bad_origin() {
|
2017-07-17 06:41:08 +00:00
|
|
|
let rocket = make_rocket();
|
2017-07-14 04:26:47 +00:00
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let origin_header = Header::new(hyper::header::ORIGIN.as_str(), "https://www.bad-origin.com");
|
|
|
|
let method_header = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_METHOD.as_str(),
|
|
|
|
hyper::Method::GET.as_str(),
|
|
|
|
);
|
|
|
|
let request_headers = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_HEADERS.as_str(),
|
|
|
|
"Authorization",
|
|
|
|
);
|
2017-07-14 04:26:47 +00:00
|
|
|
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-14 04:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_missing_origin() {
|
2017-07-17 06:41:08 +00:00
|
|
|
let rocket = make_rocket();
|
2017-07-14 04:26:47 +00:00
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let method_header = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_METHOD.as_str(),
|
|
|
|
hyper::Method::GET.as_str(),
|
|
|
|
);
|
|
|
|
let request_headers = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_HEADERS.as_str(),
|
|
|
|
"Authorization",
|
|
|
|
);
|
2018-02-14 05:22:43 +00:00
|
|
|
let req = client
|
|
|
|
.options("/")
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
2017-07-14 04:26:47 +00:00
|
|
|
|
|
|
|
let response = req.dispatch();
|
2017-07-17 06:28:54 +00:00
|
|
|
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-14 04:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_bad_request_method() {
|
2017-07-17 06:41:08 +00:00
|
|
|
let rocket = make_rocket();
|
2017-07-14 04:26:47 +00:00
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let origin_header = Header::new(hyper::header::ORIGIN.as_str(), "https://www.acme.com");
|
|
|
|
let method_header = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_METHOD.as_str(),
|
|
|
|
hyper::Method::POST.as_str(),
|
|
|
|
);
|
|
|
|
let request_headers = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_HEADERS.as_str(),
|
|
|
|
"Authorization",
|
|
|
|
);
|
2017-07-14 04:26:47 +00:00
|
|
|
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-14 04:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_options_bad_request_header() {
|
2017-07-17 06:41:08 +00:00
|
|
|
let rocket = make_rocket();
|
2017-07-14 04:26:47 +00:00
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let origin_header = Header::new(hyper::header::ORIGIN.as_str(), "https://www.acme.com");
|
|
|
|
let method_header = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_METHOD.as_str(),
|
|
|
|
hyper::Method::GET.as_str(),
|
|
|
|
);
|
|
|
|
let request_headers = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_HEADERS.as_str(),
|
|
|
|
"Foobar",
|
|
|
|
);
|
2017-07-14 04:26:47 +00:00
|
|
|
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-14 04:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cors_get_bad_origin() {
|
2017-07-17 06:41:08 +00:00
|
|
|
let rocket = make_rocket();
|
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let origin_header = Header::new(hyper::header::ORIGIN.as_str(), "https://www.bad-origin.com");
|
2017-07-17 06:41:08 +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-17 06:41:08 +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 rocket = make_rocket();
|
2017-07-14 04:26:47 +00:00
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let origin_header = Header::new(hyper::header::ORIGIN.as_str(), "https://www.bad-origin.com");
|
2017-07-14 04:26:47 +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-14 04:26:47 +00:00
|
|
|
}
|
2017-07-24 07:45:53 +00:00
|
|
|
|
|
|
|
/// This test ensures that manually mounted CORS OPTIONS routes are used even in the presence of
|
|
|
|
/// a "catch all" route.
|
|
|
|
#[test]
|
|
|
|
fn overridden_options_routes_are_used() {
|
|
|
|
let rocket = make_rocket();
|
|
|
|
let client = Client::new(rocket).unwrap();
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let origin_header = Header::new(hyper::header::ORIGIN.as_str(), "https://www.acme.com");
|
|
|
|
let method_header = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_METHOD.as_str(),
|
|
|
|
hyper::Method::GET.as_str(),
|
|
|
|
);
|
|
|
|
let request_headers = Header::new(
|
|
|
|
hyper::header::ACCESS_CONTROL_REQUEST_HEADERS.as_str(),
|
|
|
|
"Authorization",
|
|
|
|
);
|
2017-07-24 07:45:53 +00:00
|
|
|
let req = client
|
|
|
|
.options("/manual")
|
|
|
|
.header(origin_header)
|
|
|
|
.header(method_header)
|
|
|
|
.header(request_headers);
|
|
|
|
|
2020-08-11 08:36:40 +00:00
|
|
|
let response = req.dispatch();
|
2017-07-24 07:45:53 +00:00
|
|
|
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);
|
2020-08-11 08:36:40 +00:00
|
|
|
let body_str = response.into_string();
|
|
|
|
assert_eq!(body_str, Some("Manual CORS Preflight".to_string()));
|
2017-07-24 07:45:53 +00:00
|
|
|
}
|