mailApp/tests/health_check.rs

31 lines
814 B
Rust
Raw Normal View History

use std::net::TcpListener;
#[tokio::test]
async fn health_check_works() {
// Arrange
let address = spawn_app();
let client = reqwest::Client::new();
// Act
let response = client
.get(&format!("{}/health_check", &address))
.send()
.await
.expect("Failed to execute request");
// Assert
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 to random port.");
let port = listener.local_addr().unwrap().port();
let server = mail_app::run(listener).expect("Failed to bind address");
// Launch in background
let _spawn = tokio::spawn(server);
format!("http://127.0.0.1:{}", port)
}