VCR Cassettes are files that contain all of the information about the requests and corresponding responses in a human-readable/editable format. A cassette contains an array of HTTP interactions, each of which has the following:
request
method
uri
body
encoding
string
headers
response
status
code
message
headers
body
encoding
string
http version
By default, VCR uses YAML to serialize this data. You can configure VCR to use a different serializer, either on a cassette-by-cassette basis, or as a default for all cassettes if you use the default_cassette_options.
VCR supports the following serializers out of the box:
:yaml--Uses ruby's standard library YAML. This may use psych or syck, depending on your ruby installation.
:syck--Uses syck (the ruby 1.8 YAML engine). This is useful when using
VCR on a project that must run in environments where psych is not available
(such as on ruby 1.8), to ensure that syck is always used.
:psych--Uses psych (the new ruby 1.9 YAML engine). This is useful when
you want to ensure that psych is always used.
:json--Uses Ruby's standard library to serialize the cassette data as
JSON.
:compressed--Wraps the default YAML serializer with Zlib, writing compressed cassettes to disk.
You can also register a custom serializer using:
VCR.configure do |config| config.cassette_serializers[:my_custom_serializer] = my_serializer end
Your serializer must implement the following methods:
file_extension
serialize(hash)
deserialize(string)
Request/Response data is saved to disk as YAML by default
Given a file named "cassette_yaml.rb" with:
include_http_adapter_for("<http_lib>")
if ARGV.any?
$server = start_sinatra_app do
get('/:path') { ARGV[0] + ' ' + params[:path] }
end
end
require 'vcr'
VCR.configure do |c|
<configuration>
c.cassette_library_dir = 'cassettes'
c.before_record do |i|
i.request.uri.sub!(/:\d+/, ':7777')
end
end
VCR.use_cassette('example') do
make_http_request(:get, "http://localhost:#{$server.port}/foo", nil, 'Accept-Encoding' => 'identity')
make_http_request(:get, "http://localhost:#{$server.port}/bar", nil, 'Accept-Encoding' => 'identity')
end
When I successfully run ruby cassette_yaml.rb 'Hello'
Then the file "cassettes/example.yml" should contain YAML like: