Permalink
Browse files

Update typhoeus code to work with Typheous 0.5.

  • Loading branch information...
1 parent 7d59ed7 commit 11e310e5eaea9053c68ea1f3a8ecb23b92412f4f @i0rek i0rek committed with Sep 12, 2012
View
@@ -1,4 +1,9 @@
source :rubygems
+
+gem 'typhoeus', :git => "https://github.com/typhoeus/typhoeus.git"
+gem 'webmock', :git => "https://github.com/i0rek/webmock.git", :branch => "typhoeus_0.5.0"
+gem 'faraday', :git => "https://github.com/i0rek/faraday.git", :branch => "typhoeus_0.5.0"
+
gemspec
group :development do
@@ -13,20 +13,21 @@ class RequestHandler < ::VCR::RequestHandler
attr_reader :request
def initialize(request)
@request = request
+ request.block_connection = false if VCR.turned_on?
end
def vcr_request
@vcr_request ||= VCR::Request.new \
- request.method,
+ request.options.fetch(:method, :get),
request.url,
- request.body,
- request.headers
+ request.options.fetch(:body, ""),
+ request.options.fetch(:headers, {})
end
private
def externally_stubbed?
- ::Typhoeus::Hydra.stubs.detect { |stub| stub.matches?(request) }
+ ::Typhoeus::Expectation.find_by(request)
end
def set_typed_request_for_after_hook(*args)
@@ -44,8 +45,9 @@ def on_stubbed_by_vcr_request
:http_version => stubbed_response.http_version,
:code => stubbed_response.status.code,
:status_message => stubbed_response.status.message,
- :headers_hash => stubbed_response_headers,
- :body => stubbed_response.body
+ :headers => stubbed_response_headers,
+ :body => stubbed_response.body,
+ :mock => true
end
def stubbed_response_headers
@@ -61,17 +63,18 @@ def stubbed_response_headers
def self.vcr_response_from(response)
VCR::Response.new \
VCR::ResponseStatus.new(response.code, response.status_message),
- response.headers_hash,
+ response.headers,
response.body,
response.http_version
end
- ::Typhoeus::Hydra.after_request_before_on_complete do |request|
+ ::Typhoeus.on_complete do |response|
+ request = response.request
unless VCR.library_hooks.disabled?(:typhoeus)
- vcr_response = vcr_response_from(request.response)
+ vcr_response = vcr_response_from(response)
typed_vcr_request = request.send(:remove_instance_variable, :@__typed_vcr_request)
- unless request.response.mock?
+ unless request.response.mock
http_interaction = VCR::HTTPInteraction.new(typed_vcr_request, vcr_response)
VCR.record_http_interaction(http_interaction)
end
@@ -80,27 +83,17 @@ def self.vcr_response_from(response)
end
end
- ::Typhoeus::Hydra.register_stub_finder do |request|
- VCR::LibraryHooks::Typhoeus::RequestHandler.new(request).handle
+ ::Typhoeus.before do |request|
+ if response = VCR::LibraryHooks::Typhoeus::RequestHandler.new(request).handle
+ request.finish(response)
+ else
+ true
+ end
end
end
end
end
-# @private
-module Typhoeus
- class << Hydra
- # ensure HTTP requests are always allowed; VCR takes care of disallowing
- # them at the appropriate times in its hook
- def allow_net_connect_with_vcr?(*args)
- VCR.turned_on? ? true : allow_net_connect_without_vcr?
- end
-
- alias allow_net_connect_without_vcr? allow_net_connect?
- alias allow_net_connect? allow_net_connect_with_vcr?
- end unless Hydra.respond_to?(:allow_net_connect_with_vcr?)
-end
-
VCR.configuration.after_library_hooks_loaded do
# ensure WebMock's Typhoeus adapter does not conflict with us here
# (i.e. to double record requests or whatever).
@@ -28,10 +28,12 @@ def enable!(scope)
::WebMock::CallbackRegistry.add_callback(cb[:options], cb[:block])
end
when :typhoeus
- ::Typhoeus::Hydra.global_hooks = $original_typhoeus_global_hooks
- ::Typhoeus::Hydra.stub_finders.clear
- $original_typhoeus_stub_finders.each do |finder|
- ::Typhoeus::Hydra.stub_finders << finder
+ $original_typhoeus_global_hooks.each do |hook|
+ ::Typhoeus.on_complete << hook
+ end
+ ::Typhoeus.before.clear
+ $original_typhoeus_before_hooks.each do |hook|
+ ::Typhoeus.before << hook
end
when :excon
$original_excon_stubs.each do |stub|
@@ -56,8 +58,8 @@ def disable_all!
end
if defined?(::Typhoeus)
- ::Typhoeus::Hydra.clear_global_hooks
- ::Typhoeus::Hydra.stub_finders.clear
+ ::Typhoeus.on_complete.clear
+ ::Typhoeus.before.clear
end
if defined?(::Excon)
@@ -130,13 +132,13 @@ def realias_net_http(alias_extension)
require 'patron'
require 'em-http-request'
require 'curb'
-
- require 'vcr/library_hooks/typhoeus'
- $typhoeus_after_loaded_hook = VCR.configuration.hooks[:after_library_hooks_loaded].last
- $original_typhoeus_global_hooks = Typhoeus::Hydra.global_hooks.dup
- $original_typhoeus_stub_finders = Typhoeus::Hydra.stub_finders.dup
end
+require 'vcr/library_hooks/typhoeus'
+$typhoeus_after_loaded_hook = VCR.configuration.hooks[:after_library_hooks_loaded].last
+$original_typhoeus_global_hooks = Typhoeus.on_complete.dup
+$original_typhoeus_before_hooks = Typhoeus.before.dup
+
require 'vcr/library_hooks/fakeweb'
$fakeweb_after_loaded_hook = VCR.configuration.hooks[:after_library_hooks_loaded].last
@@ -160,15 +160,17 @@ def get_body_string(response)
alias get_body_object get_body_string
def get_header(header_key, response)
- response.headers_hash[header_key]
+ response.headers[header_key]
end
def make_http_request(method, url, body = nil, headers = {})
- Typhoeus::Request.send(method, url, :body => body, :headers => headers)
+ request = Typhoeus::Request.new(url, :method => method, :body => body, :headers => headers)
+ request.run
+ request.response
end
def normalize_request_headers(headers)
- headers
+ headers.merge("User-Agent"=>["Typhoeus - https://github.com/typhoeus/typhoeus"])
end
end
@@ -2,21 +2,21 @@
describe "Typhoeus hook", :with_monkey_patches => :typhoeus do
after(:each) do
- ::Typhoeus::Hydra.clear_stubs
+ ::Typhoeus::Expectation.clear
end
def disable_real_connections
- ::Typhoeus::Hydra.allow_net_connect = false
- ::Typhoeus::Hydra::NetConnectNotAllowedError
+ ::Typhoeus::Config.block_connection = true
+ ::Typhoeus::Errors::NoStub
end
def enable_real_connections
- ::Typhoeus::Hydra.allow_net_connect = true
+ ::Typhoeus::Config.block_connection = false
end
def directly_stub_request(method, url, response_body)
response = ::Typhoeus::Response.new(:code => 200, :body => response_body)
- ::Typhoeus::Hydra.stub(method, url).and_return(response)
+ ::Typhoeus.stub(url, :method => method).and_return(response)
end
it_behaves_like 'a hook into an HTTP library', :typhoeus, 'typhoeus'
@@ -27,5 +27,5 @@ def directly_stub_request(method, url, response_body)
$typhoeus_after_loaded_hook.conditionally_invoke
end
end
-end unless RUBY_PLATFORM == 'java'
+end

0 comments on commit 11e310e

Please sign in to comment.