Servirtium for
D
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
- extern (C) declarations, linked at build time. src/servirtium/package.d declares the aether_vcr_embed_* ABI with extern (C) and links libservirtium_vcr; the Vcr struct (copy disabled) owns one handle and a VcrException replaces error strings.
- Toolchain
- dmd (verified with DMD 2.113.0; no minimum stated in the README); dub optional.
- Distribution
- Clone at the tag (dub add-local or plain dmd -I) + release native library
- Finding the library
- Linked at BUILD time: -L<dir> -lservirtium_vcr plus an rpath, from SERVIRTIUM_VCR_LIB_DIR (the root dub.sdl expands $SERVIRTIUM_VCR_LIB_DIR in lflags and cannot default it) or your own dmd flags. The directory must hold a file named exactly libservirtium_vcr.so/.dylib.
- On GitHub
- d/ · README
Getting it
There is no published package for D. 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.
- 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 D 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" - Take the sources of the same tag.
git clone --depth 1 --branch v2.0.0-alpha.20 https://github.com/servirtium/servirtium-vcr.git third_party/servirtium-vcr - Without dub (this is what was run): compile your program with the binding's package.d, the library directory and an rpath.
dmd -Ithird_party/servirtium-vcr/d/src \ -L-L"$SERVIRTIUM_VCR_LIB_DIR" -L-lservirtium_vcr -L-rpath -L"$SERVIRTIUM_VCR_LIB_DIR" \ third_party/servirtium-vcr/d/src/servirtium/package.d app.d -ofapp - With dub (the README's route, NOT run: dub was not available). The root dub.sdl points at d/src; the clone must be the repo root, not d/. SERVIRTIUM_VCR_LIB_DIR must be exported for dub build, run and test.
dub add-local third_party/servirtium-vcr 2.0.0-alpha.20 # dub.sdl of your project: # dependency "servirtium" version="~>2.0.0-alpha.20" dub build
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.
import servirtium;
import std.process : executeShell;
import std.string : strip;
string httpGet(string url) { return executeShell("curl -s -S '" ~ url ~ "'").output.strip; }
void main() {
auto rec = Vcr.record("tapes/climate_api.md", "https://climatedataapi.worldbank.org");
scope (failure) rec.discard(); // a failed test never leaves a half-recorded tape
httpGet(rec.baseUrl ~ "/api/v1/countries");
rec.flush(); // writes the tape; a plain close() would write nothing and throw
}
Playback
The same tape, replayed with no network.
import servirtium;
import std.process : executeShell;
import std.string : strip;
string httpGet(string url) { return executeShell("curl -s -S '" ~ url ~ "'").output.strip; }
void main() {
auto vcr = Vcr.playback("tapes/climate_api.md");
scope (exit) vcr.close();
auto body_ = httpGet(vcr.baseUrl ~ "/api/v1/countries");
assert(vcr.lastKind == Outcome.ok); // optional: assert a clean match
}
Idiomatic extras
Discard on failure with scope(failure)
Vcr is a non-copyable struct; scope(failure) discard drops the recording when anything throws, scope(success) or an explicit flush commits it.
auto rec = Vcr.record(tape, upstream);
scope (failure) rec.discard();
// ... drive the SUT, assert on it ...
rec.flush();
Redact credentials
redactCredentials scrubs the standard credential headers; redactHeader, redactQueryParam and redactJsonField cover named ones.
auto rec = Vcr.record(tape, upstream);
rec.redactCredentials();
rec.redactJsonField("secretKey");
Volatile values
The method is volatileRule because volatile is a D keyword; each call throws VcrException with the core's message.
vcr.volatileRule("request body", "<a:MessageID>urn:uuid:{{messageId}}</a:MessageID>");
vcr.volatileMarkers(VolatileMarkers.dollarCurly); // before any rule
string report = vcr.volatileReport();
Gotchas
- The library is linked, not dlopen-ed: use the same tag for source and library.
- Vcr is a struct with copying disabled: one owner per handle.
- Flush variants: flush, flushFailIfChanged (drift), flushOrCheck (check against an existing tape); all throw VcrException on error.
- The d README states it was not verified with dub itself; neither was it here.
Prompt for your coding agent
Paste this into Claude, Codex, Gemini or any coding agent, in your D 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 D 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 D. Follow https://servirtium.dev/languages/d/ 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: Clone at the tag (dub add-local or plain dmd -I) + release native library.
Toolchain and minimum version: dmd (verified with DMD 2.113.0; no minimum stated in the README); dub optional..
How the binding finds the native library: Linked at BUILD time: -L<dir> -lservirtium_vcr plus an rpath, from SERVIRTIUM_VCR_LIB_DIR (the root dub.sdl expands $SERVIRTIUM_VCR_LIB_DIR in lflags and cannot default it) or your own dmd flags. The directory must hold a file named exactly libservirtium_vcr.so/.dylib.
3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic D 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 D 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 library is linked, not dlopen-ed: use the same tag for source and library.
- Vcr is a struct with copying disabled: one owner per handle.
- Flush variants: flush, flushFailIfChanged (drift), flushOrCheck (check against an existing tape); all throw VcrException on error.
- The d README states it was not verified with dub itself; neither was it here.
References:
- This language's page: https://servirtium.dev/languages/d/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/d
- 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, DMD 2.113.0, no dub installed) with the sha256-checked release .so and a clone of the tag, compiled with the dmd command above: playback of single_get.md (ok-body, ok), record + flush() + replay (hello-upstream, ok), close() on an unflushed recording throws VcrException and writes nothing, redactCredentials in the record. scope(failure) discard, the dub route and the volatile extra were not run. macOS not run for D.
They mirror these files in the repository:
See also the feature reference, llms.txt and the d/ directory.