VCR
v-6.0.0
v-6.0.0
  • Introduction
  • Upgrade
  • Changelog
  • About These Examples
  • License
  • Contributing
  • Cassettes
    • Cassette Format
    • Naming
    • Error for HTTP Request Made When No Cassette Is in Use
    • Dynamic ERB Cassettes
    • Automatic Re-Recording
    • Exclusive Cassette
    • Update Content Length Header
    • Decode Compressed Response
    • Allow Unused HTTP Interactions
    • Freezing Time
  • Record modes
    • Once
    • New Episodes
    • None
    • All
    • Record on Error
  • Configuration
    • Cassette Library Dir
    • Hook Into
    • Default Cassette Options
    • Ignore Request
    • Filter Sensitive Data
    • Allow HTTP Connections When No Cassette
    • Debug Logging
    • Preserve Exact Body Bytes
    • URI Parser
    • Query Parser
  • Hooks
    • Before Record Hook
    • Before Playback Hook
    • Before HTTP Request Hook
    • After HTTP Request Hook
    • Around HTTP Request Hook
  • Request matching
    • Introduction
    • Matching on Method
    • Matching on URI
    • Matching on Host
    • Matching on Path
    • Matching on Query String
    • Matching on Body
    • Matching on Headers
    • Identical Requests Are Replayed in Sequence
    • Register and Use a Custom Matcher
    • URI Without Param(s)
    • Playback Repeats
    • Matching on Body as JSON
  • Test frameworks
    • Usage With Test::Unit
    • Usage With RSpec Metadata
    • Usage With Cucumber
  • Middleware
    • Rack
    • Faraday Middleware
  • HTTP Libraries
    • Net::HTTP
    • EM HTTP Request
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
PreviousUpdate Content Length HeaderNextAllow Unused HTTP Interactions

Last updated 4 years ago

Was this helpful?