use futures_util::{SinkExt, StreamExt};
use mockforge_core::ws_proxy::{WsProxyConfig, WsProxyHandler};
use tokio_tungstenite::tungstenite::protocol::Message;
#[tokio::test]
async fn debug_ws_proxy_basic() {
let mut config = WsProxyConfig::new("ws://127.0.0.1:9999".to_string());
config.enabled = true;
config.passthrough_by_default = true;
let proxy_handler = WsProxyHandler::new(config);
let proxy_listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let proxy_addr = proxy_listener.local_addr().unwrap();
let proxy_server = tokio::spawn(async move {
let app = mockforge_ws::router_with_proxy(proxy_handler);
axum::serve(proxy_listener, app).await.unwrap()
});
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
let url = format!("ws://{}/ws", proxy_addr);
println!("Connecting to: {}", url);
match tokio_tungstenite::connect_async(url).await {
Ok((mut ws_stream, _)) => {
println!("Connected successfully!");
if let Err(e) = ws_stream.send(Message::Text("test".into())).await {
println!("Failed to send message: {}", e);
} else {
println!("Message sent successfully");
}
match tokio::time::timeout(tokio::time::Duration::from_secs(5), ws_stream.next()).await
{
Ok(Some(Ok(msg))) => {
println!("Received message: {:?}", msg);
}
Ok(Some(Err(e))) => {
println!("Received error: {}", e);
}
Ok(None) => {
println!("Connection closed by server");
}
Err(_) => {
println!("Timeout waiting for message");
}
}
}
Err(e) => {
println!("Failed to connect: {}", e);
}
}
drop(proxy_server);
}