2024-01-23 02:18:06 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
2023-04-11 21:43:12 +00:00
|
|
|
|
|
|
|
#[get("/")]
|
2024-01-23 02:18:06 +00:00
|
|
|
fn inspect_proto(proto: rocket::http::ProxyProto) -> String {
|
|
|
|
proto.to_string()
|
2023-04-11 21:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mod tests {
|
|
|
|
use rocket::{Rocket, Build, Route};
|
2024-01-23 02:18:06 +00:00
|
|
|
use rocket::http::{Header, Status};
|
2023-04-11 21:43:12 +00:00
|
|
|
use rocket::local::blocking::Client;
|
|
|
|
use rocket::figment::Figment;
|
|
|
|
|
|
|
|
fn routes() -> Vec<Route> {
|
|
|
|
routes![super::inspect_proto]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rocket_with_proto_header(header: Option<&'static str>) -> Rocket<Build> {
|
|
|
|
let mut config = rocket::Config::debug_default();
|
|
|
|
config.proxy_proto_header = header.map(|h| h.into());
|
|
|
|
rocket::custom(config).mount("/", routes())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn check_proxy_proto_header_works() {
|
2024-01-23 02:18:06 +00:00
|
|
|
let client = Client::debug(rocket_with_proto_header(Some("X-Url-Scheme"))).unwrap();
|
2023-04-11 21:43:12 +00:00
|
|
|
let response = client.get("/")
|
|
|
|
.header(Header::new("X-Forwarded-Proto", "https"))
|
|
|
|
.header(Header::new("X-Url-Scheme", "http"))
|
|
|
|
.dispatch();
|
|
|
|
|
|
|
|
assert_eq!(response.into_string().unwrap(), "http");
|
|
|
|
|
2024-01-23 02:18:06 +00:00
|
|
|
let response = client.get("/").header(Header::new("X-Url-Scheme", "https")).dispatch();
|
2023-04-11 21:43:12 +00:00
|
|
|
assert_eq!(response.into_string().unwrap(), "https");
|
|
|
|
|
|
|
|
let response = client.get("/").dispatch();
|
2024-01-23 02:18:06 +00:00
|
|
|
assert_eq!(response.status(), Status::InternalServerError);
|
2023-04-11 21:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn check_proxy_proto_header_works_again() {
|
|
|
|
let client = Client::debug(rocket_with_proto_header(Some("x-url-scheme"))).unwrap();
|
2024-01-23 02:18:06 +00:00
|
|
|
let response = client.get("/")
|
|
|
|
.header(Header::new("X-Url-Scheme", "hTTpS"))
|
2023-04-11 21:43:12 +00:00
|
|
|
.dispatch();
|
|
|
|
|
|
|
|
assert_eq!(response.into_string().unwrap(), "https");
|
|
|
|
|
|
|
|
let config = Figment::from(rocket::Config::debug_default())
|
|
|
|
.merge(("proxy_proto_header", "x-url-scheme"));
|
|
|
|
|
|
|
|
let client = Client::debug(rocket::custom(config).mount("/", routes())).unwrap();
|
2024-01-23 02:18:06 +00:00
|
|
|
let response = client.get("/")
|
|
|
|
.header(Header::new("X-url-Scheme", "HTTPS"))
|
2023-04-11 21:43:12 +00:00
|
|
|
.dispatch();
|
|
|
|
|
|
|
|
assert_eq!(response.into_string().unwrap(), "https");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn check_default_proxy_proto_header_works() {
|
|
|
|
let client = Client::debug_with(routes()).unwrap();
|
2024-01-23 02:18:06 +00:00
|
|
|
let response = client.get("/")
|
2023-04-11 21:43:12 +00:00
|
|
|
.header(Header::new("X-Forwarded-Proto", "https"))
|
|
|
|
.dispatch();
|
|
|
|
|
2024-01-23 02:18:06 +00:00
|
|
|
assert_eq!(response.status(), Status::InternalServerError);
|
2023-04-11 21:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn check_no_proxy_proto_header_works() {
|
|
|
|
let client = Client::debug(rocket_with_proto_header(None)).unwrap();
|
|
|
|
let response = client.get("/")
|
|
|
|
.header(Header::new("X-Forwarded-Proto", "https"))
|
|
|
|
.dispatch();
|
|
|
|
|
2024-01-23 02:18:06 +00:00
|
|
|
assert_eq!(response.status(), Status::InternalServerError);
|
2023-04-11 21:43:12 +00:00
|
|
|
|
|
|
|
let config =
|
|
|
|
Figment::from(rocket::Config::debug_default()).merge(("proxy_proto_header", false));
|
|
|
|
|
|
|
|
let client = Client::debug(rocket::custom(config).mount("/", routes())).unwrap();
|
2024-01-23 02:18:06 +00:00
|
|
|
let response = client.get("/")
|
2023-04-11 21:43:12 +00:00
|
|
|
.header(Header::new("X-Forwarded-Proto", "https"))
|
|
|
|
.dispatch();
|
|
|
|
|
2024-01-23 02:18:06 +00:00
|
|
|
assert_eq!(response.status(), Status::InternalServerError);
|
2023-04-11 21:43:12 +00:00
|
|
|
|
|
|
|
let config = Figment::from(rocket::Config::debug_default())
|
|
|
|
.merge(("proxy_proto_header", "x-forwarded-proto"));
|
|
|
|
|
|
|
|
let client = Client::debug(rocket::custom(config).mount("/", routes())).unwrap();
|
2024-01-23 02:18:06 +00:00
|
|
|
let response = client.get("/")
|
2023-04-11 21:43:12 +00:00
|
|
|
.header(Header::new("x-Forwarded-Proto", "https"))
|
|
|
|
.dispatch();
|
|
|
|
|
|
|
|
assert_eq!(response.into_string(), Some("https".into()));
|
|
|
|
}
|
|
|
|
}
|