← All languages

Servirtium for

Rust

No shipped package: vendor the binding's source at a release tag, with the native library from the release. Release v2.0.0-alpha.20.

FFI mechanism
libloading (dlopen). A Cargo crate that dlopens the native library at run time through libloading. The crate is found by name inside the git repository at the release tag, so no root Cargo.toml is needed.
Toolchain
Stable Rust, edition 2021 (Cargo.toml sets no rust-version; the one dependency is libloading 0.8); run was on Rust 1.100
Distribution
No crates.io package: Cargo git dependency on the release tag, plus the release native library
Finding the library
SERVIRTIUM_VCR_LIB (absolute path), or .native_lib(path) on the builder. Cargo does not pass your shell's variable to another user or a container.
On GitHub
rust/ · README

Getting it

There is no published package for Rust. You vendor the binding's source into your project at the release tag v2.0.0-alpha.20 and take the native library from the same release.

  1. Fetch the native library for your OS/arch from the release into a directory outside your git tree and verify the checksum.
    REL=https://github.com/servirtium/servirtium-vcr/releases/download/v2.0.0-alpha.20
    LIBDIR="$HOME/.servirtium-vcr/v2.0.0-alpha.20"; mkdir -p "$LIBDIR"
    LIB=libservirtium_vcr-v2.0.0-alpha.20-linux-x86_64.so      # or -macos-arm64.dylib, -linux-arm64.so, ...
    curl -fsSL -o "$LIBDIR/$LIB" $REL/$LIB
    curl -fsSL -o "$LIBDIR/$LIB.sha256" $REL/$LIB.sha256
    ( cd "$LIBDIR" && { sha256sum -c $LIB.sha256 || shasum -a 256 -c $LIB.sha256; } )
    
  2. Add the crate as a (dev-)dependency from the tag.
    cargo add --dev servirtium --git https://github.com/servirtium/servirtium-vcr --tag v2.0.0-alpha.20
    
  3. Or in Cargo.toml directly.
    [dev-dependencies]
    servirtium = { git = "https://github.com/servirtium/servirtium-vcr", tag = "v2.0.0-alpha.20" }
    
  4. Point the crate at the library wherever cargo test runs.
    export SERVIRTIUM_VCR_LIB="$HOME/.servirtium-vcr/v2.0.0-alpha.20/libservirtium_vcr-v2.0.0-alpha.20-linux-x86_64.so"
    cargo test
    

Record

Point your client at the VCR's base URL; the VCR forwards to the real service and hands back the real response. Nothing is written until you commit.

use servirtium::Vcr;

#[test]
fn records_climate() {
    let rec = Vcr::record("tapes/climate_api.md", "https://climatedataapi.worldbank.org")
        .port(0)
        .start()
        .unwrap();

    let _ = ureq::get(&format!("{}/api/v1/countries", rec.base_url())).call();

    rec.commit().unwrap();   // consumes rec; the only thing that writes the tape
    // Dropping or close()-ing a record server WITHOUT commit()/discard() writes
    // nothing (close returns an error, drop panics).
}

Playback

The same tape, replayed with no network.

use servirtium::{Outcome, Vcr};

#[test]
fn plays_climate() {
    let vcr = Vcr::playback("tapes/climate_api.md").port(0).start().unwrap();

    let body: String = ureq::get(&format!("{}/api/v1/countries", vcr.base_url()))
        .call().unwrap().into_string().unwrap();

    assert_eq!(Outcome::Ok, vcr.last_kind());   // optional: assert a clean match
    // `vcr` stops on drop.
}

Idiomatic extras

Discard on the failure path

discard() consumes the server and drops the recording without writing a tape.

let rec = Vcr::record(tape, upstream).port(0).start().unwrap();
if drive_the_sut(&rec.base_url()).is_err() {
    rec.discard();            // no tape
    return;
}
rec.commit().unwrap();

Redact credentials and JSON fields at record time

Applied when the tape is written; the live test still sees real bytes. The _with form takes the replacement and the field.

use servirtium::{Field, Vcr};

Vcr::record(tape, upstream)
    .redact_credentials()
    .redact_json_field_with("api_key", "[REDACTED]", Field::ResponseBody)
    .port(0)
    .start()
    .unwrap();

Drift detection

With fail_if_changed, commit() writes the fresh tape and returns Err(VcrError) if it differs from the one on disk.

let rec = Vcr::record(tape, upstream).fail_if_changed().port(0).start().unwrap();
// ... drive the SUT ...
rec.commit()?;   // Err on drift; do not rely on drop, which panics

