Skip to content

Commit

Permalink
Add hook API that doesn't require passing a tag.
Browse files Browse the repository at this point in the history
Most of our hook invocations don't use tags now.
  • Loading branch information
myronmarston committed Nov 21, 2011
1 parent 2b55126 commit 4ca902c
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 77 deletions.
2 changes: 1 addition & 1 deletion lib/vcr/cassette.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/vcr/library_hooks/typhoeus.rb
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/vcr/library_hooks/webmock.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/vcr/request_handler.rb
Expand Up @@ -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?
Expand Down
3 changes: 1 addition & 2 deletions lib/vcr/request_ignorer.rb
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/vcr/util/hooks.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/vcr/cassette_spec.rb
Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions spec/vcr/configuration_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
130 changes: 67 additions & 63 deletions spec/vcr/util/hooks_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 4ca902c

Please sign in to comment.