Ignore Request
By default, VCR hooks into every request, either allowing it and recording it, or playing back a recorded response, or raising an error to force you to deal with the new request. In some situations, you may prefer to have VCR ignore some requests.
VCR provides 3 configuration options to accomplish this:
ignore_request { |req| ... }
will ignore any request for which thegiven block returns true.
ignore_hosts 'foo.com', 'bar.com'
allows you to specify particularhosts to ignore.
ignore_localhost = true
is equivalent to `ignore_hosts 'localhost','127.0.0.1', '0.0.0.0'`. It is particularly useful for when you use
VCR with a javascript-enabled capybara driver, since capybara boots
your rack app and makes localhost requests to it to check that it has
booted.
unignore_hosts 'foo.com', 'bar.com'
makes VCR stop ignoring particular hosts.Ignored requests are not recorded and are always allowed, regardless of the record mode, and even outside of a
VCR.use_cassette
block.
Background ()
Given a file named "sinatra_app.rb" with:
ignore requests to a specific port
Given a file named "ignore_request.rb" with:
When I run ruby ignore_request.rb
Then it should fail with an error like:
And the output should contain:
And the file "cassettes/example.yml" should contain "Port 8888"
And the file "cassettes/example.yml" should not contain "Port 7777".
Examples
configuration | http_lib |
c.hook_into :webmock | net/http |
c.hook_into :typhoeus | typhoeus |
c.hook_into :faraday | faraday (w/ net_http) |
ignored host requests are not recorded and are always allowed
Given a file named "ignore_hosts.rb" with:
When I run ruby ignore_hosts.rb
Then it should pass with:
And the file "cassettes/example.yml" should not exist.
Examples
configuration | http_lib |
c.hook_into :webmock | net/http |
c.hook_into :typhoeus | typhoeus |
c.hook_into :excon | excon |
c.hook_into :faraday | faraday (w/ net_http) |
unignored host requests are recorded again
Given a file named "unignore_hosts.rb" with:
When I run ruby unignore_hosts.rb
Then it should pass with:
And the file "cassettes/example.yml" should not contain "Response 1"
And the file "cassettes/example.yml" should contain "Response 2".
Examples
configuration | http_lib |
c.hook_into :webmock | net/http |
c.hook_into :typhoeus | typhoeus |
c.hook_into :excon | excon |
c.hook_into :faraday | faraday (w/ net_http) |
unignored host requests are not allowed without a cassette
Given a file named "unignore_hosts_without_cassette.rb" with:
When I run ruby unignore_hosts_without_cassette.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 "Response 1"
And the output should not contain "Response 2"
And the file "cassettes/example.yml" should not exist.
Examples
configuration | http_lib |
c.hook_into :webmock | net/http |
c.hook_into :typhoeus | typhoeus |
c.hook_into :excon | excon |
c.hook_into :faraday | faraday (w/ net_http) |
localhost requests are not treated differently by default
Given a file named "localhost_not_ignored.rb" with:
When I run ruby localhost_not_ignored.rb
Then it should fail with "An HTTP request has been made that VCR does not know how to handle"
And the file "cassettes/localhost.yml" should contain "Port 7777 Response 1".
Examples
configuration | http_lib |
c.hook_into :webmock | net/http |
c.hook_into :typhoeus | typhoeus |
c.hook_into :excon | excon |
c.hook_into :faraday | faraday (w/ net_http) |
localhost requests are allowed and not recorded when ignore_localhost = true
Given a file named "ignore_localhost_true.rb" with:
When I run ruby ignore_localhost_true.rb
Then it should pass with:
And the file "cassettes/localhost.yml" should not exist.
Examples
configuration | http_lib |
c.hook_into :webmock | net/http |
c.hook_into :typhoeus | typhoeus |
c.hook_into :excon | excon |
c.hook_into :faraday | faraday (w/ net_http) |
Last updated