Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Testing your application

Three applications built on Rullst wrote their own testing layers by hand before 13.0.0-alpha.5. Between them they needed the same list, and their browser and end-to-end layers caught defects that Rust unit tests did not:

  • a static html! attribute containing \", which ended the attribute early and broke the rest of the tag;
  • name={option} writing disabled="" for None, which disabled a button;
  • content escaped twice, so the page showed <span as text;
  • an inline style attribute that the strict Content Security Policy blocked;
  • a bare fragment served when htmx restored a page from its history.

Since 13.0.0-alpha.5, cargo rullst new generates the first layers of that list for every starter (Blank, API and SaaS), and cargo rullst add browser-tests adds the browser layer. The rest stays the application’s job; this page says which is which.

LayerGeneratedWhere
Unit tests of business rulesNo: they are yours#[cfg(test)] modules, tests/
Clippy with -D warnings, formatting, tests on every push and pull requestYes.github/workflows/ci.yml
Security headers, WAF and CSRF behind the production baselineYessrc/security_tests.rs
Every page: status, security headers, HTML hygieneYesPAGES in src/security_tests.rs
Daily cargo audit and a secret scan (TruffleHog)Yes.github/workflows/ci.yml
Monthly grouped dependency updatesYes.github/dependabot.yml
Every page in Chromium, Firefox and WebKit with axe-core, both colour schemesWith cargo rullst add browser-teststests/browser/ and the browser-tests.yml workflow
Routes with {id} classified with // rullst-access:Checked by cargo rullst audit --idoryour CI, see below
Binary size, memory, CPU and page-weight limitsChecked by cargo rullst footprint --check-budgetyour CI, see Budgets in CI
A route × role permission tableNosee below
Restore a backup, test migrations on a copy, monitor the site, OWASP ZAP, published-figure freshnessNosee below

What cargo rullst new generates

src/security_tests.rs

The starter’s router is tested offline (no database, no network) wrapped in the baseline that Server adds in staging and production: secure headers, WAF and CSRF. Run it with cargo test. Since 13.0.0-alpha.5 it also has a smoke test of every page:

/// Every page a signed-out visitor can open without parameters, with the
/// status it answers. ...
const PAGES: &[(&str, u16)] = &[
    ("/", 200),
    ("/pricing", 200),
    ("/login", 200),
    ("/dashboard", 303),
    ("/health", 200),
];

