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

Was this helpful?

  1. Request matching

Playback Repeats

By default, each response in a cassette can only be matched and played back once while the cassette is in use (it can, of course, be re-used in multiple tests, each of which should use the cassette separately). Note that this is a change from the behavior in VCR 1.x. The old behavior occurred because of how WebMock behave internally and was not intended. Repeats create less accurate tests since the real HTTP server may not necessarily return the same response when identical requests are made in sequence.

If you want to allow playback repeats, VCR has a cassette option for this:

  :allow_playback_repeats => true

Responses do not repeat by default

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

--- 
http_interactions: 
- request: 
    method: get
    uri: http://example.com/foo
    body: 
      encoding: UTF-8
      string: ""
    headers: {}
  response: 
    status: 
      code: 200
      message: OK
    headers: 
      Content-Length: 
      - "10"
    body: 
      encoding: UTF-8
      string: Response 1
    http_version: "1.1"
  recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
- request: 
    method: get
    uri: http://example.com/foo
    body: 
      encoding: UTF-8
      string: ""
    headers: {}
  response: 
    status: 
      code: 200
      message: OK
    headers: 
      Content-Length: 
      - "10"
    body: 
      encoding: UTF-8
      string: Response 2
    http_version: "1.1"
  recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0

And a file named "playback_repeats.rb" with:

include_http_adapter_for("net/http")

require 'vcr'

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

puts "== With :allow_playback_repeats =="
VCR.use_cassette('example', :allow_playback_repeats => true) do
  puts response_body_for(:get, 'http://example.com/foo')
  puts response_body_for(:get, 'http://example.com/foo')
  puts response_body_for(:get, 'http://example.com/foo')
end

puts "\n== Without :allow_playback_repeats =="
VCR.use_cassette('example') do
  puts response_body_for(:get, 'http://example.com/foo')
  puts response_body_for(:get, 'http://example.com/foo')
  puts response_body_for(:get, 'http://example.com/foo')
end

When I run ruby playback_repeats.rb

Then it should fail with "An HTTP request has been made that VCR does not know how to handle"

And the output should contain:

== With :allow_playback_repeats ==
Response 1
Response 2
Response 2

== Without :allow_playback_repeats ==
Response 1
Response 2
PreviousURI Without Param(s)NextMatching on Body as JSON

Last updated 4 years ago

Was this helpful?