mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-11-15 21:21:00 +00:00
Populate Chapter3 (Part0)
This commit is contained in:
parent
7e63ca87bd
commit
8f86c3bccf
5 changed files with 2001 additions and 2 deletions
1943
Cargo.lock
generated
1943
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -7,3 +7,9 @@ edition = "2018"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
actix-web = "2.0.0"
|
||||
actix-rt = "1.1.1"
|
||||
tokio = "0.2.22"
|
||||
|
||||
[dev-dependencies]
|
||||
reqwest = "0.10.7"
|
||||
|
|
14
chapter03-0/src/lib.rs
Normal file
14
chapter03-0/src/lib.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use actix_web::dev::Server;
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
use std::net::TcpListener;
|
||||
|
||||
async fn health_check() -> HttpResponse {
|
||||
HttpResponse::Ok().finish()
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
|
@ -1,3 +1,8 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use chapter03_0::run;
|
||||
use std::net::TcpListener;
|
||||
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let address = TcpListener::bind("127.0.0.1:8000")?;
|
||||
run(address)?.await
|
||||
}
|
||||
|
|
31
chapter03-0/tests/health_check.rs
Normal file
31
chapter03-0/tests/health_check.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
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());
|
||||
}
|
Loading…
Reference in a new issue