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:41:40 +00:00
|
|
|
#[tokio::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());
|
|
|
|
}
|