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
  • Modify recorded response
  • Modify recorded response based on the cassette
  • Prevent recording by ignoring interaction in before_record hook
  • Multiple hooks are run in order
  • Use tagging to apply hook to only certain cassettes

Was this helpful?

  1. Hooks

Before Record Hook

The before_record hook is called before a cassette is written to disk. This can be used to modify the HTTP interaction before it is recorded.

Your block should accept up to 2 arguments. The first argument will be the HTTP interaction that is about to be written to disk. The second argument will be the current cassette.

If you wish to prevent VCR from recording the HTTP interaction you can call #ignore! on the interaction.

If you don't want your hook to apply to all cassettes, you can use tags to select which cassettes a given hook applies to. Consider this code:

  VCR.configure do |c|
    c.before_record(: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.

Modify recorded response

Given a file named "before_record_example.rb" with:

$server = start_sinatra_app do
  get('/') { "Hello Earth" }
end

require 'vcr'

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

  c.before_record do |i|
    i.response.body.sub!('Earth', 'World')
  end
end

VCR.use_cassette('recording_example') do
  Net::HTTP.get_response('localhost', '/', $server.port)
end

When I run ruby before_record_example.rb

Then the file "cassettes/recording_example.yml" should contain "Hello World"

And the file "cassettes/recording_example.yml" should not contain "Earth".

Modify recorded response based on the cassette

Given a file named "before_record_example.rb" with:

$server = start_sinatra_app do
  get('/') { "Hello Earth" }
end

require 'vcr'

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

  c.before_record do |interaction, cassette|
    interaction.response.body << " (#{cassette.name})"
  end
end

VCR.use_cassette('recording_example') do
  Net::HTTP.get_response('localhost', '/', $server.port)
end

When I run ruby before_record_example.rb

Then the file "cassettes/recording_example.yml" should contain "Hello Earth (recording_example)".

Prevent recording by ignoring interaction in before_record hook

Given a file named "before_record_ignore.rb" with:

$server = start_sinatra_app do
  get('/') { "Hello World" }
end

require 'vcr'

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

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

When I run ruby before_record_ignore.rb

Then it should pass with "Response: Hello World"

And the file "cassettes/recording_example.yml" should not exist.

Multiple hooks are run in order

Given a file named "multiple_hooks.rb" with:

$server = start_sinatra_app do
  get('/') { "Hello World" }
end

require 'vcr'

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

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

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

When I run ruby multiple_hooks.rb

Then it should pass with:

Response: Hello World
In before_record hook 1
In before_record hook 2

Use tagging to apply hook to only certain cassettes

Given a file named "tagged_hooks.rb" with:

$server = start_sinatra_app do
  get('/') { "Hello World" }
end

require 'vcr'

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

  c.before_record(:tag_1) do
    puts "In before_record hook for tag_1"
  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', '/', $server.port)
    puts "Response: #{response.body}"
  end
end

When I run ruby tagged_hooks.rb

Then it should pass with:

Using tag: :tag_1
Response: Hello World
In before_record hook for tag_1

Using tag: :tag_2
Response: Hello World

Using tag: nil
Response: Hello World
PreviousQuery ParserNextBefore Playback Hook

Last updated 4 years ago

Was this helpful?