Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Work around the fact that the on_complete hook can fire twice for one…

  • Loading branch information...
commit 7c6b615637c4ad1a79959dcd9becab70fc86d4ff 1 parent f75353b
@myronmarston authored
View
4 lib/vcr/library_hooks/typhoeus.rb
@@ -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)
@@ -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
View
37 spec/vcr/library_hooks/typhoeus_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.