Module: HTTPAdapter::MockAdapter

Defined in:
lib/httpadapter/adapters/mock.rb

Overview

A simple module for mocking the transmit method on an adapter.

Examples:

# Using RSpec, verify that the request being sent includes a user agent.
adapter = HTTPAdapter::MockAdapter.create do |request_ary, connection|
  method, uri, headers, body = request_ary
  headers.should be_any { |k, v| k.downcase == 'user-agent' }
end

Class Method Summary collapse

Class Method Details

.create(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/httpadapter/adapters/mock.rb', line 29

def self.create(&block)
  adapter = Class.new do
    include HTTPAdapter

    define_method('fetch_resource') do |*params|
      response = block.call(*params)
      if response.respond_to?(:each)
        return response
      else
        return [200, [], ['']]
      end
    end
  end
  return adapter.new
end