use churust_core::{Call, Churust};
use std::time::Duration;
#[tokio::test]
async fn serves_real_http_and_shuts_down() {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
drop(listener);
let app = Churust::server()
.host(addr.ip().to_string())
.port(addr.port())
.routing(|r| {
r.get("/ping", |_c: Call| async { "pong" });
})
.build();
let (tx, rx) = tokio::sync::oneshot::channel::<()>();
let server = tokio::spawn(async move {
app.start_with_shutdown(async move {
let _ = rx.await;
})
.await
.unwrap();
});
tokio::time::sleep(Duration::from_millis(100)).await;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let mut stream = tokio::net::TcpStream::connect(addr).await.unwrap();
stream
.write_all(
format!("GET /ping HTTP/1.1\r\nHost: {addr}\r\nConnection: close\r\n\r\n").as_bytes(),
)
.await
.unwrap();
let mut buf = Vec::new();
stream.read_to_end(&mut buf).await.unwrap();
let text = String::from_utf8_lossy(&buf);
assert!(text.starts_with("HTTP/1.1 200"), "got: {text}");
assert!(text.contains("pong"), "got: {text}");
let _ = tx.send(());
let _ = tokio::time::timeout(Duration::from_secs(2), server).await;
}