Gotchas

  • Use tag =, not branch: the library must be at least as new as the Rust source.
  • Do not use servirtium = "2" or any bare version: nothing is on crates.io, and a 1.x servirtium crate with #[servirtium_playback_test] macros is a different, removed API.
  • Do not put the library in ~/.cargo/git/checkouts/...: that is a cache.
  • The crate takes a process-wide lock for each VcrServer's lifetime, so parallel cargo test is safe without --test-threads=1.
  • Tape paths are relative to the test binary's working directory, which cargo test sets to the package root.

Prompt for your coding agent

Paste this into Claude, Codex, Gemini or any coding agent, in your Rust project. It wraps one of your existing integration tests with Servirtium. The general prompt has a slot for the language instead.

Hey Gemini or Claude or Codex,

I want a test in this Rust project to run without calling a real web service every time. Use Servirtium (https://servirtium.dev), which records HTTP conversations as readable Markdown "tapes" and replays them. Do these steps in order and tell me what you did at each one.

1. Find an existing integration test in this project that calls a web service as a side effect of what it is testing. Prefer one that is slow, flaky, or needs credentials or network because of that call. Tell me which test you chose and why.

2. Add the Servirtium binding for Rust. Follow https://servirtium.dev/languages/rust/ exactly. Servirtium ships no package for this language. Vendor the binding's source into the project at the release tag v2.0.0-alpha.20 and put the native library from the same release where the page says: No crates.io package: Cargo git dependency on the release tag, plus the release native library.
   Toolchain and minimum version: Stable Rust, edition 2021 (Cargo.toml sets no rust-version; the one dependency is libloading 0.8); run was on Rust 1.100.
   How the binding finds the native library: SERVIRTIUM_VCR_LIB (absolute path), or .native_lib(path) on the builder. Cargo does not pass your shell's variable to another user or a container.

3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic Rust way of setting a base URL or HTTP client. The VCR starts on its own port (use port 0, so it picks a free one) and gives you its base URL; give that URL to the client and never hard-code a port.

4. Run the test once in record mode against the real service. Commit the recording when the test passes and discard it when the test fails: a record VCR that is closed without a commit or a discard writes no tape and raises an error. Use redact_credentials (the spelling for Rust is on the page) so secrets become numbered tokens, and remove volatile request headers such as User-Agent and Date from the tape so playback does not depend on them.

5. Keep the tape beside the test as readable Markdown, in version control. Open it and check there are no secrets or personal data in it before committing.

6. Make the test play back by default, with an explicit switch (for example an environment variable such as SERVIRTIUM_RECORD=1) that re-records.

7. Prove it works offline: make the real service unreachable (network off, or a wrong host) and show the test still passes in playback.

Traps to avoid:
- A record VCR needs Commit() or Discard(); closing it without either writes nothing and raises.
- The VCR runs in your test process on its own port; the system under test must use the VCR's base URL, not the real one.
- Use tag =, not branch: the library must be at least as new as the Rust source.
- Do not use servirtium = "2" or any bare version: nothing is on crates.io, and a 1.x servirtium crate with #[servirtium_playback_test] macros is a different, removed API.
- Do not put the library in ~/.cargo/git/checkouts/...: that is a cache.
- The crate takes a process-wide lock for each VcrServer's lifetime, so parallel cargo test is safe without --test-threads=1.
- Tape paths are relative to the test binary's working directory, which cargo test sets to the package root.

References:
- This language's page: https://servirtium.dev/languages/rust/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/rust
- Concepts, features and every language's quick start: https://servirtium.dev/llms-full.txt (index: https://servirtium.dev/llms.txt)

How these snippets were checked

Real run on cachyos (Linux x86_64, Rust/cargo 1.100.0), not on the Mac (no Rust toolchain installed there): cargo add --dev servirtium --git ... --tag v2.0.0-alpha.20 in a fresh lib crate, release linux-x86_64 .so (sha256 verified) via SERVIRTIUM_VCR_LIB. The README 'Getting it' test (replays_a_tape) passed. A second integration test recorded against a raw-socket upstream with redact_credentials + redact_json_field_with then commit() (secret and bearer token absent, [REDACTED] present), and record then discard() (no tape file); passed. Not run: macOS arm64, the ureq-based snippets (they mirror docs/usage.md; the verified tests used raw TCP), fail_if_changed.

They mirror these files in the repository:

See also the feature reference, llms.txt and the rust/ directory.