zero-to-production/tests/health_check.rs

32 lines
939 B
Rust
Raw Normal View History

2020-08-23 10:53:08 +00:00
use std::net::TcpListener;
2020-12-05 16:50:21 +00:00
use zero2prod::run;
2020-08-23 10:53:08 +00:00
fn spawn_app() -> String {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
// We retrieve the port assigned to us by the OS
let port = listener.local_addr().unwrap().port();
let server = run(listener).expect("Failed to bind address");
let _ = tokio::spawn(server);
// We return the application address to the caller!
format!("http://127.0.0.1:{}", port)
}
2021-12-26 14:37:37 +00:00
#[actix_web::test]
2020-08-23 10:53:08 +00:00
async fn health_check_works() {
// Arrange
let address = spawn_app();
let client = reqwest::Client::new();
// Act
let response = client
// Use the returned application address
.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());
}