Skip to content
Myr

tests.rs

This file

Path

examples/static-file-server/src/tests.rs

28 lines · 1022 bytes · Rust

Last changed by

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

History of this file

Take it

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

lines 1–28why, one step back
1use super::using_serve_dir_with_assets_fallback;
2use axum::{body::Body, http::Request, http::StatusCode};3use http_body_util::BodyExt;4use tower::ServiceExt;56const INDEX_HTML_CONTENT: &str = include_str!("../assets/index.html");78// `/script.js` at the root is not an asset under `/assets`, so it falls through to the SPA9// fallback. The fallback serves `index.html` with a 404 status -- the path wasn't found, but10// the SPA handles routing client-side.11#[tokio::test]12async fn assets_not_served_at_root() {13 let app = using_serve_dir_with_assets_fallback();14 let response = app15 .oneshot(16 Request::builder()17 .uri("/script.js")18 .body(Body::empty())19 .unwrap(),20 )21 .await22 .unwrap();2324 assert_eq!(response.status(), StatusCode::NOT_FOUND);25 let body = response.into_body().collect().await.unwrap().to_bytes();26 let body_str = String::from_utf8(body.to_vec()).unwrap();27 assert_eq!(body_str, INDEX_HTML_CONTENT);28}