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 ()
  • Modify played back response
  • Modify played back response based on the cassette
  • Prevent playback by ignoring interaction in before_playback hook
  • Multiple hooks are run in order
  • Use tagging to apply hooks to only certain cassettes

Was this helpful?

  1. Hooks

Before Playback Hook

The before_playback hook is called before a cassette sets up its stubs for playback.

Your block should accept up to 2 arguments. The first argument will be the HTTP interaction that is about to be used for play back. The second argument will be the current cassette.

You can also call #ignore! on the HTTP interaction to prevent VCR from playing it back.

You can use tags to specify a cassette, otherwise your hook will apply to all cassettes. Consider this code:

  VCR.configure do |c|
    c.before_playback(:twitter) { ... } # modify the interactions somehow
  end

  VCR.use_cassette('cassette_1', :tag => :twitter) { ... }
  VCR.use_cassette('cassette_2') { ... }

In this example, the hook would apply to the first cassette but not the second cassette.

Background ()

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

---
http_interactions:
- request:
    method: get
    uri: http://localhost:7777/
    body:
      encoding: UTF-8
      string: ""
    headers: {}
  response:
    status:
      code: 200
      message: OK
    headers:
      Content-Type:
      - text/html;charset=utf-8
      Content-Length:
      - "20"
    body:
      encoding: UTF-8
      string: previously recorded response
    http_version: "1.1"
  recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0

Modify played back response

Given a file named "before_playback_example.rb" with:

require 'vcr'

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

  c.before_playback do |interaction|
    interaction.response.body = 'response from before_playback'
  end
end

VCR.use_cassette('example') do
  response = Net::HTTP.get_response('localhost', '/', 7777)
  puts "Response: #{response.body}"
end

When I run ruby before_playback_example.rb

Then it should pass with "Response: response from before_playback".

Modify played back response based on the cassette

Given a file named "before_playback_example.rb" with:

require 'vcr'

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

  c.before_playback do |interaction, cassette|
    interaction.response.body = "response for #{cassette.name} cassette"
  end
end

VCR.use_cassette('example') do
  response = Net::HTTP.get_response('localhost', '/', 7777)
  puts "Response: #{response.body}"
end

When I run ruby before_playback_example.rb

Then it should pass with "Response: response for example cassette".

Prevent playback by ignoring interaction in before_playback hook

Given a file named "before_playback_ignore.rb" with:

$server = start_sinatra_app do
  get('/') { "sinatra response" }
end

require 'vcr'

VCR.configure do |c|
  c.hook_into :webmock
  c.cassette_library_dir = 'cassettes'
  c.before_playback { |i| i.ignore! }
end

VCR.use_cassette('localhost', :record => :new_episodes, :match_requests_on => [:method, :host, :path]) do
  response = Net::HTTP.get_response('localhost', '/', $server.port)
  puts "Response: #{response.body}"
end

When I run ruby before_playback_ignore.rb

Then it should pass with "Response: sinatra response".

Multiple hooks are run in order

Given a file named "multiple_hooks.rb" with:

require 'vcr'

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

  c.before_playback { puts "In before_playback hook 1" }
  c.before_playback { puts "In before_playback hook 2" }
end

VCR.use_cassette('example', :record => :new_episodes) do
  response = Net::HTTP.get_response('localhost', '/', 7777)
  puts "Response: #{response.body}"
end

When I run ruby multiple_hooks.rb

Then it should pass with:

In before_playback hook 1
In before_playback hook 2
Response: previously recorded response

Use tagging to apply hooks to only certain cassettes

Given a file named "tagged_hooks.rb" with:

require 'vcr'

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

  c.before_playback(:tag_2) do |i|
    puts "In before_playback hook for tag_2"
  end
end

[:tag_1, :tag_2, nil].each do |tag|
  puts
  puts "Using tag: #{tag.inspect}"

  VCR.use_cassette('example', :record => :new_episodes, :tag => tag) do
    response = Net::HTTP.get_response('localhost', '/', 7777)
    puts "Response: #{response.body}"
  end
end

When I run ruby tagged_hooks.rb

Then it should pass with:

Using tag: :tag_1
Response: previously recorded response

Using tag: :tag_2
In before_playback hook for tag_2
Response: previously recorded response

Using tag: nil
Response: previously recorded response
PreviousBefore Record HookNextBefore HTTP Request Hook

Last updated 5 years ago

Was this helpful?