fix tests spinning up server
Some checks failed
Cargo / check (push) Successful in 28s
Cargo / build (push) Successful in 34s
Cargo / test (push) Failing after 37s

This commit is contained in:
2025-07-18 15:10:54 +01:00
parent 8a2b1d504a
commit 2fff50ec0a
6 changed files with 67 additions and 30 deletions

1
Cargo.lock generated
View File

@ -856,6 +856,7 @@ version = "0.1.0"
dependencies = [
"axum",
"clap",
"http-body-util",
"reqwest",
"serde",
"serde_json",

View File

@ -6,6 +6,7 @@ edition = "2024"
[dependencies]
axum = "0.8.4"
clap = { version = "4.5.41", features = ["derive"] }
http-body-util = "0.1.3"
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
tokio = { version = "1.46.1", features = ["full"] }
@ -16,6 +17,7 @@ tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
[dev-dependencies]
reqwest = { version = "0.12.22", features = ["json"] }
tower = { version = "0.5.2", features = ["util"] }
[lib]
path = "src/lib.rs"

View File

@ -1,4 +1,5 @@
mod routes;
pub use routes::app;
use std::sync::mpsc;

View File

@ -1,16 +0,0 @@
use std::net::TcpListener;
pub fn spawn_app() -> String {
let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
let _ = listener.set_nonblocking(true);
// We retrieve the port assigned to us by the OS
let port = listener.local_addr().unwrap().port();
let server = nuchat::run(
tokio::net::TcpListener::from_std(listener)
.expect("Failed to convert from_std to tokio listener"),
)
.expect("Failed to bind address");
tokio::spawn(server.into_future());
// We return the application address to the caller!
format!("http://127.0.0.1:{port}")
}

63
tests/endpoint_test.rs Normal file
View File

@ -0,0 +1,63 @@
use axum::{
body::Body,
http::{Request, StatusCode},
};
use http_body_util::BodyExt; // for `collect`
use tower::ServiceExt; // for `oneshot`
#[tokio::test]
async fn healthcheck_returns_healthy() {
let (app, _) = nuchat::app();
let response = app
.oneshot(
Request::builder()
.uri("/api/healthcheck")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let body = response.into_body().collect().await.unwrap().to_bytes();
assert_eq!(&body[..], br#"{"healthy":true}"#);
}
#[tokio::test]
async fn root_returns_spa() {
let (app, _) = nuchat::app();
let response = app
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let body = response.into_body().collect().await.unwrap().to_bytes();
let body = String::from_utf8(body.into_iter().collect()).unwrap();
assert!(body.starts_with("<!DOCTYPE html>"));
}
#[tokio::test]
async fn unkown_fallsback_to_spa() {
let (app, _) = nuchat::app();
let response = app
.oneshot(
Request::builder()
.uri("/asdfasdfa") // unknown url
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND);
let body = response.into_body().collect().await.unwrap().to_bytes();
let body = String::from_utf8(body.into_iter().collect()).unwrap();
assert!(body.starts_with("<!DOCTYPE html>"));
}

View File

@ -1,14 +0,0 @@
mod common;
use common::spawn_app;
use reqwest::StatusCode;
#[tokio::test]
async fn test_healthcheck() -> reqwest::Result<()> {
let address = spawn_app();
let response = reqwest::get(format!("{address}/api/healthcheck")).await?;
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(response.text().await?, r#"{"healthy":true}"#);
Ok(())
}