use proxyapi::{Proxy, ProxyConfig, ProxyEvent, ProxyMode};
use tokio::sync::mpsc;
#[tokio::test]
async fn test_reverse_proxy_starts_and_shuts_down() {
let _ = rustls::crypto::ring::default_provider().install_default();
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
drop(listener);
let ca_dir = tempfile::tempdir().unwrap();
let (event_tx, _event_rx) = mpsc::channel::<ProxyEvent>(100);
let config = ProxyConfig {
addr,
mode: ProxyMode::Reverse {
target: "http://127.0.0.1:9999".parse().unwrap(),
},
event_tx,
ca_dir: ca_dir.path().to_path_buf(),
intercept: None,
#[cfg(feature = "scripting")]
script_path: None,
replay_rx: None,
};
let proxy = Proxy::new(config);
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
let handle = tokio::spawn(async move {
proxy
.start(async {
shutdown_rx.await.ok();
})
.await
});
let deadline = tokio::time::Instant::now() + tokio::time::Duration::from_secs(10);
loop {
match tokio::net::TcpStream::connect(addr).await {
Ok(_) => break,
Err(_) if tokio::time::Instant::now() < deadline => {
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
}
Err(e) => panic!("Proxy failed to start within timeout: {e}"),
}
}
let _ = shutdown_tx.send(());
let result = handle.await.unwrap();
assert!(result.is_ok());
}