Contents

ignore_localhost configuration option

The ignore_localhost configuration option can be used to prevent VCR from having any affect on localhost requests. If set to true, it will never record them and always allow them, regardless of the record mode, and even outside of a VCR.use_cassette block.

This is particularly useful when you use VCR with Capybara, since Capybara starts a localhost server and pings it when you use one of its javascript drivers.

Background:

  • Given a file named "sinatra_app.rb" with:
    ruby
    require 'vcr_cucumber_helpers'
    
    response_count = 0
    start_sinatra_app(:port => 7777) do
      get('/') { "Response #{response_count += 1}" }
    end

Scenario Outline: localhost requests are not treated differently by default and when the setting is false

  • Given a file named "localhost_not_ignored.rb" with:
    ruby
    require 'vcr_cucumber_helpers'
    include_http_adapter_for("<http_lib>")
    require 'sinatra_app.rb'
    
    require 'vcr'
    
    VCR.config do |c|
      c.cassette_library_dir = 'cassettes'
      c.stub_with <stub_with>
      <additional_config>
    end
    
    VCR.use_cassette('localhost', :record => :new_episodes) do
      response_body_for(:get, "http://localhost:7777/")
    end
    
    response_body_for(:get, "http://localhost:7777/")
  • When I run "ruby localhost_not_ignored.rb"
  • Then it should fail with "<error>"
  • And the file "cassettes/localhost.yml" should contain "body: Response 1"

Examples:

stub_withhttp_liberroradditional_config
:fakewebnet/httpReal HTTP connections are disabled
:fakewebnet/httpReal HTTP connections are disabledc.ignore_localhost = false
:webmocknet/httpReal HTTP connections are disabled
:webmocknet/httpReal HTTP connections are disabledc.ignore_localhost = false
:webmockhttpclientReal HTTP connections are disabled
:webmockhttpclientReal HTTP connections are disabledc.ignore_localhost = false
:webmockcurbReal HTTP connections are disabled
:webmockcurbReal HTTP connections are disabledc.ignore_localhost = false
:webmockpatronReal HTTP connections are disabled
:webmockpatronReal HTTP connections are disabledc.ignore_localhost = false
:webmockem-http-requestReal HTTP connections are disabled
:webmockem-http-requestReal HTTP connections are disabledc.ignore_localhost = false
:typhoeustyphoeusReal HTTP requests are not allowed
:typhoeustyphoeusReal HTTP requests are not allowedc.ignore_localhost = false

Scenario Outline: localhost requests are allowed and not recorded when ignore_localhost = true

  • Given a file named "ignore_localhost_true.rb" with:
    ruby
    require 'vcr_cucumber_helpers'
    include_http_adapter_for("<http_lib>")
    require 'sinatra_app.rb'
    
    require 'vcr'
    
    VCR.config do |c|
      c.cassette_library_dir = 'cassettes'
      c.stub_with <stub_with>
      c.ignore_localhost = true
    end
    
    VCR.use_cassette('localhost', :record => :new_episodes) do
      puts response_body_for(:get, "http://localhost:7777/")
    end
    
    puts response_body_for(:get, "http://localhost:7777/")
  • When I run "ruby ignore_localhost_true.rb"
  • Then it should pass with:
    Response 1
    Response 2
  • And the file "cassettes/localhost.yml" should not exist

Examples:

stub_withhttp_lib
:fakewebnet/http
:webmocknet/http
:webmockhttpclient
:webmockpatron
:webmockcurb
:webmockem-http-request
:typhoeustyphoeus

Source: features/configuration/ignore_localhost.feature at 1.3.3, VCR (MIT license).