Cassette Format

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:

When I successfully run ruby cassette_yaml.rb 'Hello'

Then the file "cassettes/example.yml" should contain YAML like:

Examples

configuration

http_lib

c.hook_into :webmock

net/http

c.hook_into :webmock

httpclient

c.hook_into :webmock

patron

c.hook_into :webmock

curb

c.hook_into :webmock

em-http-request

c.hook_into :webmock

typhoeus

c.hook_into :typhoeus

typhoeus

c.hook_into :excon

excon

c.hook_into :faraday

faraday (w/ net_http)

Request/Response data can be saved as JSON

Given a file named "cassette_json.rb" with:

When I run ruby cassette_json.rb 'Hello'

Then the file "cassettes/example.json" should contain JSON like:

When I run ruby cassette_json.rb

Then it should pass with:

Request/Response data can be saved as compressed YAML

Given a file named "cassette_compressed.rb" with:

When I run ruby cassette_compressed.rb 'Hello'

Then the file "cassettes/example.zz" should contain compressed YAML like:

When I run ruby cassette_compressed.rb

Then it should pass with:

Request/Response data can be saved using a custom serializer

Given a file named "cassette_ruby.rb" with:

When I run ruby cassette_ruby.rb 'Hello'

Then the file "cassettes/example.ruby" should contain ruby like:

When I run ruby cassette_ruby.rb

Then it should pass with:

Last updated

Was this helpful?