mini-static 0.31.2

A secure, async static file server with streaming, traversal protection, and connection limits.
Documentation
use super::*;

// `select_precompressed_sidecar` only ever appends a static extension literal
// (".br"/".gz") to the `path` it's given — it never re-joins against `root` or
// re-parses a request-path string, so it structurally cannot become a second
// traversal surface the way re-running `resolve()` on modified input could. This
// test locks that in by construction: the sidecar it finds must live in exactly
// the same directory as the resolved file, for every encoding preference branch.
#[test]
fn sidecar_never_leaves_the_resolved_files_directory() {
    let root = tempfile::TempDir::new().unwrap();
    let sub = root.path().join("assets");
    fs::create_dir(&sub).unwrap();
    let resolved = sub.join("app.js");
    fs::write(&resolved, b"plain").unwrap();
    fs::write(sub.join("app.js.br"), b"brotli-bytes").unwrap();
    fs::write(sub.join("app.js.gz"), b"gzip-bytes").unwrap();

    let (_, _, encoding) = select_precompressed_sidecar(&resolved, Some("br, gzip"))
        .expect("both sidecars present, br should be preferred");
    assert_eq!(
        encoding, "br",
        "br must be preferred over gzip when both are accepted"
    );

    let (_, _, encoding) =
        select_precompressed_sidecar(&resolved, Some("gzip")).expect("gzip sidecar present");
    assert_eq!(encoding, "gzip");

    assert!(
        select_precompressed_sidecar(&resolved, None).is_none(),
        "no Accept-Encoding header should never select a sidecar"
    );
}

#[test]
fn encoding_quality_matches_only_listed_directives() {
    assert_eq!(encoding_quality("identity", "br"), None);
    assert_eq!(encoding_quality("identity", "gzip"), None);
    assert_eq!(encoding_quality("gzip", "br"), None);
    assert_eq!(encoding_quality("gzip, br", "br"), Some(1000));
    assert_eq!(encoding_quality("gzip", "gzip"), Some(1000));
}

/// A coding is a whole token. The substring match this replaces saw `br` inside
/// `brotli` — a coding name that is not `br` — and served brotli bytes to a client that
/// never asked for them.
#[test]
fn a_coding_matches_as_a_token_not_a_substring() {
    assert_eq!(encoding_quality("brotli", "br"), None);
    assert_eq!(encoding_quality("x-gzip", "gzip"), None);
    assert_eq!(encoding_quality("br", "br"), Some(1000));
}

/// `q=0` means "not acceptable" — the case the substring match got backwards, since the
/// header contains the coding name precisely in order to refuse it.
#[test]
fn a_zero_qvalue_refuses_the_coding() {
    assert_eq!(encoding_quality("gzip;q=0", "gzip"), Some(0));
    assert_eq!(encoding_quality("gzip;q=0.0", "gzip"), Some(0));
    assert_eq!(encoding_quality("br;q=0, gzip", "br"), Some(0));
    assert_eq!(encoding_quality("br;q=0, gzip", "gzip"), Some(1000));
}

#[test]
fn qvalues_and_whitespace_and_case_are_parsed() {
    assert_eq!(encoding_quality("GZIP", "gzip"), Some(1000));
    assert_eq!(encoding_quality("  br ;  Q=0.5  ", "br"), Some(500));
    assert_eq!(encoding_quality("br;q=0.333", "br"), Some(333));
    assert_eq!(encoding_quality("br;q=bogus", "br"), Some(1000));
    assert_eq!(encoding_quality("br;q=9", "br"), Some(1000));
}

/// The wildcard is deliberately not honored: matching nothing can only cost a bandwidth
/// optimization, while matching everything risks sending an encoding the client never
/// asked for.
#[test]
fn a_wildcard_never_selects_a_sidecar() {
    assert_eq!(encoding_quality("*", "br"), None);
    assert_eq!(encoding_quality("*;q=1.0", "gzip"), None);
}

/// The client's stated preference wins over this crate's own br-over-gzip default.
#[test]
fn a_higher_qvalue_beats_the_default_preference() {
    let root = tempfile::TempDir::new().unwrap();
    let resolved = root.path().join("app.js");
    fs::write(&resolved, b"plain").unwrap();
    fs::write(root.path().join("app.js.br"), b"brotli-bytes").unwrap();
    fs::write(root.path().join("app.js.gz"), b"gzip-bytes").unwrap();

    let (_, _, encoding) = select_precompressed_sidecar(&resolved, Some("br;q=0.5, gzip"))
        .expect("gzip is acceptable");
    assert_eq!(
        encoding, "gzip",
        "gzip at q=1 must beat br at q=0.5, despite br being preferred at equal weight"
    );

    let (_, _, encoding) =
        select_precompressed_sidecar(&resolved, Some("br;q=0, gzip")).expect("gzip is acceptable");
    assert_eq!(encoding, "gzip", "a refused coding must never be served");
}

/// Refusing the only coding on offer must serve the original file, not the sidecar the
/// client explicitly rejected. This is the case a `q`-aware *parser* still gets wrong if
/// selection forgets to drop zero-weight candidates — parsing `q=0` correctly and then
/// serving it anyway is the same bug one layer down.
#[test]
fn refusing_the_only_available_coding_selects_no_sidecar() {
    let root = tempfile::TempDir::new().unwrap();
    let resolved = root.path().join("only-br.js");
    fs::write(&resolved, b"plain").unwrap();
    fs::write(root.path().join("only-br.js.br"), b"brotli-bytes").unwrap();

    assert!(
        select_precompressed_sidecar(&resolved, Some("br;q=0")).is_none(),
        "a coding refused with q=0 must not be served even when it is the only sidecar"
    );
    assert!(
        select_precompressed_sidecar(&resolved, Some("gzip")).is_none(),
        "no gzip sidecar exists, so nothing should be selected"
    );
}