Skip to content

Commit

Permalink
Work around the fact that the on_complete hook can fire twice for one…
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed Nov 20, 2012
1 parent f75353b commit 7c6b615
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/vcr/library_hooks/typhoeus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def self.vcr_response_from(response)
::Typhoeus.on_complete do |response|
request = response.request
unless VCR.library_hooks.disabled?(:typhoeus)
next if request.instance_variable_get(:@__vcr_on_completed)

vcr_response = vcr_response_from(response)
typed_vcr_request = request.send(:remove_instance_variable, :@__typed_vcr_request)

Expand All @@ -83,6 +85,8 @@ def self.vcr_response_from(response)
end

VCR.configuration.invoke_hook(:after_http_request, typed_vcr_request, vcr_response)

request.instance_variable_set(:@__vcr_on_completed, true)
end
end

Expand Down
37 changes: 37 additions & 0 deletions spec/vcr/library_hooks/typhoeus_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,42 @@ def directly_stub_request(method, url, response_body)
$typhoeus_after_loaded_hook.conditionally_invoke
end
end

context 'when there are nested hydra queues' do
def make_requests
VCR.use_cassette("nested") do
response_1 = response_2 = nil

hydra = Typhoeus::Hydra.new
request = Typhoeus::Request.new("http://localhost:#{VCR::SinatraApp.port}/")

request.on_success do |r1|
response_1 = r1

nested = Typhoeus::Request.new("http://localhost:#{VCR::SinatraApp.port}/foo")
nested.on_success { |r2| response_2 = r2 }

hydra.queue(nested)
end

hydra.queue(request)
hydra.run

return body_for(response_1), body_for(response_2)
end
end

def body_for(response)
return :no_response if response.nil?
response.body
end

it 'records and plays back properly' do
recorded = make_requests
played_back = make_requests

played_back.should eq(recorded)
end
end
end

0 comments on commit 7c6b615

Please sign in to comment.