HTTP Client VCR
A Rust library for recording and replaying HTTP requests, inspired by VCR libraries in other languages. This library works with the http-client crate to provide a simple way to test HTTP interactions.
Features
- Record HTTP interactions to YAML cassettes
- Replay recorded interactions for deterministic tests
- Multiple recording modes (Record, Replay, Once, None)
- Flexible request matching (URL, method, headers, body)
- Builder pattern for easy configuration
- Thread-safe with async support
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
Basic Example
use ;
use Request;
use ;
async
VCR Modes
VcrMode::Record: Always make real HTTP requests and record them. If an interaction already exists in the cassette, replay it instead.VcrMode::Replay: Only replay interactions from the cassette. Fail if no matching interaction is found.VcrMode::Once: Record interactions only if the cassette is empty, otherwise replay existing interactions.VcrMode::None: Pass through to the inner HTTP client without any recording or replaying.
Request Matching
By default, requests are matched by HTTP method and URL. You can customize matching behavior:
use ;
// Custom matching (method, URL, specific headers)
let matcher = new
.with_method
.with_url
.with_headers;
let vcr_client = builder
.inner_client
.cassette_path
.matcher
.build
.await?;
Filtering Sensitive Data
VCR supports filtering sensitive data from requests and responses before they are stored in cassettes:
Built-in Filters
use ;
// Remove sensitive headers
let header_filter = new
.remove_auth_headers // Removes Authorization, Cookie, X-API-Key, etc.
.remove_header
.replace_header;
// Filter JSON body content
let body_filter = new
.remove_common_sensitive_keys // Removes password, token, api_key, etc.
.remove_json_key
.replace_regex
.unwrap;
// Filter URL query parameters
let url_filter = new
.remove_common_sensitive_params // Removes api_key, token, etc.
.remove_query_param
.replace_query_param;
// Chain filters together
let filter_chain = new
.add_filter
.add_filter
.add_filter;
let vcr_client = builder
.inner_client
.cassette_path
.filter_chain
.build
.await?;
Custom Filters
You can create custom filters for more complex scenarios:
use CustomFilter;
let custom_filter = new;
let vcr_client = builder
.inner_client
.add_filter
.build
.await?;
Important: Filters are applied only to the data stored in cassette files, not to the actual HTTP interactions. During recording:
- Real requests are made with original sensitive data (so APIs work properly)
- Real responses are returned to your application (unfiltered)
- Filtered copies are stored in the cassette (removing sensitive data)
This ensures your code gets the real data it needs while keeping cassettes safe for version control.
NoOp Client for Testing
For ultimate safety during testing, VCR provides a NoOpClient that ensures no real HTTP requests are ever made:
use ;
// Guarantee no real HTTP requests can be made
let vcr_client = builder
.inner_client
.cassette_path
.mode // Only replay from cassette
.build
.await?;
// This works if the request exists in the cassette
let response = vcr_client.send.await?;
// If not in cassette, you get a clear error (not a real HTTP request)
Two variants are available:
NoOpClient::new()- Returns an error if a request is attemptedNoOpClient::panicking()- Panics with a stack trace (useful for development)
This is particularly useful in CI/CD environments or when you want to be absolutely certain your tests are deterministic.
Cassette Format
Cassettes are stored as YAML files with the following structure:
interactions:
- request:
method: GET
url: https://httpbin.org/get
headers:
User-Agent:
body: null
version: Http1_1
response:
status: 200
headers:
Content-Type:
body: '{"origin": "127.0.0.1"}'
version: Http1_1
Testing with VCR
VCR is particularly useful for testing:
async
License
MIT