Skip to content

Commit

Permalink
Change hooks so they yield individual HTTPInteraction instances rathe…
Browse files Browse the repository at this point in the history
…r than an array.
  • Loading branch information
myronmarston committed Jan 12, 2011
1 parent 9e0665a commit d5e403d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
32 changes: 15 additions & 17 deletions features/configuration/hooks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Feature: Hooks

To use these, call `config.before_record` or `config.before_playback` in
your `VCR.config` block. Provide a block that accepts 0, 1 or 2 arguments.
The first argument, if the block accepts it, will be an array of HTTP
interactions. You can modify any of them and/or remove them from the array
to prevent them from being recorded or played back. The second argument,
if the block accepts it, will be the `VCR::Cassette` instance. This may be
useful for hooks that you want to behave differently for different cassettes.
The first argument, if the block accepts it, will be an HTTP interaction.
Changes you make to the interaction will be reflected in the recording or
playback. The second argument, if the block accepts it, will be the
`VCR::Cassette` instance. This may be useful for hooks that you want to
behave differently for different cassettes.

You can also use tagging to apply hooks to particular cassettes. Consider
this code:
Expand Down Expand Up @@ -68,10 +68,8 @@ Feature: Hooks
c.stub_with :fakeweb
c.cassette_library_dir = 'cassettes'
c.before_record do |interactions|
interactions.each do |i|
i.response.body.sub!(/^Hello .*$/, 'Hello World')
end
c.before_record do |i|
i.response.body.sub!(/^Hello .*$/, 'Hello World')
end
end
Expand All @@ -93,10 +91,8 @@ Feature: Hooks
c.stub_with :fakeweb
c.cassette_library_dir = 'cassettes'
c.before_playback do |interactions|
interactions.each do |i|
i.response.body = 'response from before_playback'
end
c.before_playback do |i|
i.response.body = 'response from before_playback'
end
end
Expand Down Expand Up @@ -164,8 +160,8 @@ Feature: Hooks
c.stub_with :fakeweb
c.cassette_library_dir = 'cassettes'
c.before_record(:tag_1) { puts "In before_record hook for tag_1" }
c.before_playback(:tag_2) { puts "In before_playback hook for tag_2" }
c.before_record(:tag_1) { |i| puts "In before_record hook for tag_1 (#{i.response.body})" }
c.before_playback(:tag_2) { |i| puts "In before_playback hook for tag_2 (#{i.response.body})" }
end
[:tag_1, :tag_2, nil].each do |tag|
Expand All @@ -187,10 +183,12 @@ Feature: Hooks
Using tag: :tag_1
Response 1: Hello World
Response 2: example.com response
In before_record hook for tag_1
In before_record hook for tag_1 (example.com response)
In before_record hook for tag_1 (Hello World)
Using tag: :tag_2
In before_playback hook for tag_2
In before_playback hook for tag_2 (example.com response)
In before_playback hook for tag_2 (Hello World)
Response 1: Hello World
Response 2: example.com response
Expand Down
4 changes: 3 additions & 1 deletion lib/vcr/cassette.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ def write_recorded_interactions_to_disk
end

def invoke_hook(type, interactions)
VCR::Config.invoke_hook(type, tag, interactions, self)
interactions.each do |i|
VCR::Config.invoke_hook(type, tag, i, self)
end
end
end
end
33 changes: 19 additions & 14 deletions spec/vcr/cassette_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,15 @@

if stub_requests
it 'invokes the appropriately tagged before_playback hooks' do
args = nil
VCR::Config.should_receive(:invoke_hook) { |*a| args = a }
cassette = VCR::Cassette.new('example', :record => record_mode, :tag => :foo)

args.should == [
VCR::Config.should_receive(:invoke_hook).with(
:before_playback,
:foo,
cassette.recorded_interactions,
cassette
]
an_instance_of(VCR::HTTPInteraction),
an_instance_of(VCR::Cassette)
).exactly(3).times

cassette = VCR::Cassette.new('example', :record => record_mode, :tag => :foo)
cassette.should have(3).recorded_interactions
end

it "stubs the recorded requests with the http stubbing adapter" do
Expand Down Expand Up @@ -278,15 +277,21 @@
end

it 'invokes the appropriately tagged before_record hooks' do
interactions = [VCR::HTTPInteraction.new(:req_sig_1, :response_1)]
interactions = [
VCR::HTTPInteraction.new(:req_sig_1, :response_1),
VCR::HTTPInteraction.new(:req_sig_2, :response_2)
]

cassette = VCR::Cassette.new('example', :tag => :foo)
cassette.stub!(:new_recorded_interactions).and_return(interactions)

VCR::Config.should_receive(:invoke_hook) do |hook_type, tag, _interactions, _cassette|
hook_type.should == :before_record
tag.should == :foo
_interactions.should == interactions
_cassette.should == cassette
interactions.each do |i|
VCR::Config.should_receive(:invoke_hook).with(
:before_record,
:foo,
i,
cassette
).ordered
end

cassette.eject
Expand Down

0 comments on commit d5e403d

Please sign in to comment.