feature/embbed-frontend #7

Merged
GW_MC merged 14 commits from feature/embbed-frontend into master 2026-04-25 14:34:56 +08:00
2 changed files with 107 additions and 0 deletions
Showing only changes of commit 86e9fd42bb - Show all commits

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());
}
}