use axum::body::to_bytes;
use axum::extract::Path;
use axum::http::{HeaderMap, StatusCode};
use forma_server::assets::serve_asset;
use rust_embed::Embed;
#[derive(Embed)]
#[folder = "tests/fixtures/asset-sets/a/"]
struct SetA;
#[derive(Embed)]
#[folder = "tests/fixtures/asset-sets/b/"]
struct SetB;
async fn fetch<A: Embed + 'static>(name: &str) -> (StatusCode, Vec<u8>) {
let response = serve_asset::<A>(Path(name.to_string()), HeaderMap::new()).await;
let status = response.status();
let body = to_bytes(response.into_body(), usize::MAX)
.await
.expect("an asset body is in memory and cannot fail to collect");
(status, body.to_vec())
}
fn ok(body: &[u8]) -> (StatusCode, Vec<u8>) {
(StatusCode::OK, body.to_vec())
}
#[tokio::test]
async fn two_embed_types_each_serve_their_own_assets() {
for round in 1..=3 {
assert_eq!(
fetch::<SetA>("a.aaaaaaaa.js").await,
ok(b"export default \"a\";\n"),
"round {round}: SetA must serve its own asset"
);
assert_eq!(
fetch::<SetB>("b.bbbbbbbb.js").await,
ok(b"export default \"bb\";\n"),
"round {round}: SetB must serve its own asset"
);
assert_eq!(
fetch::<SetA>("b.bbbbbbbb.js").await.0,
StatusCode::NOT_FOUND,
"round {round}: SetA must not serve SetB's asset"
);
assert_eq!(
fetch::<SetB>("a.aaaaaaaa.js").await.0,
StatusCode::NOT_FOUND,
"round {round}: SetB must not serve SetA's asset"
);
assert_eq!(
fetch::<SetA>("shared.css").await,
ok(b".a{}\n"),
"round {round}: SetA's shared.css"
);
assert_eq!(
fetch::<SetB>("shared.css").await,
ok(b".bb{}\n"),
"round {round}: SetB's shared.css"
);
}
}