Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
test(rust): integration tests + wiremock harness (phase-1)
chopratejasbcc2ad810a39
diff --git a/crates/headroom-proxy/tests/common/mod.rs b/crates/headroom-proxy/tests/common/mod.rsnew file mode 100644index 000000000..d4fa008dc--- /dev/null+++ b/crates/headroom-proxy/tests/common/mod.rs@@ -0,0 +1,66 @@+//! Shared test harness: spin up a Rust proxy bound to an ephemeral port+//! pointed at an arbitrary upstream URL.++use std::net::SocketAddr;+use std::sync::Arc;+use std::time::Duration;++use headroom_proxy::{build_app, AppState, Config};+use tokio::sync::oneshot;+use url::Url;++#[allow(dead_code)]+pub struct ProxyHandle {+ pub addr: SocketAddr,+ pub shutdown: Option<oneshot::Sender<()>>,+ pub task: tokio::task::JoinHandle<()>,+}++#[allow(dead_code)]+impl ProxyHandle {+ pub fn url(&self) -> String {+ format!("http://{}", self.addr)+ }+ pub fn ws_url(&self) -> String {+ format!("ws://{}", self.addr)+ }+ pub async fn shutdown(mut self) {+ if let Some(tx) = self.shutdown.take() {+ let _ = tx.send(());+ }+ let _ = self.task.await;+ }+}++#[allow(dead_code)]+pub async fn start_proxy(upstream: &str) -> ProxyHandle {+ let upstream_url: Url = upstream.parse().expect("valid upstream url");+ let config = Config::for_test(upstream_url);+ let state = AppState::new(config.clone()).expect("app state");+ let app = build_app(state).into_make_service_with_connect_info::<SocketAddr>();+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0")+ .await+ .expect("bind ephemeral");+ let addr = listener.local_addr().expect("local addr");+ let (tx, rx) = oneshot::channel::<()>();+ let task = tokio::spawn(async move {+ let _ = axum::serve(listener, app)+ .with_graceful_shutdown(async move {+ let _ = rx.await;+ })+ .await;+ });+ // Tiny delay to let the listener start accepting on slow CI.+ tokio::time::sleep(Duration::from_millis(20)).await;+ ProxyHandle {+ addr,+ shutdown: Some(tx),+ task,+ }+}++/// Hold a reference to the config so dead_code doesn't strip its use.+#[allow(dead_code)]+pub fn _config_ref() -> Arc<Config> {+ Arc::new(Config::for_test(Url::parse("http://127.0.0.1:1").unwrap()))+}diff --git a/crates/headroom-proxy/tests/integration_body.rs b/crates/headroom-proxy/tests/integration_body.rsnew file mode 100644index 000000000..dc605a5f5--- /dev/null+++ b/crates/headroom-proxy/tests/integration_body.rs@@ -0,0 +1,63 @@+//! Streaming bodies: 5MB POST round-trips; large response streams without full buffering.++mod common;++use bytes::Bytes;+use common::start_proxy;+use futures_util::StreamExt;+use wiremock::matchers::{method, path};+use wiremock::{Mock, MockServer, ResponseTemplate};++#[tokio::test]+async fn five_mb_post_round_trip() {+ let upstream = MockServer::start().await;+ let payload = vec![0x5Au8; 5 * 1024 * 1024];+ let payload_clone = payload.clone();+ Mock::given(method("POST"))+ .and(path("/big"))+ .respond_with(move |req: &wiremock::Request| {+ assert_eq!(req.body.len(), payload_clone.len());+ assert_eq!(&req.body[..], &payload_clone[..]);+ ResponseTemplate::new(200).set_body_string("ok")+ })+ .mount(&upstream)+ .await;++ let proxy = start_proxy(&upstream.uri()).await;+ let resp = reqwest::Client::new()+ .post(format!("{}/big", proxy.url()))+ .body(payload)+ .send()+ .await+ .unwrap();+ assert_eq!(resp.status(), 200);+ proxy.shutdown().await;+}++#[tokio::test]+async fn streaming_response_first_byte_before_done() {+ // wiremock supports delay between body chunks via set_delay; use a single+ // delayed response and make sure first byte arrives via a stream.+ let upstream = MockServer::start().await;+ let body: Bytes = Bytes::from(vec![b'X'; 1024 * 64]);+ Mock::given(method("GET"))+ .and(path("/stream"))+ .respond_with(ResponseTemplate::new(200).set_body_bytes(body.clone()))+ .mount(&upstream)+ .await;+ let proxy = start_proxy(&upstream.uri()).await;+ let resp = reqwest::Client::new()+ .get(format!("{}/stream", proxy.url()))+ .send()+ .await+ .unwrap();+ let mut stream = resp.bytes_stream();+ let first = stream.next().await.unwrap().unwrap();+ assert!(!first.is_empty());+ let mut total = first.len();+ while let Some(chunk) = stream.next().await {+ total += chunk.unwrap().len();+ }+ assert_eq!(total, body.len());+ proxy.shutdown().await;+}diff --git a/crates/headroom-proxy/tests/integration_headers.rs b/crates/headroom-proxy/tests/integration_headers.rsnew file mode 100644index 000000000..d80385a0c--- /dev/null+++ b/crates/headroom-proxy/tests/integration_headers.rs@@ -0,0 +1,95 @@+//! Header passthrough + hop-by-hop filtering + X-Forwarded-* injection.++mod common;++use common::start_proxy;+use wiremock::matchers::{method, path};+use wiremock::{Mock, MockServer, ResponseTemplate};++#[tokio::test]+async fn custom_headers_pass_through_both_ways() {+ let upstream = MockServer::start().await;+ Mock::given(method("GET"))+ .and(path("/h"))+ .respond_with(move |req: &wiremock::Request| {+ assert_eq!(req.headers.get("authorization").unwrap(), "Bearer foo");+ assert_eq!(req.headers.get("x-custom").unwrap(), "bar");+ // Hop-by-hop must be stripped from the upstream-side request.+ assert!(req.headers.get("transfer-encoding").is_none());+ // X-Forwarded-* should be injected.+ let xff = req+ .headers+ .get("x-forwarded-for")+ .unwrap()+ .to_str()+ .unwrap();+ assert!(xff.contains("127.0.0.1"));+ assert!(req.headers.get("x-forwarded-proto").is_some());+ assert!(req.headers.get("x-forwarded-host").is_some());+ ResponseTemplate::new(200)+ .insert_header("x-server-side", "ack")+ .insert_header("x-multi", "v1")+ .append_header("x-multi", "v2")+ // Hop-by-hop on response side must be stripped by the proxy.+ .insert_header("connection", "close")+ .set_body_string("done")+ })+ .mount(&upstream)+ .await;++ let proxy = start_proxy(&upstream.uri()).await;+ let resp = reqwest::Client::new()+ .get(format!("{}/h", proxy.url()))+ .header("authorization", "Bearer foo")+ .header("x-custom", "bar")+ .send()+ .await+ .unwrap();+ assert_eq!(resp.status(), 200);+ assert_eq!(resp.headers().get("x-server-side").unwrap(), "ack");+ assert!(+ resp.headers().get("connection").is_none(),+ "hop-by-hop must be stripped"+ );+ let multi: Vec<_> = resp+ .headers()+ .get_all("x-multi")+ .iter()+ .map(|v| v.to_str().unwrap().to_string())+ .collect();+ assert_eq!(multi, vec!["v1".to_string(), "v2".to_string()]);+ proxy.shutdown().await;+}++#[tokio::test]+async fn xff_appends_existing_value() {+ let upstream = MockServer::start().await;+ Mock::given(method("GET"))+ .and(path("/xff"))+ .respond_with(move |req: &wiremock::Request| {+ let xff = req+ .headers+ .get("x-forwarded-for")+ .unwrap()+ .to_str()+ .unwrap();+ // existing 1.2.3.4 must be preserved + appended.+ assert!(+ xff.starts_with("1.2.3.4"),+ "expected appended xff, got: {xff}"+ );+ assert!(xff.contains("127.0.0.1"));+ ResponseTemplate::new(200)+ })+ .mount(&upstream)+ .await;+ let proxy = start_proxy(&upstream.uri()).await;+ let resp = reqwest::Client::new()+ .get(format!("{}/xff", proxy.url()))+ .header("x-forwarded-for", "1.2.3.4")+ .send()+ .await+ .unwrap();+ assert_eq!(resp.status(), 200);+ proxy.shutdown().await;+}diff --git a/crates/headroom-proxy/tests/integration_health.rs b/crates/headroom-proxy/tests/integration_health.rsnew file mode 100644index 000000000..19a975ae2--- /dev/null+++ b/crates/headroom-proxy/tests/integration_health.rs@@ -0,0 +1,43 @@+//! Health endpoints: own /healthz always 200; /healthz/upstream reflects upstream.++mod common;++use common::start_proxy;+use wiremock::matchers::{method, path};+use wiremock::{Mock, MockServer, ResponseTemplate};++#[tokio::test]+async fn healthz_ok_when_upstream_down() {+ let proxy = start_proxy("http://127.0.0.1:1").await; // unroutable port+ let resp = reqwest::get(format!("{}/healthz", proxy.url()))+ .await+ .unwrap();+ assert_eq!(resp.status(), 200);+ proxy.shutdown().await;+}++#[tokio::test]+async fn healthz_upstream_503_when_upstream_down() {+ let proxy = start_proxy("http://127.0.0.1:1").await;+ let resp = reqwest::get(format!("{}/healthz/upstream", proxy.url()))+ .await+ .unwrap();+ assert_eq!(resp.status(), 503);+ proxy.shutdown().await;+}++#[tokio::test]+async fn healthz_upstream_200_when_upstream_healthy() {+ let upstream = MockServer::start().await;+ Mock::given(method("GET"))+ .and(path("/healthz"))+ .respond_with(ResponseTemplate::new(200).set_body_string("ok"))+ .mount(&upstream)+ .await;+ let proxy = start_proxy(&upstream.uri()).await;+ let resp = reqwest::get(format!("{}/healthz/upstream", proxy.url()))+ .await+ .unwrap();+ assert_eq!(resp.status(), 200);+ proxy.shutdown().await;+}diff --git a/crates/headroom-proxy/tests/integration_http.rs b/crates/headroom-proxy/tests/integration_http.rsnew file mode 100644index 000000000..e37f2c17b--- /dev/null+++ b/crates/headroom-proxy/tests/integration_http.rs@@ -0,0 +1,135 @@+//! HTTP method round-trip + status passthrough.++mod common;++use common::start_proxy;+use wiremock::matchers::{any, method, path, query_param};+use wiremock::{Mock, MockServer, ResponseTemplate};++#[tokio::test]+async fn all_methods_round_trip_with_body() {+ let upstream = MockServer::start().await;++ for m in ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"] {+ Mock::given(method(m))+ .and(path("/echo"))+ .respond_with(+ ResponseTemplate::new(200)+ .insert_header("x-from-upstream", "yes")+ .set_body_string(format!("ok-{m}")),+ )+ .mount(&upstream)+ .await;+ }+ Mock::given(method("HEAD"))+ .and(path("/echo"))+ .respond_with(ResponseTemplate::new(200).insert_header("x-from-upstream", "yes"))+ .mount(&upstream)+ .await;++ let proxy = start_proxy(&upstream.uri()).await;+ let client = reqwest::Client::new();++ for m in ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"] {+ let url = format!("{}/echo", proxy.url());+ let resp = client+ .request(reqwest::Method::from_bytes(m.as_bytes()).unwrap(), &url)+ .body(format!("payload-{m}"))+ .send()+ .await+ .unwrap();+ assert_eq!(resp.status(), 200, "method {m}");+ assert_eq!(resp.headers().get("x-from-upstream").unwrap(), "yes");+ let body = resp.text().await.unwrap();+ assert_eq!(body, format!("ok-{m}"));+ }++ // HEAD has no body but should round-trip headers + status.+ let head_resp = client+ .head(format!("{}/echo", proxy.url()))+ .send()+ .await+ .unwrap();+ assert_eq!(head_resp.status(), 200);+ assert_eq!(head_resp.headers().get("x-from-upstream").unwrap(), "yes");++ proxy.shutdown().await;+}++#[tokio::test]+async fn upstream_error_codes_passthrough() {+ let upstream = MockServer::start().await;+ for status in [404u16, 500, 502] {+ Mock::given(method("GET"))+ .and(path(format!("/code/{status}")))+ .respond_with(ResponseTemplate::new(status).set_body_string(format!("err-{status}")))+ .mount(&upstream)+ .await;+ }+ let proxy = start_proxy(&upstream.uri()).await;+ let client = reqwest::Client::new();+ for status in [404u16, 500, 502] {+ let resp = client+ .get(format!("{}/code/{status}", proxy.url()))+ .send()+ .await+ .unwrap();+ assert_eq!(resp.status().as_u16(), status);+ assert_eq!(resp.text().await.unwrap(), format!("err-{status}"));+ }+ proxy.shutdown().await;+}++#[tokio::test]+async fn query_string_preserved() {+ let upstream = MockServer::start().await;+ Mock::given(method("GET"))+ .and(path("/q"))+ .and(query_param("a", "1"))+ .and(query_param("b", "two"))+ .respond_with(ResponseTemplate::new(200).set_body_string("matched"))+ .mount(&upstream)+ .await;+ Mock::given(any())+ .respond_with(ResponseTemplate::new(418))+ .mount(&upstream)+ .await;++ let proxy = start_proxy(&upstream.uri()).await;+ let resp = reqwest::get(format!("{}/q?a=1&b=two", proxy.url()))+ .await+ .unwrap();+ assert_eq!(resp.status(), 200);+ assert_eq!(resp.text().await.unwrap(), "matched");+ proxy.shutdown().await;+}++#[tokio::test]+async fn one_mb_post_streams_through() {+ let upstream = MockServer::start().await;+ let payload = vec![0xABu8; 1024 * 1024];+ let payload_clone = payload.clone();+ Mock::given(method("POST"))+ .and(path("/upload"))+ .respond_with(move |req: &wiremock::Request| {+ assert_eq!(+ req.body.len(),+ payload_clone.len(),+ "upstream got full body"+ );+ assert_eq!(&req.body[..], &payload_clone[..]);+ ResponseTemplate::new(200).set_body_string("uploaded")+ })+ .mount(&upstream)+ .await;+ let proxy = start_proxy(&upstream.uri()).await;+ let resp = reqwest::Client::new()+ .post(format!("{}/upload", proxy.url()))+ .body(payload)+ .send()+ .await+ .unwrap();+ assert_eq!(resp.status(), 200);+ assert_eq!(resp.text().await.unwrap(), "uploaded");+ proxy.shutdown().await;+}diff --git a/crates/headroom-proxy/tests/integration_sse.rs b/crates/headroom-proxy/tests/integration_sse.rsnew file mode 100644index 000000000..0eadbf5f9--- /dev/null+++ b/crates/headroom-proxy/tests/integration_sse.rs@@ -0,0 +1,148 @@+//! SSE chunk fidelity: events stream through with timing preserved.++mod common;++use std::convert::Infallible;+use std::net::SocketAddr;+use std::sync::Arc;+use std::time::{Duration, Instant};++use bytes::Bytes;+use common::start_proxy;+use futures_util::StreamExt;+use http_body_util::StreamBody;+use hyper::body::Frame;+use hyper::service::service_fn;+use hyper::{Request, Response};+use hyper_util::rt::TokioIo;+use tokio::sync::Notify;++async fn sse_upstream(on_disconnect: Arc<Notify>) -> (SocketAddr, tokio::task::JoinHandle<()>) {+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let addr = listener.local_addr().unwrap();+ let task = tokio::spawn(async move {+ loop {+ let Ok((stream, _)) = listener.accept().await else {+ break;+ };+ let on_disconnect = on_disconnect.clone();+ tokio::spawn(async move {+ let io = TokioIo::new(stream);+ let _ = hyper::server::conn::http1::Builder::new()+ .serve_connection(+ io,+ service_fn(move |_req: Request<hyper::body::Incoming>| {+ let on_disconnect = on_disconnect.clone();+ async move {+ let (tx, rx) = tokio::sync::mpsc::channel::<+ Result<Frame<Bytes>, std::io::Error>,+ >(4);+ tokio::spawn(async move {+ for i in 0..10u32 {+ let payload = format!("data: event-{i}\n\n");+ if tx+ .send(Ok(Frame::data(Bytes::from(payload))))+ .await+ .is_err()+ {+ // Client disconnected — notify the test.+ on_disconnect.notify_one();+ return;+ }+ tokio::time::sleep(Duration::from_millis(50)).await;+ }+ });+ let stream = tokio_stream::wrappers::ReceiverStream::new(rx);+ let body = StreamBody::new(stream);+ Ok::<_, Infallible>(+ Response::builder()+ .status(200)+ .header("content-type", "text/event-stream")+ .header("cache-control", "no-cache")+ .body(body)+ .unwrap(),+ )+ }+ }),+ )+ .await;+ });+ }+ });+ (addr, task)+}++#[tokio::test]+async fn sse_chunks_arrive_with_preserved_timing() {+ let on_disconnect = Arc::new(Notify::new());+ let (addr, _server) = sse_upstream(on_disconnect.clone()).await;+ let proxy = start_proxy(&format!("http://{addr}")).await;++ let resp = reqwest::Client::new()+ .get(format!("{}/sse", proxy.url()))+ .send()+ .await+ .unwrap();+ assert_eq!(resp.status(), 200);+ assert_eq!(+ resp.headers().get("content-type").unwrap(),+ "text/event-stream"+ );+ let mut stream = resp.bytes_stream();++ let mut events = Vec::new();+ let mut last = Instant::now();+ let mut max_gap = Duration::ZERO;+ while let Some(chunk) = stream.next().await {+ let chunk = chunk.unwrap();+ let now = Instant::now();+ let gap = now.duration_since(last);+ last = now;+ // SSE chunks aren't always 1:1 with events on slow boxes — accumulate+ // and split per `\n\n`.+ let s = String::from_utf8_lossy(&chunk).to_string();+ events.push(s);+ if events.len() > 1 && gap > max_gap {+ max_gap = gap;+ }+ }+ let combined = events.join("");+ let parsed: Vec<&str> = combined.split("\n\n").filter(|s| !s.is_empty()).collect();+ assert_eq!(parsed.len(), 10, "got events: {parsed:?}");+ for (i, ev) in parsed.iter().enumerate() {+ assert_eq!(ev.trim(), format!("data: event-{i}"));+ }+ // Loose CI bound — chunks should not be buffered until end. Each event was+ // ~50ms apart, so the longest inter-chunk gap should be well under 500ms.+ assert!(+ max_gap < Duration::from_millis(500),+ "max chunk gap {max_gap:?} suggests buffering"+ );++ proxy.shutdown().await;+}++#[tokio::test]+async fn client_disconnect_propagates_to_upstream() {+ let on_disconnect = Arc::new(Notify::new());+ let (addr, _server) = sse_upstream(on_disconnect.clone()).await;+ let proxy = start_proxy(&format!("http://{addr}")).await;++ let client = reqwest::Client::new();+ let resp = client+ .get(format!("{}/sse", proxy.url()))+ .send()+ .await+ .unwrap();+ let mut stream = resp.bytes_stream();+ // Read the first chunk, then drop the stream to disconnect.+ let _ = stream.next().await;+ drop(stream);++ // Upstream should observe disconnect within 1s.+ let observed = tokio::time::timeout(Duration::from_secs(2), on_disconnect.notified())+ .await+ .is_ok();+ assert!(observed, "upstream did not see client disconnect within 2s");+ proxy.shutdown().await;+}diff --git a/crates/headroom-proxy/tests/integration_ws.rs b/crates/headroom-proxy/tests/integration_ws.rsnew file mode 100644index 000000000..7f9688d87--- /dev/null+++ b/crates/headroom-proxy/tests/integration_ws.rs@@ -0,0 +1,96 @@+//! WebSocket proxy: bidirectional pump + close propagation.++mod common;++use std::net::SocketAddr;+use std::time::Duration;++use common::start_proxy;+use futures_util::{SinkExt, StreamExt};+use tokio_tungstenite::tungstenite::protocol::CloseFrame;+use tokio_tungstenite::tungstenite::Message;++/// Spawns an upstream WS echo server. Handshake uses tungstenite over a raw TCP listener.+async fn echo_upstream() -> (SocketAddr, tokio::sync::oneshot::Sender<()>) {+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let addr = listener.local_addr().unwrap();+ let (stop_tx, mut stop_rx) = tokio::sync::oneshot::channel();+ tokio::spawn(async move {+ loop {+ tokio::select! {+ _ = &mut stop_rx => break,+ accepted = listener.accept() => {+ let Ok((stream, _)) = accepted else { continue };+ tokio::spawn(async move {+ let Ok(ws) = tokio_tungstenite::accept_async(stream).await else { return };+ let (mut sink, mut src) = ws.split();+ while let Some(Ok(msg)) = src.next().await {+ match msg {+ Message::Close(cf) => {+ let _ = sink.send(Message::Close(cf)).await;+ break;+ }+ m => {+ if sink.send(m).await.is_err() { break; }+ }+ }+ }+ });+ }+ }+ }+ });+ (addr, stop_tx)+}++#[tokio::test]+async fn ws_text_and_binary_round_trip() {+ let (upstream_addr, _stop) = echo_upstream().await;+ let proxy = start_proxy(&format!("http://{upstream_addr}")).await;++ let url = format!("{}/ws", proxy.ws_url());+ let (mut ws, _) = tokio_tungstenite::connect_async(&url).await.unwrap();++ for i in 0..5 {+ let m = format!("hello-{i}");+ ws.send(Message::Text(m.clone())).await.unwrap();+ let echoed = ws.next().await.unwrap().unwrap();+ match echoed {+ Message::Text(t) => assert_eq!(t.as_str(), m),+ other => panic!("expected text, got {other:?}"),+ }+ }+ for i in 0..5u8 {+ let m: Vec<u8> = (0..32u8).map(|b| b ^ i).collect();+ ws.send(Message::Binary(m.clone())).await.unwrap();+ let echoed = ws.next().await.unwrap().unwrap();+ match echoed {+ Message::Binary(b) => assert_eq!(b.to_vec(), m),+ other => panic!("expected binary, got {other:?}"),+ }+ }+ ws.send(Message::Close(None)).await.unwrap();+ proxy.shutdown().await;+}++#[tokio::test]+async fn ws_client_close_propagates() {+ let (upstream_addr, _stop) = echo_upstream().await;+ let proxy = start_proxy(&format!("http://{upstream_addr}")).await;++ let (mut ws, _) = tokio_tungstenite::connect_async(format!("{}/ws", proxy.ws_url()))+ .await+ .unwrap();++ ws.send(Message::Close(Some(CloseFrame {+ code: tokio_tungstenite::tungstenite::protocol::frame::coding::CloseCode::Normal,+ reason: "bye".into(),+ })))+ .await+ .unwrap();++ // Server-side echo will reflect the close; we should see a Close back.+ let got = tokio::time::timeout(Duration::from_secs(3), ws.next()).await;+ assert!(got.is_ok(), "expected close echo within 3s");+ proxy.shutdown().await;+}