class WebMock::HttpLibAdapters::ManticoreAdapter::WebMockManticoreClient

Public Instance Methods

request(klass, url, options={}, &block) click to toggle source
Calls superclass method
# File lib/webmock/http_lib_adapters/manticore_adapter.rb, line 28
def request(klass, url, options={}, &block)
  super(klass, WebMock::Util::URI.normalize_uri(url).to_s, format_options(options))
end

Private Instance Methods

format_options(options) click to toggle source
# File lib/webmock/http_lib_adapters/manticore_adapter.rb, line 34
def format_options(options)
  return options unless headers = options[:headers]

  options.merge(headers: join_array_values(headers))
end
generate_manticore_response(webmock_response) click to toggle source
# File lib/webmock/http_lib_adapters/manticore_adapter.rb, line 105
def generate_manticore_response(webmock_response)
  raise Manticore::ConnectTimeout if webmock_response.should_timeout

  Manticore::StubbedResponse.stub(
    code: webmock_response.status[0],
    body: webmock_response.body,
    headers: webmock_response.headers,
    cookies: {}
  )
end
generate_webmock_request_signature(request, context) click to toggle source
# File lib/webmock/http_lib_adapters/manticore_adapter.rb, line 77
def generate_webmock_request_signature(request, context)
  method = request.method.downcase
  uri = request.uri.to_s
  body = read_body(request)
  headers = split_array_values(request.headers)

  if context.get_credentials_provider && credentials = context.get_credentials_provider.get_credentials(AuthScope::ANY)
    headers['Authorization'] = WebMock::Util::Headers.basic_auth_header(credentials.get_user_name,credentials.get_password)
  end

  WebMock::RequestSignature.new(method, uri, {body: body, headers: headers})
end
generate_webmock_response(manticore_response) click to toggle source
# File lib/webmock/http_lib_adapters/manticore_adapter.rb, line 116
def generate_webmock_response(manticore_response)
  webmock_response = WebMock::Response.new
  webmock_response.status = [manticore_response.code, manticore_response.message]
  webmock_response.body = manticore_response.body
  webmock_response.headers = manticore_response.headers
  webmock_response
end
join_array_values(headers) click to toggle source
# File lib/webmock/http_lib_adapters/manticore_adapter.rb, line 40
def join_array_values(headers)
  headers.reduce({}) do |h, (k,v)|
    v = v.join(', ') if v.is_a?(Array)
    h.merge(k => v)
  end
end
read_body(request) click to toggle source
# File lib/webmock/http_lib_adapters/manticore_adapter.rb, line 90
def read_body(request)
  if request.respond_to?(:entity) && !request.entity.nil?
    Manticore::EntityConverter.new.read_entity(request.entity)
  end
end
real_request_allowed?(uri) click to toggle source
# File lib/webmock/http_lib_adapters/manticore_adapter.rb, line 73
def real_request_allowed?(uri)
  WebMock.net_connect_allowed?(uri)
end
registered_response_for(request_signature) click to toggle source
# File lib/webmock/http_lib_adapters/manticore_adapter.rb, line 69
def registered_response_for(request_signature)
  WebMock::StubRegistry.instance.response_for_request(request_signature)
end
response_object_for(request, context, &block) click to toggle source
# File lib/webmock/http_lib_adapters/manticore_adapter.rb, line 47
def response_object_for(request, context, &block)
  request_signature = generate_webmock_request_signature(request, context)
  WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)

  if webmock_response = registered_response_for(request_signature)
    webmock_response.raise_error_if_any
    manticore_response = generate_manticore_response(webmock_response).call
    real_request = false

  elsif real_request_allowed?(request_signature.uri)
    manticore_response = Manticore::Response.new(self, request, context, &block).call
    webmock_response = generate_webmock_response(manticore_response)
    real_request = true

  else
    raise WebMock::NetConnectNotAllowedError.new(request_signature)
  end

  WebMock::CallbackRegistry.invoke_callbacks({lib: :manticore, real_request: real_request}, request_signature, webmock_response)
  manticore_response
end
split_array_values(headers = []) click to toggle source
# File lib/webmock/http_lib_adapters/manticore_adapter.rb, line 96
def split_array_values(headers = [])
  headers.each_with_object({}) do |(k, v), h|
    h[k] = case v
           when /,/ then v.split(',').map(&:strip)
           else v
           end
  end
end