Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Typhoeus::Easy support but without callbacks yet

  • Loading branch information...
commit b6e212606e6307452137dbdfce63464e1b79812d 1 parent 764ad72
@bblimke authored
View
2  lib/webmock.rb
@@ -11,6 +11,8 @@
require 'webmock/http_lib_adapters/curb'
require 'webmock/http_lib_adapters/em_http_request'
require 'webmock/http_lib_adapters/typhoeus_hydra'
+require 'webmock/http_lib_adapters/typhoeus_easy'
+
require 'webmock/errors'
View
98 lib/webmock/http_lib_adapters/typhoeus_easy.rb
@@ -0,0 +1,98 @@
+if defined?(Typhoeus)
+ module Typhoeus
+ class Easy
+ def build_request_signature
+ uri = WebMock::Util::URI.heuristic_parse(self.url)
+ uri.path = uri.normalized_path.gsub("[^:]//","/")
+ if @webmock_auth
+ uri.user = @webmock_auth[:username]
+ uri.password = @webmock_auth[:password]
+ end
+
+ request_signature = WebMock::RequestSignature.new(
+ @method,
+ uri.to_s,
+ :body => @request_body,
+ :headers => @headers
+ )
+ request_signature
+ end
+
+ def build_webmock_response
+ response = Typhoeus::Response.new(:headers => self.response_header)
+ webmock_response = WebMock::Response.new
+ webmock_response.status = [self.response_code, response.status_message]
+ webmock_response.body = self.response_body
+ webmock_response.headers = WebMock::Util::Headers.normalize_headers(response.headers_hash)
+ webmock_response
+ end
+
+ def build_easy_response(webmock_response)
+ raise Curl::Err::TimeoutError if webmock_response.should_timeout
+ webmock_response.raise_error_if_any
+ @response_body = webmock_response.body
+ @webmock_response_code = webmock_response.status[0]
+
+ @response_header = "HTTP/1.1 #{webmock_response.status[0]} #{webmock_response.status[1]}\r\n"
+ if webmock_response.headers
+ @response_header << webmock_response.headers.map do |k,v|
+ "#{k}: #{v.is_a?(Array) ? v.join(", ") : v}"
+ end.join("\r\n")
+ end
+ end
+
+ def auth_with_webmock=(auth)
+ @webmock_auth = auth
+ auth_without_webmock=(auth)
+ end
+ alias :auth_without_webmock= :auth=
+ alias :auth= :auth_with_webmock=
+
+ def response_code_with_webmock
+ if @webmock_response_code
+ @webmock_response_code
+ else
+ response_code_without_webmock
+ end
+ end
+ alias :response_code_without_webmock :response_code
+ alias :response_code :response_code_with_webmock
+
+ def reset_with_webmock
+ @webmock_auth = nil
+ reset_without_webmock
+ end
+ alias :reset_without_webmock :reset
+ alias :reset :reset_with_webmock
+
+ def perform_with_webmock
+ @webmock_response_code = nil
+ request_signature = build_request_signature
+ WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
+ if WebMock::StubRegistry.instance.registered_request?(request_signature)
+ webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
+ build_easy_response(webmock_response)
+ WebMock::CallbackRegistry.invoke_callbacks(
+ {:lib => :typhoeus}, request_signature, webmock_response)
+ # invoke_curb_callbacks
+ true
+ elsif WebMock.net_connect_allowed?(request_signature.uri)
+ res = perform_without_webmock
+ if WebMock::CallbackRegistry.any_callbacks?
+ webmock_response = build_webmock_response
+ WebMock::CallbackRegistry.invoke_callbacks(
+ {:lib => :typhoeus, :real_request => true}, request_signature,
+ webmock_response)
+ end
+ res
+ else
+ raise WebMock::NetConnectNotAllowedError.new(request_signature)
+ end
+ end
+ alias :perform_without_webmock :perform
+ alias :perform :perform_with_webmock
+ end
+
+
+ end
+end
View
2  spec/curb_spec_helper.rb
@@ -1,4 +1,4 @@
-module CurbSpecHelper
+ module CurbSpecHelper
def http_request(method, uri, options = {}, &block)
uri = Addressable::URI.heuristic_parse(uri)
body = options[:body]
View
12 spec/spec_helper.rb
@@ -47,3 +47,15 @@ def setup_expectations_for_real_example_com_request(options = {})
setup_expectations_for_real_request(defaults.merge(options))
end
+
+def join_array_values(hash)
+ joined = {}
+ if hash
+ hash.each do |k,v|
+ v = v.join(", ") if v.is_a?(Array)
+ joined[k] = v
+ end
+ end
+ joined
+ end
+
View
13 spec/typhoeus_easy_spec.rb
@@ -0,0 +1,13 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require 'webmock_shared'
+
+unless RUBY_PLATFORM =~ /java/
+ require 'typhoeus_easy_spec_helper'
+
+ describe "Webmock with Typhoeus::Easy" do
+ include TyphoeusEasySpecHelper
+
+ it_should_behave_like "WebMock"
+
+ end
+end
View
47 spec/typhoeus_easy_spec_helper.rb
@@ -0,0 +1,47 @@
+require 'ostruct'
+
+module TyphoeusEasySpecHelper
+ class FakeTyphoeusEasyError < StandardError; end
+
+ def http_request(method, uri, options = {}, &block)
+ uri = Addressable::URI.heuristic_parse(uri)
+
+ easy = Typhoeus::Easy.new
+ easy.method = method
+ easy.url = uri.omit(:userinfo).to_s
+ easy.auth = { :username => uri.user, :password => uri.password } if uri.user
+ easy.request_body = options[:body] if options[:body]
+ easy.headers = options[:headers] if options[:headers]
+ easy.timeout = 2000
+ easy.connect_timeout = 2000
+
+ easy.perform
+
+ # status, response_headers = Curl::Easy::WebmockHelper.parse_header_string(curl.header_str)
+ raise FakeTyphoeusEasyError.new if easy.response_code.to_s == "0"
+ response = Typhoeus::Response.new(:headers => easy.response_header)
+ OpenStruct.new(
+ :body => easy.response_body,
+ :headers => WebMock::Util::Headers.normalize_headers(join_array_values(response.headers_hash)),
+ :status => easy.response_code.to_s,
+ :message => response.status_message
+ )
+ end
+
+ def client_timeout_exception_class
+ Curl::Err::TimeoutError
+ end
+
+ def connection_refused_exception_class
+ FakeTyphoeusEasyError
+ end
+
+ def setup_expectations_for_real_request(options = {})
+ #TODO
+ end
+
+ def http_library
+ :typhoeus
+ end
+
+end
View
21 spec/typhoeus_hydra_spec_helper.rb
@@ -1,7 +1,7 @@
require 'ostruct'
module TyphoeusHydraSpecHelper
- class FakeTyphoeusError < StandardError; end
+ class FakeTyphoeusHydraError < StandardError; end
def http_request(method, uri, options = {}, &block)
@@ -14,7 +14,7 @@ def http_request(method, uri, options = {}, &block)
:timeout => 2000 # milliseconds
}
)
- raise FakeTyphoeusError.new if response.code.to_s == "0"
+ raise FakeTyphoeusHydraError.new if response.code.to_s == "0"
OpenStruct.new({
:body => response.body,
:headers => WebMock::Util::Headers.normalize_headers(join_array_values(response.headers_hash)),
@@ -24,11 +24,11 @@ def http_request(method, uri, options = {}, &block)
end
def client_timeout_exception_class
- FakeTyphoeusError
+ FakeTyphoeusHydraError
end
def connection_refused_exception_class
- FakeTyphoeusError
+ FakeTyphoeusHydraError
end
def setup_expectations_for_real_request(options = {})
@@ -39,17 +39,4 @@ def http_library
:typhoeus
end
- private
-
- def join_array_values(headers)
- joined = {}
- if headers
- headers.each do |k,v|
- v = v.join(", ") if v.is_a?(Array)
- joined[k] = v
- end
- end
- joined
- end
-
end
View
6 spec/webmock_shared.rb
@@ -1041,10 +1041,10 @@ def call(request)
it "should fail if request was executed with different body" do
lambda {
- http_request(:get, "http://www.example.com/", :body => /^abc/)
+ http_request(:get, "http://www.example.com/", :body => "abc")
a_request(:get, "www.example.com").
- with(:body => "xabc").should have_been_made
- }.should fail_with(%r(The request GET http://www.example.com/ with body "xabc" was expected to execute 1 time but it executed 0 times))
+ with(:body => /^xabc/).should have_been_made
+ }.should fail_with(%r(The request GET http://www.example.com/ with body /\^xabc/ was expected to execute 1 time but it executed 0 times))
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.