Skip to content

Instantly share code, notes, and snippets.

@shubhamkumar13
Last active February 1, 2021 11:30
Show Gist options
  • Save shubhamkumar13/a91fe97a94dcd43acb10912a1aeb2265 to your computer and use it in GitHub Desktop.
Save shubhamkumar13/a91fe97a94dcd43acb10912a1aeb2265 to your computer and use it in GitHub Desktop.
zero2prod
     Running target/debug/deps/health_check-1b83a7cb3a2b9b85

running 1 test
test health_check_works ... FAILED

failures:

---- health_check_works stdout ----
thread 'health_check_works' panicked at 'Failed to spawn our app.: Os { code: 98, kind: AddrInUse, message: "Address already in use" }', tests/health_check.rs:9:23
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Panic in Arbiter thread.


failures:
    health_check_works

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.13s
// tests/health_check.rs
// `actix_rt::test` is the testing equivalent of `actix_rt::main`.
// It also spares you from having to specify the `#[test]` attribute.
// You can inspect what code gets generated using
// `cargo expand --test health_check` (<- name of the test file)
#[actix_rt::test]
async fn health_check_works() {
// Arrange
spawn_app().await.expect("Failed to spawn our app.");
// We need to bring in `reqwest`
// to perform HTTP requests against our application.
// Add `reqwest = "0.10"` under
// `[dev-dependencies]` in Cargo.toml
let client = reqwest::Client::new();
// Act
let response = client
.get("http://127.0.0.1:8000/health_check")
.send()
.await
.expect("Failed to execute request.");
// Assert
assert!(response.status().is_success());
assert_eq!(Some(0), response.content_length());
}
async fn spawn_app() -> std::io::Result<()> {
zero2prod::run().await
}
use actix_web::{web, App, HttpResponse, HttpServer};
async fn health_check() -> HttpResponse {
HttpResponse::Ok().finish()
}
// We need to mark `run` as public.
// It is no longer a binary entrypoint, therefore we can mark it as async
// without having to use any proc-macro incantation.
pub async fn run() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.route("/health_check", web::get().to(health_check))
})
.bind("127.0.0.1:8000")?
.run()
.await
}
use zero2prod::run;
#[actix_rt::main]
async fn main() -> std::io::Result<()> {
run().await
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment