test: write tests

Finished chapter 3.5
This commit is contained in:
Kristofers Solo
2024-03-23 13:55:52 +02:00
parent 60466ef61c
commit c79eca74ee
5 changed files with 577 additions and 17 deletions

25
tests/health_check.rs Normal file
View File

@@ -0,0 +1,25 @@
use std::net::SocketAddr;
use tokio::net::TcpListener;
use zero2prod::app;
#[tokio::test]
async fn health_check() {
spawn_app();
let client = reqwest::Client::new();
let response = client
.get("http://localhost:8000/health_check")
.send()
.await
.expect("Failed to execute request.");
assert!(response.status().is_success());
assert_eq!(Some(0), response.content_length());
}
fn spawn_app() {
let addr = SocketAddr::from(([127, 0, 0, 1], 8000));
let _ = tokio::spawn(async move {
let listener = TcpListener::bind(addr).await.unwrap();
axum::serve(listener, app()).await.unwrap();
});
}