Files

58 lines
1.7 KiB
Rust

use axum::{
body::Body,
http::{Request, StatusCode, header},
};
use http_body_util::BodyExt;
use task_pilot_backend::state::AppState;
use tempfile::TempDir;
use tower::ServiceExt;
#[tokio::test]
async fn serves_spa_and_keeps_api_404_as_json() {
let temp = TempDir::new().unwrap();
let static_dir = temp.path().join("web");
std::fs::create_dir_all(&static_dir).unwrap();
std::fs::write(
static_dir.join("index.html"),
"<!doctype html><title>TaskPilot SPA</title>",
)
.unwrap();
let pool = task_pilot_backend::database::connect("sqlite::memory:")
.await
.unwrap();
let state = AppState::start(pool, temp.path().join("logs"))
.await
.unwrap();
let app = task_pilot_backend::router_with_static(state.clone(), static_dir);
let page = app
.clone()
.oneshot(
Request::builder()
.uri("/tasks/example")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(page.status(), StatusCode::OK);
let page_body = page.into_body().collect().await.unwrap().to_bytes();
assert!(String::from_utf8_lossy(&page_body).contains("TaskPilot SPA"));
let api_not_found = app
.oneshot(
Request::builder()
.uri("/api/v1/does-not-exist")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(api_not_found.status(), StatusCode::NOT_FOUND);
assert_eq!(
api_not_found.headers().get(header::CONTENT_TYPE),
Some(&header::HeaderValue::from_static("application/json"))
);
state.shutdown();
}