Permalink
Browse files

Allow FakeWeb/WebMock/Typhoeus stubbing APIs to be used when VCR is t…

…urned off.

Closes #112.
  • Loading branch information...
1 parent c74dde6 commit 732d24c6280b533313f5750b9b91568a828980d2 @myronmarston committed Dec 5, 2011
@@ -137,9 +137,13 @@ module Net
class HTTP
unless method_defined?(:request_with_vcr)
def request_with_vcr(*args, &block)
- VCR::LibraryHooks::FakeWeb::RequestHandler.new(
- self, *args, &block
- ).handle
+ if VCR.turned_on?
+ VCR::LibraryHooks::FakeWeb::RequestHandler.new(
+ self, *args, &block
+ ).handle
+ else
+ request_without_vcr(*args, &block)
+ end
end
alias request_without_vcr request
@@ -148,6 +152,17 @@ def request_with_vcr(*args, &block)
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
if defined?(WebMock)
raise ArgumentError.new("You have configured VCR to hook into both :fakeweb and :webmock. You cannot use both.")
@@ -83,6 +83,17 @@ def stubbed_response_headers
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
# ensure WebMock's Typhoeus adapter does not conflict with us here
# (i.e. to double record requests or whatever).
@@ -79,9 +79,11 @@ def on_stubbed_request
class << WebMock
# ensure HTTP requests are always allowed; VCR takes care of disallowing
# them at the appropriate times in its hook
- undef net_connect_allowed?
- def net_connect_allowed?(*args)
- true
+ def net_connect_allowed_with_vcr?(*args)
+ VCR.turned_on? ? true : net_connect_allowed_without_vcr?
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?)
@@ -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")}"
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
context 'when there is an around_http_request hook' do
before(:each) do
@@ -1,6 +1,12 @@
require 'spec_helper'
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_performs('version checking', 'Excon',
@@ -1,6 +1,23 @@
require 'spec_helper'
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
before(:each) do
VCR::LibraryHooks::FakeWeb::RequestHandler.already_seen_requests.clear
@@ -1,6 +1,24 @@
require 'spec_helper'
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'
def stub_callback_registration
@@ -1,6 +1,23 @@
require 'spec_helper'
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|
it_behaves_like 'a hook into an HTTP library', :webmock, lib do
if lib == 'net/http'

0 comments on commit 732d24c

Please sign in to comment.