mod generated {
include!(concat!(env!("OUT_DIR"), "/serve_assets.rs"));
}
pub(super) fn lookup(path: &str) -> Option<(&'static str, &'static [u8])> {
let index = generated::SERVE_ASSETS
.binary_search_by(|(url, _, _)| (*url).cmp(path))
.ok()?;
let (_url, mime, bytes) = generated::SERVE_ASSETS[index];
Some((mime, bytes))
}
pub(super) fn asset_count() -> usize {
generated::SERVE_ASSETS.len()
}
#[cfg(test)]
mod tests {
use super::{asset_count, generated, lookup};
#[test]
fn the_documented_engines_are_embedded_with_their_mime_types() {
for (path, mime) in [
(
"/static/katex/katex.min.js",
"application/javascript; charset=utf-8",
),
("/static/katex/katex.min.css", "text/css; charset=utf-8"),
("/static/katex/fonts/KaTeX_Main-Regular.woff2", "font/woff2"),
(
"/static/mathjax/tex-svg.js",
"application/javascript; charset=utf-8",
),
] {
let (embedded, bytes) =
lookup(path).unwrap_or_else(|| panic!("{path} must be embedded"));
assert_eq!(embedded, mime, "{path}");
assert!(!bytes.is_empty(), "{path}");
}
let embedded = asset_count();
assert!(embedded >= 23, "katex + fonts + mathjax: {embedded}");
}
#[test]
fn traversal_shapes_never_match_the_closed_table() {
for path in [
"/static/",
"/static/../Cargo.toml",
"/static/katex/../../Cargo.toml",
"/static/%2e%2e/Cargo.toml",
"/static\\katex\\katex.min.js",
"/static/katex/katex.min.js/",
"/static/katex/katex.min.jsx",
] {
assert!(lookup(path).is_none(), "{path} must not resolve");
}
}
#[test]
fn the_generated_table_is_sorted_and_within_the_allowed_shapes() {
let urls: Vec<&str> = generated::SERVE_ASSETS
.iter()
.map(|(url, _, _)| *url)
.collect();
assert!(
urls.windows(2).all(|pair| pair[0] < pair[1]),
"table must be sorted and unique: {urls:?}"
);
for (url, mime, bytes) in generated::SERVE_ASSETS {
assert!(url.starts_with("/static/"), "{url}");
assert!(
matches!(
*mime,
"application/javascript; charset=utf-8"
| "text/css; charset=utf-8"
| "font/woff2"
),
"{url}: unexpected mime {mime}"
);
assert!(!bytes.is_empty(), "{url}");
}
}
#[test]
fn every_static_path_the_page_references_is_embedded() {
let page = crate::serve::run::PAGE_TEMPLATE;
let mut referenced = 0usize;
for rest in page
.match_indices("/static/")
.map(|(index, _)| &page[index..])
{
let token: String = rest
.chars()
.take_while(
|c| matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '/' | '.' | '-' | '_'),
)
.collect();
if token.ends_with(".js") || token.ends_with(".css") || token.ends_with(".woff2") {
assert!(
lookup(&token).is_some(),
"the page references {token} but it is not embedded"
);
referenced += 1;
}
}
assert!(
referenced >= 3,
"FX_LIBS js/css paths expected in the page, saw {referenced}"
);
}
}