Files

202 lines
6.8 KiB
Rust

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Upstream handling: name resolution at connect time, health checks that survive odd
//! answers, websocket refusal on every Upgrade token, absolute-form requests.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
#[cfg(unix)]
mod common;
#[cfg(not(unix))]
#[test]
fn skipped_on_windows() {
eprintln!(
"SKIPPED on Windows: {} needs Linux (Pingora runner, Caddy, Pebble); run it in WSL2, see CLAUDE.md",
file!()
);
}
#[cfg(unix)]
mod unix {
use std::time::{Duration, Instant};
use crate::common::{EdgeSpec, host_file, start};
use ianitor_testkit::client::{self, Req};
use ianitor_testkit::raw::{RawServer, Reply, response};
use ianitor_testkit::upstream::Echo;
/// QC M1: `localhost` may resolve to ::1 first while the service listens on 127.0.0.1 only.
#[tokio::test(flavor = "multi_thread")]
async fn upstream_names_are_resolved_at_connect_time_trying_every_address() {
let echo = Echo::start("app").await;
let edge = start(EdgeSpec {
hosts: vec![
host_file(
"local.test",
&format!("upstreams = [\"http://localhost:{}\"]\n", echo.addr.port()),
),
host_file(
"gone.test",
"upstreams = [\"http://no-such-host.invalid:8080\"]\n",
),
],
..Default::default()
});
let r = client::https(
edge.https,
"local.test",
edge.roots(),
&Req::get("local.test", "/"),
)
.await
.unwrap();
assert_eq!(r.status, 200, "{}", edge.stderr());
// An upstream that does not resolve is a 502 for that host, not a startup failure.
let r = client::https(
edge.https,
"gone.test",
edge.roots(),
&Req::get("gone.test", "/"),
)
.await
.unwrap();
assert_eq!(r.status, 502);
}
/// QC B2a: a health endpoint that sends a 1xx must not kill health checking.
#[tokio::test(flavor = "multi_thread")]
async fn health_checks_survive_informational_answers() {
let started = Instant::now();
let up = RawServer::start(move |req| {
if req.path == "/healthz" && started.elapsed() < Duration::from_secs(3) {
let mut b = b"HTTP/1.1 102 Processing\r\n\r\n".to_vec();
b.extend(response(200, "OK", &[], b"ok"));
Reply::Bytes(b)
} else {
Reply::Bytes(response(500, "Internal Server Error", &[], b"broken"))
}
})
.await;
let edge = start(EdgeSpec {
hosts: vec![host_file(
"hc.test",
&format!(
"upstreams = [\"{}\"]\n[health_check]\npath = \"/healthz\"\ninterval_secs = 1\n",
up.url()
),
)],
..Default::default()
});
tokio::time::sleep(Duration::from_millis(5500)).await;
let r = client::https(
edge.https,
"hc.test",
edge.roots(),
&Req::get("hc.test", "/"),
)
.await
.unwrap();
assert_eq!(
r.status,
503,
"health checks kept running and marked the upstream down\n{}",
edge.stderr()
);
assert!(
up.requests()
.iter()
.filter(|r| r.path == "/healthz")
.count()
>= 4
);
}
/// QC m4: every Upgrade header and every comma-separated token counts.
#[tokio::test(flavor = "multi_thread")]
async fn websocket_refusal_checks_every_upgrade_token() {
let echo = Echo::start("app").await;
let edge = start(EdgeSpec {
hosts: vec![host_file(
"nows.test",
&format!("upstreams = [\"{}\"]\nwebsockets = false\n", echo.url()),
)],
..Default::default()
});
let cases = [
Req::get("nows.test", "/")
.header("connection", "upgrade")
.header("upgrade", "h2c")
.header("upgrade", "websocket"),
Req::get("nows.test", "/")
.header("connection", "upgrade")
.header("upgrade", "websocket, h2c"),
Req::get("nows.test", "/")
.header("connection", "upgrade")
.header("upgrade", "h2c , WebSocket"),
];
for req in &cases {
let r = client::https(edge.https, "nows.test", edge.roots(), req)
.await
.unwrap();
assert_eq!(r.status, 403, "{req:?}");
}
assert!(echo.proxied().is_empty());
}
/// QC nit: absolute-form request targets are answered, never dropped. Pingora refuses a target
/// whose authority differs from Host (400, on both listeners); a consistent one is routed and
/// sent upstream in origin form.
#[tokio::test(flavor = "multi_thread")]
async fn absolute_form_requests_are_answered() {
let echo = Echo::start("app").await;
let edge = start(EdgeSpec {
hosts: vec![host_file(
"plain.test",
&format!(
"upstreams = [\"{}\"]
",
echo.url()
),
)],
..Default::default()
});
let r = client::http(edge.http, &Req::get("plain.test", "http://evil.test/x"))
.await
.unwrap();
assert_eq!(r.status, 400, "plain HTTP, Host differs from the target");
let r = client::https(
edge.https,
"plain.test",
edge.roots(),
&Req::get("plain.test", "https://evil.test/x"),
)
.await
.unwrap();
assert_eq!(r.status, 400, "HTTPS, Host differs from the target");
assert!(echo.proxied().is_empty());
let r = client::http(edge.http, &Req::get("plain.test", "http://plain.test/x?y"))
.await
.unwrap();
assert_eq!(r.status, 308);
assert_eq!(
r.header("location"),
Some(format!("https://plain.test:{}/x?y", edge.https.port()).as_str())
);
let r = client::https(
edge.https,
"plain.test",
edge.roots(),
&Req::get("plain.test", "https://plain.test/abs?q=1"),
)
.await
.unwrap();
assert_eq!(r.status, 200);
assert!(r.text().contains("\"path\":\"/abs\""), "{}", r.text());
}
}