use std::net::{SocketAddr, TcpListener}; struct TestApp { addr: SocketAddr, } #[tokio::test] async fn health_check_works() { let TestApp { addr, .. } = spawn_app().await; let client = reqwest::Client::new(); let response = client .get(format!("http://{addr}/health_check")) .send() .await .expect("Failed to execute request."); assert!(response.status().is_success()); assert_eq!(Some(0), response.content_length()); } // fn spawn_app() { // let server = learn_axum::run().expect("Failed to bind address."); // tokio::spawn(server); // } async fn spawn_app() -> TestApp { let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind to random port"); let addr = listener.local_addr().unwrap(); let server = learn_axum::run(listener).expect("Failed to bind to address"); tokio::spawn(server); TestApp { addr } }