Rocket/core/lib/tests/config-proxy-proto-header.rs

101 lines
3.5 KiB
Rust
Raw Normal View History

2024-01-23 02:18:06 +00:00
#[macro_use] extern crate rocket;
#[get("/")]
2024-01-23 02:18:06 +00:00
fn inspect_proto(proto: rocket::http::ProxyProto) -> String {
proto.to_string()
}
mod tests {
use rocket::{Rocket, Build, Route};
2024-01-23 02:18:06 +00:00
use rocket::http::{Header, Status};
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();
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();
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);
}
#[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"))
.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"))
.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("/")
.header(Header::new("X-Forwarded-Proto", "https"))
.dispatch();
2024-01-23 02:18:06 +00:00
assert_eq!(response.status(), Status::InternalServerError);
}
#[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);
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("/")
.header(Header::new("X-Forwarded-Proto", "https"))
.dispatch();
2024-01-23 02:18:06 +00:00
assert_eq!(response.status(), Status::InternalServerError);
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("/")
.header(Header::new("x-Forwarded-Proto", "https"))
.dispatch();
assert_eq!(response.into_string(), Some("https".into()));
}
}