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 ()
  • Default :update_content_length_header setting
  • :update_content_length_header => false
  • :update_content_length_header => true

Was this helpful?

  1. cassettes

Update content_length header

When the :update_content_length_header option is set to a truthy value, VCR will ensure that the Content-Length header will have the correct value. This is useful in several situations:

  • When you manually edit the cassette file and change the resonse body

    length. You can use this option so you don't have to manually calculate

    and update the body length.

  • When you use ERB, the response body length may vary. This will ensure

    it is always correct.

  • Syck, the default YAML engine for ruby 1.8 (and 1.9, unless you compile it to use Psych), has a bug where it sometimes will remove some whitespace strings when you serialize them. This may cause the Content-Length header to have the wrong value.

    This is especially important when you use a client that checks the Content-Length header. Mechanize, for example, will raise an EOFError when the header value does not match the actual body length.

Background ()

Given a previously recorded cassette file "cassettes/example.yml" with:

--- 
http_interactions: 
- request: 
    method: get
    uri: http://example.com/
    body: 
      encoding: UTF-8
      string: ""
    headers: {}
  response: 
    status: 
      code: 200
      message: OK
    headers: 
      Content-Type: 
      - text/html;charset=utf-8
      Content-Length: 
      - "11"
    body: 
      encoding: UTF-8
      string: Hello <modified>
    http_version: "1.1"
  recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0

And a file named "common_stuff.rb" with:

require 'vcr'

VCR.configure do |c|
  c.cassette_library_dir = 'cassettes'
  c.hook_into :webmock
end

def make_request_and_print_results
  response = Net::HTTP.get_response('example.com', '/')
  puts "Body length: #{response.body.length}"
  puts "Header value: #{response['Content-Length']}"
end

Default :update_content_length_header setting

Given a file named "default.rb" with:

require 'common_stuff'

VCR.use_cassette('example') do
  make_request_and_print_results
end

When I run ruby default.rb

Then the output should contain:

Body length: 16
Header value: 11

:update_content_length_header => false

Given a file named "false.rb" with:

require 'common_stuff'

VCR.use_cassette('example', :update_content_length_header => false) do
  make_request_and_print_results
end

When I run ruby false.rb

Then the output should contain:

Body length: 16
Header value: 11

:update_content_length_header => true

Given a file named "true.rb" with:

require 'common_stuff'

VCR.use_cassette('example', :update_content_length_header => true) do
  make_request_and_print_results
end

When I run ruby true.rb

Then the output should contain:

Body length: 16
Header value: 16
Previousexclusive cassetteNextError for HTTP request made when no cassette is in use

Last updated 5 years ago

Was this helpful?