From 18169dcbc2d0f50c99965852001d43f13333e6f9 Mon Sep 17 00:00:00 2001 From: mnovak Date: Tue, 10 Nov 2026 21:36:53 +0100 Subject: [PATCH] [edge] Pipelining: a unit test on an in-memory pipe for the plain-HTTP listener (runs on Windows too) --- crates/edge/src/http_app.rs | 96 +++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/crates/edge/src/http_app.rs b/crates/edge/src/http_app.rs index 6671ae2..a846dc1 100644 --- a/crates/edge/src/http_app.rs +++ b/crates/edge/src/http_app.rs @@ -132,3 +132,99 @@ impl HttpApp { r } } + +#[cfg(test)] +mod tests { + #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + use super::*; + use pingora::apps::ServerApp; + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + /// An edge runtime with one Let's Encrypt host (nothing to read from disk). + fn shared() -> Arc { + let dir = std::env::temp_dir().join(format!("ianitor-http-app-{}", std::process::id())); + let d = dir.to_string_lossy().replace('\\', "/"); + let d = if d.starts_with('/') { + d + } else { + format!("/{d}") + }; + let model = ianitor_core::load( + &ianitor_core::SourceFile { + name: "ianitor.toml", + contents: "", + }, + &[ianitor_core::SourceFile { + name: "hosts/app.test.toml", + contents: "hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:9\"]\n", + }], + ) + .unwrap(); + let opts = ianitor_core::compile::CompileOptions { + http_listen: "127.0.0.1:18080".parse().unwrap(), + https_listen: "127.0.0.1:18443".parse().unwrap(), + cert_dir: format!("{d}/certs"), + state_dir: format!("{d}/state"), + access_log: format!("{d}/access.log"), + caddy_credentials_dir: format!("{d}/creds"), + caddy_admin: None, + caddy_admin_socket: None, + trusted_proxies: Vec::new(), + proxy_socket: None, + }; + let cfg = + serde_json::from_str(&ianitor_core::compile::edge(&model, &opts).unwrap()).unwrap(); + let rt = crate::runtime::build(cfg, None).unwrap(); + crate::shared_for(rt, &crate::Options::default()) + } + + /// Pipelined requests on the plain-HTTP listener are each answered, in order (phase 4; the + /// listener used to answer the first and close). + #[tokio::test] + async fn pipelined_requests_are_answered_in_order() { + let app = Arc::new(crate::deadline::HeaderDeadline::new( + HttpApp { + shared: shared(), + challenges: Arc::new(Challenges::default()), + }, + crate::deadline::HEADER_TIMEOUT, + )); + let (mut client, server) = tokio::io::duplex(64 * 1024); + tokio::spawn(async move { + let (_tx, rx) = tokio::sync::watch::channel(false); + let _ = app.process_new(Box::new(server), &rx).await; + }); + client + .write_all( + b"GET /one HTTP/1.1\r\nHost: app.test\r\n\r\n\ + GET / HTTP/1.1\r\nHost: nobody.test\r\n\r\n\ + GET /three?x=1 HTTP/1.1\r\nHost: app.test\r\n\r\n", + ) + .await + .unwrap(); + let mut got = Vec::new(); + let mut buf = [0u8; 4096]; + let end = tokio::time::Instant::now() + std::time::Duration::from_secs(5); + while got + .windows(12) + .filter(|w| w.starts_with(b"HTTP/1.1 ")) + .count() + < 3 + { + match tokio::time::timeout_at(end, client.read(&mut buf)).await { + Ok(Ok(n)) if n > 0 => got.extend_from_slice(&buf[..n]), + _ => break, + } + } + let text = String::from_utf8_lossy(&got); + let statuses: Vec<&str> = text + .match_indices("HTTP/1.1 ") + .map(|(i, _)| &text[i + 9..i + 12]) + .collect(); + assert_eq!(statuses, vec!["308", "404", "308"], "{text}"); + assert!( + text.contains("location: https://app.test:18443/three?x=1"), + "{text}" + ); + } +}