test: Add unit tests for frontend routing and file handling
Some checks failed
Test / get-ci-image (pull_request) Successful in 48s
Test / lint-frontend (pull_request) Successful in 1m10s
Test / frontend-build (pull_request) Successful in 25s
Test / test-frontend (pull_request) Successful in 1m13s
Verify / get-ci-image (pull_request) Successful in 5s
Test / lint-crates (pull_request) Successful in 6m10s
Test / test-crates (pull_request) Failing after 6m22s
Verify / verify-generated-db-entities (pull_request) Successful in 7m5s

This commit is contained in:
GW_MC
2026-04-24 12:08:08 +00:00
parent d9fe053d41
commit 86e9fd42bb
2 changed files with 107 additions and 0 deletions

View File

@@ -67,3 +67,83 @@ async fn get_file_handler(
.map(|html| html.into_response()), .map(|html| html.into_response()),
} }
} }
#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
use super::*;
#[tokio::test]
async fn test_get_index_html() {
let index_html = get_index_html();
assert!(
index_html.is_some(),
"Expected to find index.html in embedded assets"
);
}
#[tokio::test]
async fn test_get_file_handler_existing_file() {
let response = get_file_handler(axum::extract::Path("index.html".to_string())).await;
assert!(
response.is_ok(),
"Expected to successfully retrieve index.html"
);
let response = response.expect("Expected response to be Ok");
assert_eq!(response.status(), axum::http::StatusCode::OK);
assert!(
response
.headers()
.get(axum::http::header::CONTENT_TYPE)
.map(|ct| ct.to_str().unwrap_or(""))
.expect("Content-Type header should be present")
.starts_with("text/html")
);
}
#[tokio::test]
async fn test_get_file_handler_nonexistent_file() {
let response = get_file_handler(axum::extract::Path("nonexistent.txt".to_string())).await;
assert!(
response.is_ok(),
"Expected to fallback to index.html for nonexistent file"
);
let response = response.expect("Expected response to be Ok");
assert_eq!(response.status(), axum::http::StatusCode::OK);
assert!(
response
.headers()
.get(axum::http::header::CONTENT_TYPE)
.map(|ct| ct.to_str().unwrap_or(""))
.expect("Content-Type header should be present")
.starts_with("text/html")
)
}
}
#[cfg(test)]
mod axum_tests {
use super::*;
use axum_test::TestServer;
#[tokio::test]
async fn test_should_return_index_html_for_root_path() {
let router = get_router().await;
let server = TestServer::new(router);
let response = server.get("/").await;
assert_eq!(response.status_code(), 200);
}
#[tokio::test]
async fn test_should_return_index_html_for_nonexistent_path() {
let router = get_router().await;
let server = TestServer::new(router);
let fallback_response = server.get("/nonexistent").await;
assert_eq!(fallback_response.status_code(), 200);
let index_response = server.get("/").await;
assert_eq!(index_response.status_code(), 200);
assert_eq!(fallback_response.text(), index_response.text());
}
}

View File

@@ -7,3 +7,30 @@ pub async fn get_root_router() -> Router {
.merge(frontend::get_router().await) .merge(frontend::get_router().await)
.fallback(frontend::get_fallback_handler().await) .fallback(frontend::get_fallback_handler().await)
} }
#[cfg(test)]
mod tests {
use super::*;
use axum_test::TestServer;
#[tokio::test]
async fn test_should_return_index_html_for_root_path() {
let router = get_root_router().await;
let server = TestServer::new(router);
let response = server.get("/").await;
assert_eq!(response.status_code(), 200);
}
#[tokio::test]
async fn test_should_return_index_html_for_nonexistent_path() {
let router = get_root_router().await;
let server = TestServer::new(router);
let fallback_response = server.get("/nonexistent").await;
assert_eq!(fallback_response.status_code(), 200);
let index_response = server.get("/").await;
assert_eq!(index_response.status_code(), 200);
assert_eq!(fallback_response.text(), index_response.text());
}
}