Skip to content

Commit

Permalink
Allow FakeWeb/WebMock/Typhoeus stubbing APIs to be used when VCR is t…
Browse files Browse the repository at this point in the history
…urned off.

Closes vcr#112.
  • Loading branch information
myronmarston committed Dec 6, 2011
1 parent c74dde6 commit 732d24c
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 7 deletions.
21 changes: 18 additions & 3 deletions lib/vcr/library_hooks/fakeweb.rb
Expand Up @@ -137,9 +137,13 @@ module Net
class HTTP class HTTP
unless method_defined?(:request_with_vcr) unless method_defined?(:request_with_vcr)
def request_with_vcr(*args, &block) def request_with_vcr(*args, &block)
VCR::LibraryHooks::FakeWeb::RequestHandler.new( if VCR.turned_on?
self, *args, &block VCR::LibraryHooks::FakeWeb::RequestHandler.new(
).handle self, *args, &block
).handle
else
request_without_vcr(*args, &block)
end
end end


alias request_without_vcr request alias request_without_vcr request
Expand All @@ -148,6 +152,17 @@ def request_with_vcr(*args, &block)
end end
end end


class << FakeWeb
# ensure HTTP requests are always allowed; VCR takes care of disallowing
# them at the appropriate times in its hook
def allow_net_connect_with_vcr?(*args)
VCR.turned_on? ? true : allow_net_connect_without_vcr?
end

alias allow_net_connect_without_vcr? allow_net_connect?
alias allow_net_connect? allow_net_connect_with_vcr?
end unless FakeWeb.respond_to?(:allow_net_connect_with_vcr?)

VCR.configuration.after_library_hooks_loaded do VCR.configuration.after_library_hooks_loaded do
if defined?(WebMock) if defined?(WebMock)
raise ArgumentError.new("You have configured VCR to hook into both :fakeweb and :webmock. You cannot use both.") raise ArgumentError.new("You have configured VCR to hook into both :fakeweb and :webmock. You cannot use both.")
Expand Down
11 changes: 11 additions & 0 deletions lib/vcr/library_hooks/typhoeus.rb
Expand Up @@ -83,6 +83,17 @@ def stubbed_response_headers
end end
end end


class << Typhoeus::Hydra
# ensure HTTP requests are always allowed; VCR takes care of disallowing
# them at the appropriate times in its hook
def allow_net_connect_with_vcr?(*args)
VCR.turned_on? ? true : allow_net_connect_without_vcr?
end

alias allow_net_connect_without_vcr? allow_net_connect?
alias allow_net_connect? allow_net_connect_with_vcr?
end unless Typhoeus::Hydra.respond_to?(:allow_net_connect_with_vcr?)

VCR.configuration.after_library_hooks_loaded do VCR.configuration.after_library_hooks_loaded do
# ensure WebMock's Typhoeus adapter does not conflict with us here # ensure WebMock's Typhoeus adapter does not conflict with us here
# (i.e. to double record requests or whatever). # (i.e. to double record requests or whatever).
Expand Down
10 changes: 6 additions & 4 deletions lib/vcr/library_hooks/webmock.rb
Expand Up @@ -79,9 +79,11 @@ def on_stubbed_request
class << WebMock class << WebMock
# ensure HTTP requests are always allowed; VCR takes care of disallowing # ensure HTTP requests are always allowed; VCR takes care of disallowing
# them at the appropriate times in its hook # them at the appropriate times in its hook
undef net_connect_allowed? def net_connect_allowed_with_vcr?(*args)
def net_connect_allowed?(*args) VCR.turned_on? ? true : net_connect_allowed_without_vcr?
true
end end
end
alias net_connect_allowed_without_vcr? net_connect_allowed?
alias net_connect_allowed? net_connect_allowed_with_vcr?
end unless WebMock.respond_to?(:net_connect_allowed_with_vcr?)


40 changes: 40 additions & 0 deletions spec/support/shared_example_groups/hook_into_http_library.rb
Expand Up @@ -109,6 +109,46 @@ def self.test_playback(description, url)
test_playback "with an encoded ampersand", "http://example.com:80/search?q=#{CGI.escape("Q&A")}" test_playback "with an encoded ampersand", "http://example.com:80/search?q=#{CGI.escape("Q&A")}"
end end


describe "using the library's stubbing/disconnection APIs" do
let!(:request_url) { "http://localhost:#{VCR::SinatraApp.port}/foo" }

if method_defined?(:disable_real_connections)
it 'can make a real request when VCR is turned off' do
enable_real_connections
VCR.turn_off!
get_body_string(make_http_request(:get, request_url)).should eq("FOO!")
end

it 'does not mess with VCR when real connections are disabled' do
VCR.insert_cassette('example')
disable_real_connections

