forked from bblimke/webmock
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
begin | ||
require 'excon' | ||
rescue LoadError | ||
# excon not found | ||
end | ||
|
||
if defined?(Excon) | ||
|
||
module WebMock | ||
module HttpLibAdapters | ||
|
||
class ExconAdapter < HttpLibAdapter | ||
adapter_for :excon | ||
|
||
def self.enable! | ||
Excon.send(:remove_const, :Connection) | ||
Excon.send(:const_set, :Connection, ExconConnection) | ||
end | ||
|
||
def self.disable! | ||
Excon.send(:remove_const, :Connection) | ||
Excon.send(:const_set, :Connection, ExconConnection.superclass) | ||
end | ||
|
||
def self.build_request(params) | ||
uri = Addressable::URI.new(params).to_s | ||
method = (params[:method] || :get).to_s.downcase.to_sym | ||
WebMock::RequestSignature.new method, uri, :body => params[:body], :headers => params[:headers] | ||
end | ||
|
||
def self.real_response(mock) | ||
raise Excon::Errors::Timeout if mock.should_timeout | ||
mock.raise_error_if_any | ||
Excon::Response.new \ | ||
:body => mock.body, | ||
:status => mock.status[0].to_i, | ||
:headers => mock.headers | ||
end | ||
|
||
def self.mock_response(real) | ||
mock = WebMock::Response.new | ||
mock.status = real.status | ||
mock.headers = real.headers | ||
mock.body = real.body | ||
mock | ||
end | ||
|
||
def self.perform_callbacks(request, response, options = {}) | ||
return unless WebMock::CallbackRegistry.any_callbacks? | ||
WebMock::CallbackRegistry.invoke_callbacks(options.merge(:lib => :excon), request, response) | ||
end | ||
|
||
end | ||
|
||
class ExconConnection < ::Excon::Connection | ||
|
||
def request_kernel(params, &block) | ||
mock_request = ExconAdapter.build_request params.dup | ||
WebMock::RequestRegistry.instance.requested_signatures.put(mock_request) | ||
|
||
if mock_response = WebMock::StubRegistry.instance.response_for_request(mock_request) | ||
ExconAdapter.perform_callbacks(mock_request, mock_response, :real_request => false) | ||
ExconAdapter.real_response(mock_response) | ||
elsif WebMock.net_connect_allowed?(mock_request.uri) | ||
real_response = super | ||
ExconAdapter.perform_callbacks(mock_request, ExconAdapter.mock_response(real_response), :real_request => true) | ||
real_response | ||
else | ||
raise WebMock::NetConnectNotAllowedError.new(mock_request) | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'spec_helper' | ||
require 'acceptance/webmock_shared' | ||
require 'acceptance/excon/excon_spec_helper' | ||
|
||
describe "Excon" do | ||
include ExconSpecHelper | ||
include_context "with WebMock" | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'ostruct' | ||
|
||
module ExconSpecHelper | ||
|
||
def http_request(method, uri, options = {}, &block) | ||
uri = Addressable::URI.heuristic_parse(uri) | ||
uri = uri.omit(:userinfo).to_s.gsub(' ', '+') | ||
|
||
options = options.merge(:method => method) # Dup and merge | ||
response = Excon.new(uri).request(options, &block) | ||
|
||
headers = WebMock::Util::Headers.normalize_headers(response.headers) | ||
headers = headers.inject({}) do |res, (name, value)| | ||
res[name] = value.is_a?(Array) ? value.flatten.join(', ') : value | ||
res | ||
end | ||
|
||
OpenStruct.new \ | ||
:body => response.body, | ||
:headers => headers, | ||
:status => response.status.to_s, | ||
:message => "" | ||
end | ||
|
||
def client_timeout_exception_class | ||
Excon::Errors::Timeout | ||
end | ||
|
||
def connection_refused_exception_class | ||
Excon::Errors::SocketError | ||
end | ||
|
||
def http_library | ||
:excon | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters