VCR
1.0.0
1.0.0
  • Initial page
  • record_modes
    • :none
    • :new_episodes
    • :all
    • :once
  • test_frameworks
    • Usage with Test::Unit
    • Usage with Cucumber
    • Usage with RSpec metadata
  • request_matching
    • Matching on Headers
    • Matching on Body
    • Matching on Method
    • URI without param(s)
    • Identical requests are replayed in sequence
    • Matching on Body
    • Playback repeats
    • Matching on URI
    • Matching on Host
    • Matching on Path
    • Matching on Query string
    • Register and use a custom matcher
  • cassettes
    • exclusive cassette
    • Update content_length header
    • Error for HTTP request made when no cassette is in use
    • Naming
    • Allow Unused HTTP Interactions
    • Freezing Time
    • Decode compressed response
    • Dynamic ERB cassettes
    • Automatic Re-recording
    • Cassette format
  • configuration
    • hook_into
    • cassette_library_dir
    • Filter sensitive data
    • Ignore Request
    • Debug Logging
    • default_cassette_options
    • query_parser
    • uri_parser
    • Allow HTTP connections when no cassette
    • Preserve Exact Body Bytes
  • http_libraries
    • EM HTTP Request
    • Net::HTTP
  • hooks
    • before_playback hook
    • after_http_request hook
    • before_http_request hook
    • before_record hook
    • around_http_request hook
  • middleware
    • Faraday middleware
    • Rack
Powered by GitBook
On this page
  • Background ()
  • The option is not set by default
  • The option is enabled

Was this helpful?

  1. cassettes

Decode compressed response

When the :decode_compressed_response option is set to a truthy value, VCR will decompress "gzip" and "deflate" response bodies before recording. This ensures that these interactions become readable and editable after being serialized.

This option should be avoided if the actual decompression of response bodies is part of the functionality of the library or app being tested.

Background ()

Given a file named "decompress.rb" with:

require 'zlib'
require 'stringio'

$server = start_sinatra_app do
  get('/') {
    content = 'The quick brown fox jumps over the lazy dog'
    io = StringIO.new

    writer = Zlib::GzipWriter.new(io)
    writer << content
    writer.close

    headers['Content-Encoding'] = 'gzip'
    io.string
  }
end

require 'vcr'

VCR.configure do |c|
  c.cassette_library_dir = 'cassettes'
  c.hook_into :webmock
  c.default_cassette_options = { :serialize_with => :syck }
end

The option is not set by default

When I append to file "decompress.rb":

VCR.use_cassette(:decompress) do
  Net::HTTP.start('localhost', $server.port) do |http|
    http.get('/', 'accept-encoding' => 'identity')
  end
end

And I run ruby decompress.rb

Then the file "cassettes/decompress.yml" should contain a YAML fragment like:

Content-Encoding:
- gzip

The option is enabled

When I append to file "decompress.rb":

VCR.use_cassette(:decompress, :decode_compressed_response => true) do
  Net::HTTP.start('localhost', $server.port) do |http|
    http.get('/', 'accept-encoding' => 'identity')
  end
end

And I run ruby decompress.rb

Then the file "cassettes/decompress.yml" should contain a YAML fragment like:

Content-Length:
- '43'

And the file "cassettes/decompress.yml" should contain:

string: The quick brown fox jumps over the lazy dog
PreviousFreezing TimeNextDynamic ERB cassettes

Last updated 5 years ago

Was this helpful?