Re-Arrange structure. Add unit testing.

This commit is contained in:
2023-08-31 22:15:34 +10:00
parent ab685d711a
commit bd9f5a7819
5 changed files with 526 additions and 18 deletions
+30
View File
@@ -0,0 +1,30 @@
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)
}