diff --git a/spec/monkey_patches.rb b/spec/monkey_patches.rb index 4072bc68..aeccce05 100644 --- a/spec/monkey_patches.rb +++ b/spec/monkey_patches.rb @@ -12,9 +12,14 @@ module MonkeyPatches def enable!(scope) case scope - when :all - MONKEY_PATCHES.each do |mp| - realias mp.first, mp.last, :with_monkeypatches + when :fakeweb + realias_all :with_fakeweb + enable!(:vcr) # fakeweb adapter relies upon VCR's Net::HTTP monkey patch + when :webmock + ::WebMock::HttpLibAdapters::NetHttpAdapter.enable! + ::WebMock::HttpLibAdapters::TyphoeusAdapter.enable! + $original_webmock_callbacks.each do |cb| + ::WebMock::CallbackRegistry.add_callback(cb[:options], cb[:block]) end when :vcr realias Net::HTTP, :request, :with_vcr @@ -22,15 +27,13 @@ def enable!(scope) end end - def disable!(scope) - case scope - when :all - MONKEY_PATCHES.each do |mp| - realias mp.first, mp.last, :without_monkeypatches - end - when :vcr - realias Net::HTTP, :request, :without_vcr - else raise ArgumentError.new("Unexpected scope: #{scope}") + def disable_all! + realias_all :without_monkeypatches + + if defined?(::WebMock) + ::WebMock::HttpLibAdapters::NetHttpAdapter.disable! + ::WebMock::HttpLibAdapters::TyphoeusAdapter.disable! + ::WebMock::CallbackRegistry.reset end end @@ -45,13 +48,7 @@ def init def capture_method_definition(klass, method, original) klass.class_eval do - monkeypatch_methods = [ - :with_vcr, :without_vcr, - :with_fakeweb, :without_fakeweb, - :with_webmock, :without_webmock - ].select do |m| - method_defined?(:"#{method}_#{m}") - end + monkeypatch_methods = [:vcr, :fakeweb].select { |m| method_defined?(:"#{method}_with_#{m}") } if original if monkeypatch_methods.size > 0 @@ -78,6 +75,12 @@ def capture_method_definition(klass, method, original) def realias(klass, method, alias_extension) klass.class_eval { alias_method method, :"#{method}_#{alias_extension}" } end + + def realias_all(alias_extension) + MONKEY_PATCHES.each do |mp| + realias mp.first, mp.last, alias_extension + end + end end # Require all the HTTP libraries--these must be required before WebMock @@ -91,13 +94,26 @@ def realias(klass, method, alias_extension) require 'typhoeus' end -# The FakeWeb adapter must be required after WebMock's so -# that VCR's Net::HTTP monkey patch is loaded last. -# This allows us to disable it (i.e. by realiasing to -# the version of Net::HTTP's methods before it was loaded) -require 'vcr/http_stubbing_adapters/webmock' require 'vcr/http_stubbing_adapters/fakeweb' # All Net::HTTP monkey patches have now been loaded, so capture the # appropriate method definitions so we can disable them later. MonkeyPatches.init + +# Disable FakeWeb/VCR Net::HTTP patches before WebMock +# subclasses Net::HTTP and inherits them... +MonkeyPatches.disable_all! + +require 'vcr/http_stubbing_adapters/webmock' +$original_webmock_callbacks = ::WebMock::CallbackRegistry.callbacks + +# disable all by default; we'll enable specific ones when we need them +MonkeyPatches.disable_all! + +RSpec.configure do |config| + [:fakeweb, :webmock, :vcr].each do |scope| + config.before(:all, :with_monkey_patches => scope) { MonkeyPatches.enable!(scope) } + config.after(:all, :with_monkey_patches => scope) { MonkeyPatches.disable_all! } + end +end + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3aa2ea86..62ae3c56 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -78,22 +78,6 @@ def reset!(stubbing_lib = :fakeweb) $stderr = @orig_std_err end - config.before(:all, :without_webmock_callbacks => true) do - @original_webmock_callbacks = ::WebMock::CallbackRegistry.callbacks - ::WebMock::CallbackRegistry.reset - end - - config.after(:all, :without_webmock_callbacks => true) do - @original_webmock_callbacks.each do |cb| - ::WebMock::CallbackRegistry.add_callback(cb[:options], cb[:block]) - end - end - - [:all, :vcr].each do |scope| - config.before(:each, :without_monkey_patches => scope) { MonkeyPatches.disable!(scope) } - config.after(:each, :without_monkey_patches => scope) { MonkeyPatches.enable!(scope) } - end - config.filter_run :focus => true config.run_all_when_everything_filtered = true diff --git a/spec/vcr/extensions/net_http_response_spec.rb b/spec/vcr/extensions/net_http_response_spec.rb index f0c47906..551e6604 100644 --- a/spec/vcr/extensions/net_http_response_spec.rb +++ b/spec/vcr/extensions/net_http_response_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe VCR::Net::HTTPResponse, :without_monkey_patches => :all do +describe VCR::Net::HTTPResponse do def self.it_allows_the_body_to_be_read(expected_regex) it 'allows the body to be read using #body' do response.body.to_s.should =~ expected_regex diff --git a/spec/vcr/extensions/net_http_spec.rb b/spec/vcr/extensions/net_http_spec.rb index 8e5bb7a2..5da54578 100644 --- a/spec/vcr/extensions/net_http_spec.rb +++ b/spec/vcr/extensions/net_http_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe "Net::HTTP Extensions", :without_webmock_callbacks => true do +describe "Net::HTTP Extensions", :with_monkey_patches => :vcr do before(:all) { VCR::SinatraApp.port } # ensure the server is started before instantiating any Net::HTTP instances let(:uri) { URI.parse("http://localhost:#{VCR::SinatraApp.port}/") } diff --git a/spec/vcr/http_stubbing_adapters/excon_spec.rb b/spec/vcr/http_stubbing_adapters/excon_spec.rb index 827c6b95..7399e36c 100644 --- a/spec/vcr/http_stubbing_adapters/excon_spec.rb +++ b/spec/vcr/http_stubbing_adapters/excon_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe VCR::HttpStubbingAdapters::Excon, :without_monkey_patches => :vcr do +describe VCR::HttpStubbingAdapters::Excon do it_behaves_like 'an http stubbing adapter', ['excon'], [:method, :uri, :host, :path, :body, :headers], diff --git a/spec/vcr/http_stubbing_adapters/fakeweb_spec.rb b/spec/vcr/http_stubbing_adapters/fakeweb_spec.rb index a4d88f79..55793079 100644 --- a/spec/vcr/http_stubbing_adapters/fakeweb_spec.rb +++ b/spec/vcr/http_stubbing_adapters/fakeweb_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe VCR::HttpStubbingAdapters::FakeWeb, :without_webmock_callbacks => true do +describe VCR::HttpStubbingAdapters::FakeWeb, :with_monkey_patches => :fakeweb do it_behaves_like 'an http stubbing adapter', ['net/http'], [:method, :uri, :host, :path], :needs_net_http_extension it_performs('version checking', diff --git a/spec/vcr/http_stubbing_adapters/faraday_spec.rb b/spec/vcr/http_stubbing_adapters/faraday_spec.rb index e0c78fec..7fe06665 100644 --- a/spec/vcr/http_stubbing_adapters/faraday_spec.rb +++ b/spec/vcr/http_stubbing_adapters/faraday_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe VCR::HttpStubbingAdapters::Faraday, :without_monkey_patches => :all do +describe VCR::HttpStubbingAdapters::Faraday do it_behaves_like 'an http stubbing adapter', %w[ faraday-typhoeus faraday-net_http faraday-patron ], [:method, :uri, :host, :path, :body, :headers], diff --git a/spec/vcr/http_stubbing_adapters/typhoeus_spec.rb b/spec/vcr/http_stubbing_adapters/typhoeus_spec.rb index ef79f662..0a23c673 100644 --- a/spec/vcr/http_stubbing_adapters/typhoeus_spec.rb +++ b/spec/vcr/http_stubbing_adapters/typhoeus_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe VCR::HttpStubbingAdapters::Typhoeus, :without_monkey_patches => :vcr do +describe VCR::HttpStubbingAdapters::Typhoeus do before(:each) do ::Typhoeus::Hydra.stubs = [] ::Typhoeus::Hydra.allow_net_connect = true diff --git a/spec/vcr/http_stubbing_adapters/webmock_spec.rb b/spec/vcr/http_stubbing_adapters/webmock_spec.rb index 338ad22c..62649f1c 100644 --- a/spec/vcr/http_stubbing_adapters/webmock_spec.rb +++ b/spec/vcr/http_stubbing_adapters/webmock_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe VCR::HttpStubbingAdapters::WebMock, :without_monkey_patches => :vcr do +describe VCR::HttpStubbingAdapters::WebMock, :with_monkey_patches => :webmock do it_behaves_like 'an http stubbing adapter', %w[net/http patron httpclient em-http-request curb], [:method, :uri, :host, :path, :body, :headers]