examples: fix using_serve_dir_with_assets_fallback to not expose assets at root (#3762)

Co-authored-by: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
Zelys
2026-05-16 22:54:23 +02:00
committed by GitHub
co-authored by Claude Sonnet 4.6
parent 7a37cdd00a
commit a480d71f6d
4 changed files with 46 additions and 5 deletions
+28
View File
@@ -0,0 +1,28 @@
use super::using_serve_dir_with_assets_fallback;
use axum::{body::Body, http::Request, http::StatusCode};
use http_body_util::BodyExt;
use tower::ServiceExt;
const INDEX_HTML_CONTENT: &str = include_str!("../assets/index.html");
// `/script.js` at the root is not an asset under `/assets`, so it falls through to the SPA
// fallback. The fallback serves `index.html` with a 404 status -- the path wasn't found, but
// the SPA handles routing client-side.
#[tokio::test]
async fn assets_not_served_at_root() {
let app = using_serve_dir_with_assets_fallback();
let response = app
.oneshot(
Request::builder()
.uri("/script.js")
.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_str = String::from_utf8(body.to_vec()).unwrap();
assert_eq!(body_str, INDEX_HTML_CONTENT);
}