testing
GitHub规范 Static Web Server 项目的测试标准,涵盖单元测试、集成测试、属性测试、模糊测试及基准测试的编写与审查指南。
触发场景
安装
npx skills add static-web-server/static-web-server --skill testing -g -y
SKILL.md
Frontmatter
{
"name": "testing",
"description": "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."
}
Testing Standards
Every behavior change ships with a test that fails before the change and passes after it.
When to load: adding or editing #[test] / #[tokio::test], creating a file under tests/, adding fixtures under tests/fixtures/, writing a property test, 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-value parsing) |
| Handler integration | tests/<feature>.rs via RequestHandler::handle() |
Behavior that depends on pipeline order or multiple steps |
| Static-file integration | tests/static_files.rs via static_files::handle() |
Path 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 |
Certificate formats in tests/tls/ |
| 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 test files start with the crate lint attributes 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 Helper Behavior (src/testing.rs)
fixture_settings(path)loadstests/fixtures/<path>through the real settings parser (Settings::get_unparsed), so TOML[general]values override CLI defaults exactly as in productionfixture_req_handler_opts(general, advanced)copies mostGeneralfields but hard-codescors: None,page_fallback: vec![], andmemory_cache: None. To test CORS, fallback pages, or the memory cache, set those fields on the returnedRequestHandlerOpts(or call the module'sinit()) before building the handler- It does not run
server/opts.rs, soinit()side effects (compiled CORS config, cache store, loaded 404/50x pages) are absent unless the test performs them - When you add a field to
RequestHandlerOpts, add it tofixture_req_handler_opts()too
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,
#[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,
index_files: &["index.htm"],
follow_symlinks: false,
})
.await;
// Ok(StaticFileResponse { resp, file_path }) or Err(StatusCode)
Fixtures
tests/
fixtures/
public/ default web root: index.htm (+ .br), index.html.gz, 404.html (+ .br), 50x.html,
readme.md, assets/ (main.css + .zst, tiny.js + .br, main.js),
symlink, "spécial-directöry.net/" (non-ASCII and space handling)
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/ TOML configs loaded by fixture_settings("toml/<name>.toml")
tls/ PEM certificates and keys (PKCS#1, PKCS#8, SEC1)
toml/ sws.toml, memory_cache.toml
- Reuse
tests/fixtures/public/when possible. Changing an existing fixture file can break unrelated tests; add a new file instead - Files in
public/under 200 bytes are never compressed dynamically. Usecompression/for dynamic compression tests - Tests that write files use
tempfile(dev-dependency) so cleanup is automatic - Paths are relative to the crate root;
cargo testruns from there
Property Tests and Fuzzing
proptestis a dev-dependency used incors.rs,fs/path.rs,redirects.rs,markdown.rs,response/range.rs,exts/headers/quality_value.rs, anddirectory_listing/download.rs. Add one for any new parser of untrusted input- Shrunk failures persist under
proptest-regressions/. Commit new regression files with the fix - Fuzz targets live in
fuzz/(static_files,content_disposition,cors_origin,redirect_placeholders):cargo +nightly fuzz run <target>from the repo root
Benchmarks
benches/ is a separate crate (Criterion via codspeed-criterion-compat) run on every PR by .github/workflows/codspeed.yml. Add a bench when a change touches a hot-path function. See performance/SKILL.md.
Running Tests
cargo test --features all # everything
cargo test --no-default-features # strict feature build
cargo test --features all --test compression # one integration file
cargo test --features all --test compression -- compression_static_file_exists
cargo test --features all --lib control_headers # unit tests in one module
RUST_LOG=trace cargo test --features all --test static_files -- --nocapture
.cargo/config.toml sets --cfg tokio_unstable. An exported RUSTFLAGS overrides it and breaks --features all; append the flag instead: RUSTFLAGS="$RUSTFLAGS --cfg tokio_unstable".
What to Test
- Always: status codes, response headers (presence and absence), body for small responses,
GETvsHEADvsOPTIONS, disallowed methods (405), and the feature-off path - Security-relevant changes: traversal (
../, encoded%2e%2e, absolute paths), hidden files, symlinks withfollow_symlinkson and off, non-ASCII paths - Config options: default value, CLI flag, env var, and TOML key, including TOML overriding CLI
- Skip: trivial getters, exact log strings, hyper internals
Naming and Style
fn <feature>_<scenario>_<expectation>(), e.g.cache_control_404_uses_no_cache- Arrange → act → assert. One scenario per test; several asserts on the same outcome are fine
- Use
assert_eq!with a message on non-obvious checks:assert_eq!(v, "br", "pre-compressed variant must win") - Loop over methods when behavior must hold for each (
for method in [Method::GET, Method::HEAD])
Checklist
- A test fails without the change and passes with it
- Happy path and at least one error path covered
- Passes under
--features alland--no-default-features - New
RequestHandlerOptsfields are mapped intesting.rs - New parsers of untrusted input have a proptest
- Fixtures are minimal and added rather than modified
版本历史
- a1500d4 当前 2026-10-20 05:09
- 21dc11b 2026-08-20 17:42


