Testing

From static-web-server/static-web-server · View interactive version

Testing is an AI agent skill from the GitHub repository static-web-server/static-web-server. Write or review tests for the Static Web Server (SWS) project — unit tests, handler and static-file integration tests, TOML and file fixtures, property tests, fuzz targets, and benchmarks. Use when adding or editing any test, fixture, proptest, fuzz target, or bench, or when a test fails.

How to use this skill

Skill file

Testing Standards

Every behavior change ships with a test that fails before the change and passes after it. Prove it: run the test on the base before the fix.

When to load: adding or editing #[test] / #[tokio::test], creating a file under tests/, adding fixtures, writing a proptest, fuzz target, or bench, or diagnosing a failing test.

Where Tests Go

| Kind | Location | Use for |

|------|----------|---------|

| Unit | #[cfg(test)] mod tests at the bottom of the source file | Pure functions and private logic (control_headers::append_headers, compression::auto, q-values) |

| Handler integration | tests/<feature>.rs via RequestHandler::handle() | Behavior that depends on pipeline order or several steps. Security fixes always get one |

| Static-file integration | tests/static_files.rs via static_files::handle() | Resolution, index files, ranges, pre-compressed variants, without the pipeline |

| Settings | tests/settings.rs | CLI/env/TOML parsing and merging |

| TLS / server | tests/tls.rs, tests/idle_timeout.rs, tests/max_header_size.rs, tests/max_connections.rs | Certificates in tests/tls/; connection limits |

| Property | proptest! blocks in source files | Parsers and path handling over arbitrary input |

| Fuzz | fuzz/src/*.rs (cargo-fuzz) | Untrusted-input parsers |

| Bench | benches/*.rs (Criterion/CodSpeed) | Hot-path functions |

Integration files start with the crate lints and wrap tests in a module:

#![forbid(unsafe_code)]
#![deny(warnings)]
#![deny(rust_2018_idioms)]
#![deny(dead_code)]

#[cfg(test)]
mod tests { /* ... */ }

Gate tests on the features they need (#[cfg(feature = "directory-listing")]) and gate their imports too, or cargo test --no-default-features fails on unused imports.

Handler Test Pattern

use hyper::{Request, header::ACCEPT_ENCODING};
use std::net::SocketAddr;
use static_web_server::settings::cli::General;
use static_web_server::testing::fixtures::{
    REMOTE_ADDR, fixture_req_handler, fixture_req_handler_opts, fixture_settings,
};

#[tokio::test]
async fn compression_static_serves_brotli_variant() {
    let opts = fixture_settings("toml/handler_fixtures.toml");
    let general = General { compression_static: true, ..opts.general };
    let req_handler = fixture_req_handler(fixture_req_handler_opts(general, opts.advanced));
    let remote_addr = Some(REMOTE_ADDR.parse::<SocketAddr>().unwrap());

    let mut req = Request::new(());
    *req.uri_mut() = "http://localhost/index.htm".parse().unwrap();
    req.headers_mut().insert(ACCEPT_ENCODING, "gzip, deflate, br".parse().unwrap());

    let res = req_handler.handle(&mut req, remote_addr).await.unwrap();
    assert_eq!(res.status(), 200);
    assert_eq!(res.headers()["content-encoding"], "br");
}

Fixture Helpers (`src/testing.rs`)

Static File Test Pattern

let result = static_files::handle(&HandleOpts {
    method: &Method::GET,
    headers: &HeaderMap::new(),
    base_path: &PathBuf::from("tests/fixtures/public/"),
    uri_path: "index.htm",
    uri_query: None,
    rewritten_from: None,
    index_files: &["index.htm"],
    #[cfg(feature = "mem-cache")]
    memory_cache: None,
    #[cfg(feature = "directory-listing")]
    dir_listing: false,
    #[cfg(feature = "directory-listing")]
    dir_listing_order: 6,
    #[cfg(feature = "directory-listing")]
    dir_listing_format: &DirListFmt::Html,
    #[cfg(feature = "directory-listing-download")]
    dir_listing_download: &[],
    redirect_trailing_slash: true,
    compression_static: false,
    etag: false,
    include_hidden: false,
    follow_symlinks: false,
})
.await;
// Ok(StaticFileResponse { resp, file_path, is_dir_listing }) or Err(StatusCode)

Fixtures

tests/fixtures/
  public/        default root: index.htm (+ .br), index.html.gz, 404.html (+ .br), 50x.html, readme.md,
                 assets/ (index.html + .gz, main.css + .zst, main.js, tiny.js + .br),
                 symlink, "spécial-directöry.net/" (non-ASCII, spaces)
  compression/   large-test.html, above the 200-byte dynamic compression threshold
  markdown/      .md variants for --accept-markdown
  symlink/       targets for symlink policy tests
  empty/         empty root
  toml/          configs for fixture_settings("toml/<name>.toml")
tests/tls/       PEM certificates and keys (PKCS#1, PKCS#8, SEC1)
tests/toml/      sws.toml, memory_cache.toml

Property Tests and Fuzzing

Benchmarks

benches/ is a separate crate (codspeed-criterion-compat) run by .github/workflows/codspeed.yml. Add a bench when a change touches a hot-path function (performance/SKILL.md).

Running Tests

cargo test --features all
cargo test --no-default-features
cargo test --features all --test compression                   # one integration file
cargo test --features all --test compression -- <test_name>
cargo test --features all --lib control_headers                # unit tests of one module
RUST_LOG=trace cargo test --features all --test static_files -- --nocapture

Tooling pitfalls (RUSTFLAGS, --all-features): rust-conventions/SKILL.md.

What to Test

Naming and Style

Checklist

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.