mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-11-15 21:21:00 +00:00
31 lines
940 B
Rust
31 lines
940 B
Rust
use chapter03_0::run;
|
|
use std::net::TcpListener;
|
|
|
|
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)
|
|
}
|
|
|
|
#[actix_rt::test]
|
|
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());
|
|
}
|