/health_check with integration test implemented
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
1780
Cargo.lock
generated
Normal file
1780
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
19
Cargo.toml
Normal file
19
Cargo.toml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
[package]
|
||||||
|
name = "zero2prod"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Andre Heber <andre.heber@gmx.net>"]
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
path = "src/main.rs"
|
||||||
|
name = "zero2prod"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
actix-web = "4"
|
||||||
|
tokio = { version="1", features = ["macros", "rt-multi-thread"] }
|
||||||
|
reqwest = "0.11"
|
||||||
18
src/lib.rs
Normal file
18
src/lib.rs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||||
|
use actix_web::dev::Server;
|
||||||
|
use std::net::TcpListener;
|
||||||
|
|
||||||
|
async fn health_check() -> HttpResponse {
|
||||||
|
HttpResponse::Ok().into()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
|
||||||
|
let server = HttpServer::new(|| {
|
||||||
|
App::new()
|
||||||
|
.route("/health_check", web::get().to(health_check))
|
||||||
|
})
|
||||||
|
.listen(listener)?
|
||||||
|
.run();
|
||||||
|
|
||||||
|
Ok(server)
|
||||||
|
}
|
||||||
9
src/main.rs
Normal file
9
src/main.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
use std::net::TcpListener;
|
||||||
|
|
||||||
|
use zero2prod::run;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> std::io::Result<()> {
|
||||||
|
let listener = TcpListener::bind("127.0.0.1:8000").expect("Failed to bind random port");
|
||||||
|
run(listener)?.await
|
||||||
|
}
|
||||||
23
tests/health_check.rs
Normal file
23
tests/health_check.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
use std::net::TcpListener;
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn health_check_works() {
|
||||||
|
let address = spawn_app();
|
||||||
|
let health_check_endpoint = format!("{}/health_check", address);
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let response = client.get(health_check_endpoint)
|
||||||
|
.send().await.expect("Failed to execute request.");
|
||||||
|
|
||||||
|
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 random port");
|
||||||
|
let port = listener.local_addr().unwrap().port();
|
||||||
|
|
||||||
|
let server = zero2prod::run(listener).expect("Failed to bind address");
|
||||||
|
tokio::spawn(server);
|
||||||
|
|
||||||
|
format!("http://127.0.0.1:{}", port)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user