diff --git a/lib/vcr/cassette.rb b/lib/vcr/cassette.rb index b10523f8..51c5e8d9 100644 --- a/lib/vcr/cassette.rb +++ b/lib/vcr/cassette.rb @@ -170,7 +170,7 @@ def write_recorded_interactions_to_disk def invoke_hook(type, interactions) interactions.delete_if do |i| - VCR.configuration.invoke_hook(type, tag, i, self) + VCR.configuration.invoke_tagged_hook(type, tag, i, self) i.ignored? end end diff --git a/lib/vcr/library_hooks/typhoeus.rb b/lib/vcr/library_hooks/typhoeus.rb index 7a5a2f60..50d9a04d 100644 --- a/lib/vcr/library_hooks/typhoeus.rb +++ b/lib/vcr/library_hooks/typhoeus.rb @@ -72,7 +72,7 @@ def stubbed_response_headers VCR.record_http_interaction(http_interaction) end - VCR.configuration.invoke_hook(:after_http_request, nil, vcr_request, vcr_response) + VCR.configuration.invoke_hook(:after_http_request, vcr_request, vcr_response) end end diff --git a/lib/vcr/library_hooks/webmock.rb b/lib/vcr/library_hooks/webmock.rb index 8e6c8ecd..326aa177 100644 --- a/lib/vcr/library_hooks/webmock.rb +++ b/lib/vcr/library_hooks/webmock.rb @@ -69,7 +69,7 @@ def on_stubbed_request ::WebMock.after_request do |request, response| unless VCR.library_hooks.disabled?(:webmock) - VCR.configuration.invoke_hook(:after_http_request, nil, vcr_request_from(request), vcr_response_from(response)) + VCR.configuration.invoke_hook(:after_http_request, vcr_request_from(request), vcr_response_from(response)) end end end diff --git a/lib/vcr/request_handler.rb b/lib/vcr/request_handler.rb index eabab75d..7bdc45e2 100644 --- a/lib/vcr/request_handler.rb +++ b/lib/vcr/request_handler.rb @@ -12,12 +12,12 @@ def handle def invoke_before_request_hook return if disabled? - VCR.configuration.invoke_hook(:before_http_request, nil, vcr_request) + VCR.configuration.invoke_hook(:before_http_request, vcr_request) end def invoke_after_request_hook(vcr_response) return if disabled? - VCR.configuration.invoke_hook(:after_http_request, nil, vcr_request, vcr_response) + VCR.configuration.invoke_hook(:after_http_request, vcr_request, vcr_response) end def should_ignore? diff --git a/lib/vcr/request_ignorer.rb b/lib/vcr/request_ignorer.rb index 2dd60e37..d5fd95bf 100644 --- a/lib/vcr/request_ignorer.rb +++ b/lib/vcr/request_ignorer.rb @@ -29,8 +29,7 @@ def ignore_hosts(*hosts) end def ignore?(request) - tag = nil # we don't use tags here... - invoke_hook(:ignore_request, tag, request).any? + invoke_hook(:ignore_request, request).any? end private diff --git a/lib/vcr/util/hooks.rb b/lib/vcr/util/hooks.rb index 281489bb..af179e6b 100644 --- a/lib/vcr/util/hooks.rb +++ b/lib/vcr/util/hooks.rb @@ -8,7 +8,11 @@ def self.included(klass) klass.extend(ClassMethods) end - def invoke_hook(hook, tag=nil, *args) + def invoke_hook(hook, *args) + invoke_tagged_hook(hook, nil, *args) + end + + def invoke_tagged_hook(hook, tag, *args) hooks_for(hook, tag).map do |callback| call_block(callback, *args) end diff --git a/spec/vcr/cassette_spec.rb b/spec/vcr/cassette_spec.rb index 5ec20109..b99f6071 100644 --- a/spec/vcr/cassette_spec.rb +++ b/spec/vcr/cassette_spec.rb @@ -342,7 +342,7 @@ def stub_old_interactions(interactions) if stub_requests it 'invokes the appropriately tagged before_playback hooks' do - VCR.configuration.should_receive(:invoke_hook).with( + VCR.configuration.should_receive(:invoke_tagged_hook).with( :before_playback, :foo, an_instance_of(VCR::HTTPInteraction), @@ -403,7 +403,7 @@ def stub_old_interactions(interactions) cassette.stub!(:new_recorded_interactions).and_return(interactions) interactions.each do |i| - VCR.configuration.should_receive(:invoke_hook).with( + VCR.configuration.should_receive(:invoke_tagged_hook).with( :before_record, :foo, i, diff --git a/spec/vcr/configuration_spec.rb b/spec/vcr/configuration_spec.rb index 51066717..fe283f03 100644 --- a/spec/vcr/configuration_spec.rb +++ b/spec/vcr/configuration_spec.rb @@ -117,13 +117,13 @@ it 'adds a before_record hook that replaces the string returned by the block with the given string' do subject.filter_sensitive_data('foo', &lambda { 'bar' }) interaction.should_receive(:filter!).with('bar', 'foo') - subject.invoke_hook(:before_record, nil, interaction) + subject.invoke_hook(:before_record, interaction) end it 'adds a before_playback hook that replaces the given string with the string returned by the block' do subject.filter_sensitive_data('foo', &lambda { 'bar' }) interaction.should_receive(:filter!).with('foo', 'bar') - subject.invoke_hook(:before_playback, nil, interaction) + subject.invoke_hook(:before_playback, interaction) end it 'tags the before_record hook when given a tag' do @@ -139,14 +139,14 @@ it 'yields the interaction to the block for the before_record hook' do yielded_interaction = nil subject.filter_sensitive_data('foo', &lambda { |i| yielded_interaction = i; 'bar' }) - subject.invoke_hook(:before_record, nil, interaction) + subject.invoke_hook(:before_record, interaction) yielded_interaction.should equal(interaction) end it 'yields the interaction to the block for the before_playback hook' do yielded_interaction = nil subject.filter_sensitive_data('foo', &lambda { |i| yielded_interaction = i; 'bar' }) - subject.invoke_hook(:before_playback, nil, interaction) + subject.invoke_hook(:before_playback, interaction) yielded_interaction.should equal(interaction) end end diff --git a/spec/vcr/util/hooks_spec.rb b/spec/vcr/util/hooks_spec.rb index 7daf894d..07da1d6d 100644 --- a/spec/vcr/util/hooks_spec.rb +++ b/spec/vcr/util/hooks_spec.rb @@ -17,7 +17,7 @@ it 'clears all hooks' do subject.before_foo { invocations << :callback } subject.clear_hooks - subject.invoke_hook(:before_foo, nil) + subject.invoke_hook(:before_foo) invocations.should be_empty end end @@ -29,70 +29,74 @@ subject.invoke_hook(:before_foo).should eq([17, 12]) end - context 'with a tag argument' do - it 'invokes each of the :before_foo callbacks with a matching tag' do - subject.before_foo(:green) { invocations << :callback_1 } - subject.before_foo(:green) { invocations << :callback_2 } - - invocations.should be_empty - subject.invoke_hook(:before_foo, :green) - invocations.should eq([:callback_1, :callback_2]) - end - - it 'invokes each of the :before_foo callbacks with no tag' do - subject.before_foo { invocations << :no_tag_1 } - subject.before_foo { invocations << :no_tag_2 } - - subject.invoke_hook(:before_foo, :green) - invocations.should eq([:no_tag_1, :no_tag_2]) - end - - it 'does not invoke any callbacks with a different tag' do - subject.before_foo(:blue) { invocations << :blue_callback } - subject.invoke_hook(:before_foo, :green) - invocations.should be_empty - end + it 'invokes each of the :before_foo callbacks' do + subject.before_foo { invocations << :callback_1 } + subject.before_foo { invocations << :callback_2 } + + invocations.should be_empty + subject.invoke_hook(:before_foo) + invocations.should eq([:callback_1, :callback_2]) + end + + it 'does not invoke :before_bar callbacks' do + subject.before_bar { invocations << :bar_callback } + subject.invoke_hook(:before_foo) + invocations.should be_empty + end + + it 'does not invoke any tagged callbacks' do + subject.before_foo(:blue) { invocations << :blue_callback } + subject.invoke_hook(:before_foo) + invocations.should be_empty + end + + it 'passes along the provided arguments to the callback' do + subject.before_foo(&lambda { |a, b| invocations << [a, b] }) + subject.invoke_hook(:before_foo, :arg1, :arg2) + invocations.flatten.should eq([:arg1, :arg2]) + end + + it 'only passes along 1 argument when the block accepts only 1 arguments' do + subject.before_foo(&lambda { |a| invocations << a }) + subject.invoke_hook(:before_foo, :arg1, :arg2) + invocations.flatten.should eq([:arg1]) + end + + it 'passes along all arguments when the block accepts a variable number of args' do + subject.before_foo(&lambda { |*a| invocations << a }) + subject.invoke_hook(:before_foo, :arg1, :arg2) + invocations.flatten.should eq([:arg1, :arg2]) + end + end + + describe "#invoke_tagged_hook(:before_foo, tag)" do + it 'invokes each of the :before_foo callbacks with a matching tag' do + subject.before_foo(:green) { invocations << :callback_1 } + subject.before_foo(:green) { invocations << :callback_2 } + + invocations.should be_empty + subject.invoke_tagged_hook(:before_foo, :green) + invocations.should eq([:callback_1, :callback_2]) + end + + it 'invokes each of the :before_foo callbacks with no tag' do + subject.before_foo { invocations << :no_tag_1 } + subject.before_foo { invocations << :no_tag_2 } + + subject.invoke_hook(:before_foo, :green) + invocations.should eq([:no_tag_1, :no_tag_2]) + end + + it 'does not invoke any callbacks with a different tag' do + subject.before_foo(:blue) { invocations << :blue_callback } + subject.invoke_tagged_hook(:before_foo, :green) + invocations.should be_empty end - context 'without a tag argument' do - it 'invokes each of the :before_foo callbacks' do - subject.before_foo { invocations << :callback_1 } - subject.before_foo { invocations << :callback_2 } - - invocations.should be_empty - subject.invoke_hook(:before_foo, nil) - invocations.should eq([:callback_1, :callback_2]) - end - - it 'does not invoke :before_bar callbacks' do - subject.before_bar { invocations << :bar_callback } - subject.invoke_hook(:before_foo, nil) - invocations.should be_empty - end - - it 'does not invoke any tagged callbacks' do - subject.before_foo(:blue) { invocations << :blue_callback } - subject.invoke_hook(:before_foo, nil) - invocations.should be_empty - end - - it 'passes along the provided arguments to the callback' do - subject.before_foo(&lambda { |a, b| invocations << [a, b] }) - subject.invoke_hook(:before_foo, nil, :arg1, :arg2) - invocations.flatten.should eq([:arg1, :arg2]) - end - - it 'only passes along 1 argument when the block accepts only 1 arguments' do - subject.before_foo(&lambda { |a| invocations << a }) - subject.invoke_hook(:before_foo, nil, :arg1, :arg2) - invocations.flatten.should eq([:arg1]) - end - - it 'passes along all arguments when the block accepts a variable number of args' do - subject.before_foo(&lambda { |*a| invocations << a }) - subject.invoke_hook(:before_foo, nil, :arg1, :arg2) - invocations.flatten.should eq([:arg1, :arg2]) - end + it 'passes along the provided arguments to the callback' do + subject.before_foo(:green, &lambda { |a, b| invocations << [a, b] }) + subject.invoke_tagged_hook(:before_foo, :green, :arg1, :arg2) + invocations.flatten.should eq([:arg1, :arg2]) end end end