Files
zero2prod/tests/health_check.rs
2024-01-27 16:53:22 +01:00

24 lines
709 B
Rust

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