fix tests spinning up server
This commit is contained in:
@ -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