1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
Feature: 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.
Scenario: Modify recorded response
Given a file named "before_record_example.rb" with:
"""ruby
$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"
Scenario: Modify recorded response based on the cassette
Given a file named "before_record_example.rb" with:
"""ruby
$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)"
Scenario: Prevent recording by ignoring interaction in before_record hook
Given a file named "before_record_ignore.rb" with:
"""ruby
$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
Scenario: Multiple hooks are run in order
Given a file named "multiple_hooks.rb" with:
"""ruby
$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
"""
Scenario: Use tagging to apply hook to only certain cassettes
Given a file named "tagged_hooks.rb" with:
"""ruby
$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
"""
|