fix tests spinning up server
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -856,6 +856,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"clap",
|
"clap",
|
||||||
|
"http-body-util",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|||||||
@ -6,6 +6,7 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
axum = "0.8.4"
|
axum = "0.8.4"
|
||||||
clap = { version = "4.5.41", features = ["derive"] }
|
clap = { version = "4.5.41", features = ["derive"] }
|
||||||
|
http-body-util = "0.1.3"
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
serde_json = "1.0.140"
|
serde_json = "1.0.140"
|
||||||
tokio = { version = "1.46.1", features = ["full"] }
|
tokio = { version = "1.46.1", features = ["full"] }
|
||||||
@ -16,6 +17,7 @@ tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
reqwest = { version = "0.12.22", features = ["json"] }
|
reqwest = { version = "0.12.22", features = ["json"] }
|
||||||
|
tower = { version = "0.5.2", features = ["util"] }
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
mod routes;
|
mod routes;
|
||||||
|
pub use routes::app;
|
||||||
|
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
|
|
||||||
|
|||||||
@ -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
63
tests/endpoint_test.rs
Normal 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>"));
|
||||||
|
}
|
||||||
@ -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(())
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user