class WebMock::HttpLibAdapters::ExconAdapter

Constants

PARAMS_TO_DELETE

Public Class Methods

add_excon_stub() click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 30
def self.add_excon_stub
  if not @stub
    @original_excon_mock_default = ::Excon.defaults[:mock]
    ::Excon.defaults[:mock] = true
    @stub = ::Excon.stub({}) do |params|
      self.handle_request(params)
    end
  end
end
body_from(params) click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 111
def self.body_from(params)
  body = params[:body]
  return body unless body.respond_to?(:read)

  contents = body.read
  body.rewind if body.respond_to?(:rewind)
  contents
end
build_request(params) click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 103
def self.build_request(params)
  params = params.dup
  method  = (params.delete(:method) || :get).to_s.downcase.to_sym
  params[:query] = to_query(params[:query]) if params[:query].is_a?(Hash)
  uri = Addressable::URI.new(params).to_s
  WebMock::RequestSignature.new method, uri, :body => body_from(params), :headers => params[:headers]
end
connection_params_from(hash) click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 74
def self.connection_params_from(hash)
  hash = hash.dup
  PARAMS_TO_DELETE.each { |key| hash.delete(key) }
  hash
end
disable!() click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 26
def self.disable!
  self.remove_excon_stub
end
enable!() click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 22
def self.enable!
  self.add_excon_stub
end
handle_request(params) click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 47
def self.handle_request(params)
  mock_request  = self.build_request params.dup
  WebMock::RequestRegistry.instance.requested_signatures.put(mock_request)

  if mock_response = WebMock::StubRegistry.instance.response_for_request(mock_request)
    self.perform_callbacks(mock_request, mock_response, :real_request => false)
    response = self.real_response(mock_response)
    response
  elsif WebMock.net_connect_allowed?(mock_request.uri)
    conn = new_excon_connection(params)
    real_response = conn.request(request_params_from(params.merge(:mock => false)))

    ExconAdapter.perform_callbacks(mock_request, ExconAdapter.mock_response(real_response), :real_request => true)

    real_response.data
  else
    raise WebMock::NetConnectNotAllowedError.new(mock_request)
  end
end
mock_response(real) click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 130
def self.mock_response(real)
  mock = WebMock::Response.new
  mock.status  = real.status
  mock.headers = real.headers
  mock.body    = real.body.dup
  mock
end
new_excon_connection(params) click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 67
def self.new_excon_connection(params)
  # Ensure the connection is constructed with the exact same args
  # that the orginal connection was constructed with.
  args = params.fetch(:__construction_args)
  ::Excon::Connection.new(connection_params_from args.merge(:mock => false))
end
perform_callbacks(request, response, options = {}) click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 138
def self.perform_callbacks(request, response, options = {})
  return unless WebMock::CallbackRegistry.any_callbacks?
  WebMock::CallbackRegistry.invoke_callbacks(options.merge(:lib => :excon), request, response)
end
real_response(mock) click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 120
def self.real_response(mock)
  raise Excon::Errors::Timeout if mock.should_timeout
  mock.raise_error_if_any
  {
    :body    => mock.body,
    :status  => mock.status[0].to_i,
    :headers => mock.headers || {}
  }
end
remove_excon_stub() click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 40
def self.remove_excon_stub
  ::Excon.defaults[:mock] = @original_excon_mock_default
  @original_excon_mock_default = nil
  Excon.stubs.delete(@stub)
  @stub = nil
end
request_params_from(hash) click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 80
def self.request_params_from(hash)
  hash = hash.dup
  if defined?(Excon::VALID_REQUEST_KEYS)
    hash.reject! {|key,_| !Excon::VALID_REQUEST_KEYS.include?(key) }
  end
  PARAMS_TO_DELETE.each { |key| hash.delete(key) }
  hash
end
to_query(hash) click to toggle source
# File lib/webmock/http_lib_adapters/excon_adapter.rb, line 89
def self.to_query(hash)
  string = ""
  for key, values in hash
    if values.nil?
      string << key.to_s << '&'
    else
      for value in [*values]
        string << key.to_s << '=' << CGI.escape(value.to_s) << '&'
      end
    end
  end
  string.chop! # remove trailing '&'
end