spec/monkey_patches.rb in vcr-1.1.2 vs spec/monkey_patches.rb in vcr-1.2.0

- old
+ new

@@ -1,26 +1,45 @@ module MonkeyPatches extend self + module RSpecMacros + def without_monkey_patches(scope) + before(:each) { MonkeyPatches.disable!(scope) } + after(:each) { MonkeyPatches.enable!(scope) } + end + end + NET_HTTP_SINGLETON = class << Net::HTTP; self; end MONKEY_PATCHES = [ [Net::BufferedIO, :initialize], [Net::HTTP, :request], [Net::HTTP, :connect], [NET_HTTP_SINGLETON, :socket_type] ] - def enable! - MONKEY_PATCHES.each do |mp| - realias mp.first, mp.last, :with_monkeypatches + def enable!(scope) + case scope + when :all + MONKEY_PATCHES.each do |mp| + realias mp.first, mp.last, :with_monkeypatches + end + when :vcr + realias Net::HTTP, :request, :with_vcr + else raise ArgumentError.new("Unexpected scope: #{scope}") end end - def disable! - MONKEY_PATCHES.each do |mp| - realias mp.first, mp.last, :without_monkeypatches + 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}") end end def init # capture the monkey patched definitions so we can realias to them in the future @@ -66,5 +85,25 @@ def realias(klass, method, alias_extension) klass.class_eval { alias_method method, :"#{method}_#{alias_extension}" } end end +# Require all the HTTP libraries--these must be required before WebMock +# for WebMock to work with them. +require 'httpclient' + +if RUBY_INTERPRETER == :mri + require 'patron' + require 'em-http-request' + require 'curb' +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