VCR.should_receive(:record_http_interaction) do |interaction|
interaction.request.uri.should eq(request_url)
end

make_http_request(:get, request_url)
end

it 'can disable real connections when VCR is turned off' do
VCR.turn_off!
expected_error = disable_real_connections

expect {
make_http_request(:get, request_url)
}.to raise_error(expected_error)
end
end

if method_defined?(:directly_stub_request)
it 'can directly stub the request when VCR is turned off' do
VCR.turn_off!
directly_stub_request(:get, request_url, "stubbed response")
get_body_string(make_http_request(:get, request_url)).should eq("stubbed response")
end
end
end

describe "request hooks" do describe "request hooks" do
context 'when there is an around_http_request hook' do context 'when there is an around_http_request hook' do
before(:each) do before(:each) do
Expand Down
6 changes: 6 additions & 0 deletions spec/vcr/library_hooks/excon_spec.rb
@@ -1,6 +1,12 @@
require 'spec_helper' require 'spec_helper'


describe "Excon hook" do describe "Excon hook" do
# TODO: figure out a way to get disabling VCR to work with Excon
# and allow dirct excon stubs to work.
# def directly_stub_request(method, url, response_body)
# ::Excon.stub({ :method => method, :url => url }, { :body => response_body })
# end

it_behaves_like 'a hook into an HTTP library', :excon, 'excon', :status_message_not_exposed it_behaves_like 'a hook into an HTTP library', :excon, 'excon', :status_message_not_exposed


it_performs('version checking', 'Excon', it_performs('version checking', 'Excon',
Expand Down
17 changes: 17 additions & 0 deletions spec/vcr/library_hooks/fakeweb_spec.rb
@@ -1,6 +1,23 @@
require 'spec_helper' require 'spec_helper'


describe "FakeWeb hook", :with_monkey_patches => :fakeweb do describe "FakeWeb hook", :with_monkey_patches => :fakeweb do
after(:each) do
::FakeWeb.clean_registry
end

def disable_real_connections
::FakeWeb.allow_net_connect = false
::FakeWeb::NetConnectNotAllowedError
end

def enable_real_connections
::FakeWeb.allow_net_connect = true
end

def directly_stub_request(method, url, response_body)
::FakeWeb.register_uri(method, url, :body => response_body)
end

it_behaves_like 'a hook into an HTTP library', :fakeweb, 'net/http' do it_behaves_like 'a hook into an HTTP library', :fakeweb, 'net/http' do
before(:each) do before(:each) do
VCR::LibraryHooks::FakeWeb::RequestHandler.already_seen_requests.clear VCR::LibraryHooks::FakeWeb::RequestHandler.already_seen_requests.clear
Expand Down
18 changes: 18 additions & 0 deletions spec/vcr/library_hooks/typhoeus_spec.rb
@@ -1,6 +1,24 @@
require 'spec_helper' require 'spec_helper'


describe "Typhoeus hook", :with_monkey_patches => :typhoeus do describe "Typhoeus hook", :with_monkey_patches => :typhoeus do
after(:each) do
::Typhoeus::Hydra.clear_stubs
end

def disable_real_connections
::Typhoeus::Hydra.allow_net_connect = false
::Typhoeus::Hydra::NetConnectNotAllowedError
end

def enable_real_connections
::Typhoeus::Hydra.allow_net_connect = true
end

def directly_stub_request(method, url, response_body)
response = ::Typhoeus::Response.new(:code => 200, :body => response_body)
::Typhoeus::Hydra.stub(method, url).and_return(response)
end

it_behaves_like 'a hook into an HTTP library', :typhoeus, 'typhoeus' it_behaves_like 'a hook into an HTTP library', :typhoeus, 'typhoeus'


def stub_callback_registration def stub_callback_registration
Expand Down
17 changes: 17 additions & 0 deletions spec/vcr/library_hooks/webmock_spec.rb
@@ -1,6 +1,23 @@
require 'spec_helper' require 'spec_helper'


describe "WebMock hook", :with_monkey_patches => :webmock do describe "WebMock hook", :with_monkey_patches => :webmock do
after(:each) do
::WebMock.reset!
end

def disable_real_connections
::WebMock.disable_net_connect!
::WebMock::NetConnectNotAllowedError
end

def enable_real_connections
::WebMock.allow_net_connect!
end

def directly_stub_request(method, url, response_body)
::WebMock.stub_request(method, url).to_return(:body => response_body)
end

%w[net/http patron httpclient em-http-request curb typhoeus].each do |lib| %w[net/http patron httpclient em-http-request curb typhoeus].each do |lib|
it_behaves_like 'a hook into an HTTP library', :webmock, lib do it_behaves_like 'a hook into an HTTP library', :webmock, lib do
if lib == 'net/http' if lib == 'net/http'
Expand Down

0 comments on commit 732d24c

Please sign in to comment.