health_check test

This commit is contained in:
2025-09-16 23:13:56 +02:00
parent 3721de1e97
commit 244ee3f677
5 changed files with 1782 additions and 2 deletions

27
tests/health_check.rs Normal file
View File

@@ -0,0 +1,27 @@
use tokio::net::TcpListener;
use zero2prod::app;
#[tokio::test]
async fn health_check_works() {
let addr = spawn_app().await;
let client = reqwest::Client::new();
let response = client
.get(&format!("http://{}/health_check", addr))
.send()
.await
.expect("Failed to execute request");
assert!(response.status().is_success());
assert_eq!(Some(0), response.content_length());
}
async fn spawn_app() -> String {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
axum::serve(listener, app()).await.unwrap();
});
addr.to_string()
}