:Once
The :once
record mode will:
- Replay previously recorded interactions.
- Record new interactions if there is no cassette file.
- Cause an error to be raised for new requests if there is a cassette file.
It is similar to the :new_episodes
record mode, but will prevent new,
unexpected requests from being made (i.e. because the request URI changed
or whatever).
:once
is the default record mode, used when you do not set one.BackgroundGivena file named “setup.rb” with:
$server = start_sinatra_app do
get('/') { 'Hello' }
end
require 'vcr'
VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = 'cassettes'
end
Anda 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:
- "20"
body:
encoding: UTF-8
string: example.com response
http_version: "1.1"
recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0
Scenarios
- Previously recorded responses are replayed
- New requests result in an error when the cassette file exists
- New requests get recorded when there is no cassette file
Previously recorded responses are replayedGivena file named “replay_recorded_response.rb” with:
require 'setup'
VCR.use_cassette('example', :record => :once) do
response = Net::HTTP.get_response('example.com', '/foo')
puts "Response: #{response.body}"
end
WhenI run ruby replay_recorded_response.rb
Thenit should pass with “Response: example.com response”
- @exclude-jruby
New requests result in an error when the cassette file existsGivena file named “error_for_new_requests_when_cassette_exists.rb” with:
require 'setup'
VCR.use_cassette('example', :record => :once) do
response = Net::HTTP.get_response('localhost', '/', $server.port)
puts "Response: #{response.body}"
end
WhenI run ruby error_for_new_requests_when_cassette_exists.rb
Thenit should fail with “An HTTP request has been made that VCR does not know how to handle”New requests get recorded when there is no cassette fileGivena file named “record_new_requests.rb” with:
require 'setup'
VCR.use_cassette('example', :record => :once) do
response = Net::HTTP.get_response('localhost', '/', $server.port)
puts "Response: #{response.body}"
end
WhenI remove the file “cassettes/example.yml”AndI run ruby record_new_requests.rb
Thenit should pass with “Response: Hello”Andthe file “cassettes/example.yml” should contain “Hello”
:New_episodes
The :new_episodes
record mode will:
- Record new interactions.
- Replay previously recorded interactions.
It is similar to the :once
record mode, but will always record new
interactions, even if you have an existing recorded one that is similar
(but not identical, based on the :match_request_on
option).BackgroundGivena file named “setup.rb” with:
$server = start_sinatra_app do
get('/') { 'Hello' }
end
require 'vcr'
VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = 'cassettes'
end
Anda 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:
- "20"
body:
encoding: UTF-8
string: example.com response
http_version: "1.1"
recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0
ScenariosPreviously recorded responses are replayedGivena file named “replay_recorded_response.rb” with:
require 'setup'
VCR.use_cassette('example', :record => :new_episodes) do
response = Net::HTTP.get_response('example.com', '/foo')
puts "Response: #{response.body}"
end
WhenI run ruby replay_recorded_response.rb
Thenit should pass with “Response: example.com response”New requests get recordedGivena file named “record_new_requests.rb” with:
require 'setup'
VCR.use_cassette('example', :record => :new_episodes) do
response = Net::HTTP.get_response('localhost', '/', $server.port)
puts "Response: #{response.body}"
end
WhenI run ruby record_new_requests.rb
Thenit should pass with “Response: Hello”Andthe file “cassettes/example.yml” should contain each of these:
example.com response |
Hello |
:None
The :none
record mode will:
- Replay previously recorded interactions.
- Cause an error to be raised for any new requests.
This is useful when your code makes potentially dangerous
HTTP requests. The :none
record mode guarantees that no
new HTTP requests will be made.BackgroundGivena file named “vcr_config.rb” with:
require 'vcr'
VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = 'cassettes'
end
Anda 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:
- "5"
body:
encoding: UTF-8
string: Hello
http_version: "1.1"
recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0
ScenariosPreviously recorded responses are replayedGivena file named “replay_recorded_response.rb” with:
require 'vcr_config'
VCR.use_cassette('example', :record => :none) do
response = Net::HTTP.get_response('example.com', '/foo')
puts "Response: #{response.body}"
end
WhenI run ruby replay_recorded_response.rb
Thenit should pass with “Response: Hello”
- @exclude-jruby
New requests are preventedGivena file named “prevent_new_request.rb” with:
require 'vcr_config'
VCR.use_cassette('example', :record => :none) do
Net::HTTP.get_response('example.com', '/bar')
end
WhenI run ruby prevent_new_request.rb
Thenit should fail with “An HTTP request has been made that VCR does not know how to handle”
:All
The :all
record mode will:
- Record new interactions.
- Never replay previously recorded interactions.
This can be temporarily used to force VCR to re-record
a cassette (i.e. to ensure the responses are not out of date)
or can be used when you simply want to log all HTTP requests.BackgroundGivena file named “setup.rb” with:
$server = start_sinatra_app do
get('/') { 'Hello' }
get('/foo') { 'Goodbye' }
end
require 'vcr'
VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = 'cassettes'
end
Anda previously recorded cassette file “cassettes/example.yml” with:
---
http_interactions:
- request:
method: get
uri: http://localhost/
body:
encoding: UTF-8
string: ""
headers: {}
response:
status:
code: 200
message: OK
headers:
Content-Length:
- "20"
body:
encoding: UTF-8
string: old response
http_version: "1.1"
recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0
ScenariosRe-record previously recorded responseGivena file named “re_record.rb” with:
require 'setup'
VCR.use_cassette('example', :record => :all, :match_requests_on => [:method, :host, :path]) do
response = Net::HTTP.get_response('localhost', '/', $server.port)
puts "Response: #{response.body}"
end
WhenI run ruby re_record.rb
Thenit should pass with “Response: Hello”Andthe file “cassettes/example.yml” should contain “Hello”Butthe file “cassettes/example.yml” should not contain “old response”Record new requestGivena file named “record_new.rb” with:
require 'setup'
VCR.use_cassette('example', :record => :all) do
response = Net::HTTP.get_response('localhost', '/foo', $server.port)
puts "Response: #{response.body}"
end
WhenI run ruby record_new.rb
Thenit should pass with “Response: Goodbye”Andthe file “cassettes/example.yml” should contain each of these:
old response |
Goodbye |
:Record_on_error
The :record_on_error
flag mode will prevent a cassette from being recorded when the code
that uses the cassette (a test) raises an error (test failure).BackgroundGivena file named “setup.rb” with:
$server = start_sinatra_app do
get('/') { 'Hello' }
end
require 'vcr'
VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = 'cassettes'
end
Scenarios
- Requests are recorded when no error is raised
- Requests are not recorded when an error is raised and :record_on_error is set to false
- Requests are recorded when an error is raised and :record_on_error is set to true
Requests are recorded when no error is raisedGivena file named “record_when_no_error.rb” with:
require 'setup'
VCR.use_cassette('example', :record_on_error => false) do
response = Net::HTTP.get_response('localhost', '/', $server.port)
puts "Response: #{response.body}"
end
WhenI run ruby record_when_no_error.rb
Thenit should pass with “Response: Hello”Andthe file “cassettes/example.yml” should contain “Hello”Requests are not recorded when an error is raised and :record_on_error is set to falseGivena file named “do_not_record_on_error.rb” with:
require 'setup'
VCR.use_cassette('example', :record => :once, :record_on_error => false) do
Net::HTTP.get_response('localhost', '/', $server.port)
raise StandardError, 'The example failed'
end
WhenI run ruby do_not_record_on_error.rb
Thenit should fail with “The example failed”Andthe file “cassettes/example.yml” should not existRequests are recorded when an error is raised and :record_on_error is set to trueGivena file named “record_on_error.rb” with:
require 'setup'
VCR.use_cassette('example', :record => :once, :record_on_error => true) do
Net::HTTP.get_response('localhost', '/', $server.port)
raise StandardError, 'The example failed'
end
WhenI run ruby record_on_error.rb
Thenit should fail with “The example failed”Butthe file “cassettes/example.yml” should contain “Hello”