For each entry, every_page_answers_with_safe_headers_and_sound_html sends a GET with TestApp and checks:

  • the status (pages behind the sign-in answer 303 to /login, so the list also proves they stay protected);
  • TestResponse::assert_security_headers: a Content-Security-Policy, X-Content-Type-Options: nosniff and frame protection (X-Frame-Options DENY/SAMEORIGIN or a CSP frame-ancestors directive);
  • TestResponse::assert_html_hygiene on HTML responses (JSON passes unchecked), which reports:
    • an escaped element tag or comment in text (<span class, </div>) or an entity escaped again (<) anywhere;
    • a broken attribute: a quote in a name, a quoted value followed directly by more characters (the \" defect above), a quote, <, = or backtick in an unquoted value, name= without a value, or a tag, comment or <script>/<style> that never ends;
    • a boolean attribute set to "false" (disabled="false" disables: write disabled?={flag});
    • an inline <script> (without src) or <style> without the response’s CSP nonce;
    • a style or on* attribute the response’s policy blocks (nonces do not apply to attributes);
  • for a page that answers a whole document, the same GET as htmx sends it after a history-cache miss (HX-Request and HX-History-Restore-Request) must answer the whole document too, not a fragment.

Add each new page to PAGES. make:mfa (/account/security, 303), make:privacy (/privacy, 303) and make:pwa (/offline, 200) add theirs (since 13.0.0-alpha.5). Use the path a signed-out visitor opens and the status they receive; pages that need a signed-in user can be checked in your own tests by sending the session cookie with TestApp::get(path) .header("cookie", …). Routes with parameters (/posts/{id}) are not listed: test them with fixtures in your own tests.

The checks are a tokenizer, not an HTML parser or a browser. Text inside code, pre, kbd, samp and textarea may show escaped markup; anywhere else a page that shows an HTML tag on purpose (<p>Use &lt;b&gt; for bold</p>) is reported, so keep such pages out of PAGES or check them yourself. The same functions are available to any test:

#![allow(unused)]
fn main() {
use rullst::testing::{HtmlIssueKind, html_issues};

let csp = "default-src 'self'; script-src 'self' 'nonce-abc'";
let issues = html_issues("<p>&lt;span&gt;</p><script>run()</script>", Some(csp));
assert_eq!(issues[0].kind(), HtmlIssueKind::DoubleEscaped);
assert_eq!(issues[1].kind(), HtmlIssueKind::MissingNonce);
}

rullst::testing::assert_html_snapshot! compares a page with a reviewed snapshot when you want to pin its whole markup.

.github/workflows/ci.yml and .github/dependabot.yml

new writes them unless you pass --no-ci. They belong to the application: edit them freely.

  • Formatting: cargo fmt --all -- --check. A new project is not rustfmt-formatted yet, so run cargo fmt --all once and commit before the first push.

  • Clippy and tests: cargo clippy --all-targets -- -D warnings and cargo test --all-targets, with a dependency cache. Cargo warnings fail this job too: setup-rust-toolchain sets CARGO_BUILD_WARNINGS=deny by default, and the workflow keeps it. Since Cargo 1.100 a dependency in [dependencies] that no target uses is such a warning (cargo::unused_dependencies), so a crate you added and no longer use fails CI. Remove it from Cargo.toml, or, when it is there on purpose (for example only to turn on a feature of a crate another dependency brings in), allow the lint for the package:

    [lints.cargo]
    unused_dependencies = "allow"
    

    Cargo before 1.100 prints a warning that the [lints.cargo] table needs -Zcargo-lints, and builds.

  • Dependency advisories: cargo audit on every push and pull request and daily at 06:17 UTC, so a new RustSec advisory for a locked crate fails the scheduled run. It reads Cargo.lock: commit it. It ignores one advisory that Rullst governs and cannot fix yet, RUSTSEC-2026-0459 (bundled SQLite through sqlx-sqlite), until its expiry, 2027-01-28, after which the step audits without it; the comment above the step says when to remove it (see security advisory exceptions).

  • Secret scan: TruffleHog, verified findings only, over the commits of the push or pull request.

Every action is pinned to a full commit SHA with its version in a comment, the same pins Rullst’s own workflows use, and the token is read-only (permissions: contents: read). Dependabot opens one grouped pull request a month for Cargo and one for GitHub Actions. Enable Dependabot security updates in the repository settings to receive advisory fixes at once.

The workflow sits at the root of the new project. When the project lives in a subdirectory of a larger repository, move the file to that repository’s .github/workflows/ and set working-directory.

The workflow does not measure resources. To keep the binary size, idle memory, CPU per request and page weight under limits, set them in Rullst.toml [footprint.budgets] and add the job that Budgets in CI shows: it installs the CLI, starts the application with --env staging and fails when a limit is exceeded.

Browser tests

cargo rullst add browser-tests

writes:

FilePurpose
tests/browser/package.json, package-lock.jsonPlaywright 1.64.1 and axe-core 4.14.0, pinned with sha512 integrity
tests/browser/pages.mjsOpens each HTML page of PAGES in Chromium, Firefox and WebKit
tests/browser/.gitignoreKeeps node_modules/ and results/ out of Git
browser-tests.yml (in .github/workflows/)Builds and starts the application, then runs pages.mjs

pages.mjs reads the PAGES list from src/security_tests.rs, so one list drives both layers. It fails on a console error, an uncaught page error, a failed same-origin request, a Content-Security-Policy violation (the application’s own policy applies) or an axe-core violation of serious or critical impact; moderate and minor findings are printed as notes. When a stylesheet has a prefers-color-scheme: dark rule, axe-core runs again in the dark scheme. Pages that redirect (303) are checked where they land.

Locally:

npm ci --prefix tests/browser
node tests/browser/node_modules/playwright/cli.js install --with-deps chromium firefox webkit
cargo run                      # in another terminal
node tests/browser/pages.mjs   # --base-url, --browsers chromium,firefox,webkit

The workflow copies .env.example to .env, sets a random APP_KEY, runs db:migrate when the project has src/migrations/, starts the application on port 3000 and waits for /health. Running the command again changes nothing; a file you changed is kept. A project generated before 13.0.0-alpha.5 has no PAGES list: the script then visits / only until you add one.

Access classification of {id} routes

cargo rullst audit --idor lists every route with a path parameter that has no adjacent // rullst-access: public|owner|role|admin — reason comment, or whose classification has no matching guard in the crate, and exits non-zero. The guard is looked up once per crate: a single authorize_owner_or_role call anywhere satisfies every owner route, so a handler that forgot its own check is not reported. Add it to your CI once the CLI is installed there (cargo install cargo-rullst --version <the version you generated with> --locked). The audit points at the routes; a person still has to read each handler and decide whether it really checks ownership. Rullst does not generate this check yet: the audit reports findings as text and its JSON summary carries counts, not a machine-readable route list a generated test could read.

What stays the application’s job

  • Business rules: unit tests next to the code, and integration tests that boot the application with a real database and walk the main screens (#[rullst_orm::test] runs a test inside an ORM transaction that is always rolled back, on DATABASE_URL or an in-memory SQLite database).
  • Roles: with several user roles, a table of route × role and the status each role must receive, as a test that signs in as each role.
  • Production: restore a backup now and then, run migrations on a copy of production data before releasing them, and monitor the site (see the production monitoring endpoint).
  • Scans: a weekly OWASP ZAP baseline scan of a deployed or locally started copy; Rullst’s own .github/workflows/dast-zap.yml is a model.
  • Published figures: when you publish numbers such as page weight or response times, re-measure them in CI and fail when the published figure is stale.
  • Hygiene: coverage and badges, actionlint for the workflows and cargo machete for unused dependencies.