Servirtium for
Crystal
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
- lib/fun block under @[Link] (direct C ABI, no glue C). src/servirtium.cr declares the aether_vcr_embed_* ABI in a lib block and links libservirtium_vcr at compile time with a baked rpath; the Servirtium module and Vcr class add block forms, typed enums and Servirtium::Error.
- Toolchain
- Crystal >= 1.0.0 per shard.yml (verified with Crystal 1.22.3) and shards.
- Distribution
- Shard from the git tag (github: dependency) + release native library
- Finding the library
- Linked at COMPILE time: SERVIRTIUM_VCR_LIB_DIR (read by a macro in servirtium.cr) is the directory holding a file named exactly libservirtium_vcr.so/.dylib, baked into -L and the rpath; otherwise the shard's own crystal/native/ (empty in a git install). Export it for every compile.
- On GitHub
- crystal/ · README
Getting it
There is no published package for Crystal. 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.
- Add the shard from the git tag. The prerelease version must be matched explicitly; if it does not resolve use branch: main.
# shard.yml of your project dependencies: servirtium: github: servirtium/servirtium-vcr version: ~> 2.0.0-alpha.20 - Fetch the native library for your OS/arch, verify its checksum, and save it under the plain name the linker wants. Windows: take the .dll asset (and .dll.lib) by hand.
TAG=v2.0.0-alpha.20 REL=https://github.com/servirtium/servirtium-vcr/releases/download/$TAG case "$(uname -s)" in Linux) OS=linux; EXT=so; ldd --version 2>&1 | grep -qi musl && LIBC=-musl || LIBC= ;; Darwin) OS=macos; EXT=dylib; LIBC= ;; FreeBSD) OS=freebsd; EXT=so; LIBC= ;; esac case "$(uname -m)" in x86_64|amd64) ARCH=x86_64 ;; arm64|aarch64) ARCH=arm64 ;; esac export SERVIRTIUM_VCR_LIB_DIR="$HOME/.servirtium-vcr/$TAG"; mkdir -p "$SERVIRTIUM_VCR_LIB_DIR" LIB=libservirtium_vcr-$TAG-$OS-$ARCH$LIBC.$EXT curl -fsSL -o "$SERVIRTIUM_VCR_LIB_DIR/$LIB" "$REL/$LIB" curl -fsSL -o "$SERVIRTIUM_VCR_LIB_DIR/$LIB.sha256" "$REL/$LIB.sha256" ( cd "$SERVIRTIUM_VCR_LIB_DIR" && { sha256sum -c "$LIB.sha256" || shasum -a 256 -c "$LIB.sha256"; } ) cp "$SERVIRTIUM_VCR_LIB_DIR/$LIB" "$SERVIRTIUM_VCR_LIB_DIR/libservirtium_vcr.$EXT" - macOS only: the release .dylib carries a CI build path as its install name, so a program linked against it fails to start with 'Library not loaded: /home/runner/...'. Rewrite the install name and re-sign first (not run for Crystal on macOS; the same fix was verified for Lua and Zig).
install_name_tool -id @rpath/libservirtium_vcr.dylib "$SERVIRTIUM_VCR_LIB_DIR/libservirtium_vcr.dylib" codesign -f -s - "$SERVIRTIUM_VCR_LIB_DIR/libservirtium_vcr.dylib" - Install the shard and check it links and loads (prints 'servirtium linked and loaded: ...'). The variable is read at compile time; after changing it run rm -rf ~/.cache/crystal.
shards install cat > check.cr <<'EOF' require "servirtium" begin Servirtium.playback("/nonexistent.md") { } rescue ex puts "servirtium linked and loaded: #{ex.class}" end EOF crystal run check.cr
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.
require "http/client"
require "servirtium"
Servirtium.record("tapes/climate_api.md", "https://climatedataapi.worldbank.org") do |vcr|
HTTP::Client.get("#{vcr.base_url}/api/v1/countries")
vcr.flush # writes the tape; a block that raises discards instead
end
Playback
The same tape, replayed with no network.
require "http/client"
require "servirtium"
Servirtium.playback("tapes/climate_api.md") do |vcr|
body = HTTP::Client.get("#{vcr.base_url}/api/v1/countries").body
vcr.last_kind.should eq(Servirtium::Outcome::Ok) # optional: assert a clean match
end # the block form closes the server on every path
Idiomatic extras
The record block discards on error
A block that raises discards its recording; one that ends with neither flush nor discard writes nothing and raises Servirtium::Error.
Servirtium.record(tape, upstream) do |vcr|
HTTP::Client.get("#{vcr.base_url}/ok")
raise "test failed" # recording dropped, no tape written
end
Redact credentials
redact_credentials scrubs the standard credential headers; redact_header, redact_query_param and redact_json_field cover named ones. Call before the SUT sends traffic.
Servirtium.record(tape, upstream) do |vcr|
vcr.redact_credentials
vcr.redact_json_field("secretKey")
# ... drive SUT ...
vcr.flush
end
Volatile values
Rules tokenise values that change on every run; each call raises Servirtium::Error with the core's message.
vcr.volatile_rule("request body", "<a:MessageID>urn:uuid:{{messageId}}</a:MessageID>")
vcr.volatile_markers(Servirtium::VolatileMarkers::DollarCurly) # before any rule
puts vcr.volatile_report
Gotchas
- The version: line is mandatory for a prerelease: omitted, shards resolves '*', which does not match a prerelease and the error does not mention prereleases.
- Pick the asset for the machine that RUNS the program (-musl on Alpine); a wrong-libc or wrong-arch file fails at load with invalid ELF header / wrong ELF class.
- Use the same tag for the shard and the library; an older library fails to link with undefined reference to aether_vcr_embed_...
- Do not vendor the library into git or the shard's checkout directory.
Prompt for your coding agent
Paste this into Claude, Codex, Gemini or any coding agent, in your Crystal 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 Crystal 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 Crystal. Follow https://servirtium.dev/languages/crystal/ 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: Shard from the git tag (github: dependency) + release native library.
Toolchain and minimum version: Crystal >= 1.0.0 per shard.yml (verified with Crystal 1.22.3) and shards..
How the binding finds the native library: Linked at COMPILE time: SERVIRTIUM_VCR_LIB_DIR (read by a macro in servirtium.cr) is the directory holding a file named exactly libservirtium_vcr.so/.dylib, baked into -L and the rpath; otherwise the shard's own crystal/native/ (empty in a git install). Export it for every compile.
3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic Crystal 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 Crystal 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.
- The version: line is mandatory for a prerelease: omitted, shards resolves '*', which does not match a prerelease and the error does not mention prereleases.
- Pick the asset for the machine that RUNS the program (-musl on Alpine); a wrong-libc or wrong-arch file fails at load with invalid ELF header / wrong ELF class.
- Use the same tag for the shard and the library; an older library fails to link with undefined reference to aether_vcr_embed_...
- Do not vendor the library into git or the shard's checkout directory.
References:
- This language's page: https://servirtium.dev/languages/crystal/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/crystal
- 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
Ran on cachyos (Linux x86_64, Crystal 1.22.3): a project with the github: dependency at ~> 2.0.0-alpha.20, shards install (fetched the tag), SERVIRTIUM_VCR_LIB_DIR pointing at the sha256-checked release .so; playback of single_get.md (ok-body Ok), record + vcr.flush + replay (hello-upstream Ok), a raising record block leaves no tape, a record block with no flush raises Servirtium::Error and writes nothing, redact_credentials in the record. Volatile extra not run. macOS not run for Crystal (no toolchain on the Mac).
They mirror these files in the repository:
crystal/spec/playback_spec.crcrystal/example/consumer.crcrystal/tapes/single_get.mdcrystal/.example.ae
See also the feature reference, llms.txt and the crystal/ directory.