class Faraday::Adapter::Test::Stubs

A stack of Stubs

def delete(path, headers = {}, &block)

def delete(path, headers = {}, &block)
  new_stub(:delete, path, headers, &block)
end

def empty?

def empty?
  @stack.empty?
end

def get(path, headers = {}, &block)

def get(path, headers = {}, &block)
  new_stub(:get, path, headers, &block)
end

def head(path, headers = {}, &block)

def head(path, headers = {}, &block)
  new_stub(:head, path, headers, &block)
end

def initialize

def initialize
  # { get: [Stub, Stub] }
  @stack = {}
  @consumed = {}
  yield(self) if block_given?
end

def match(request_method, host, path, headers, body)

def match(request_method, host, path, headers, body)
  return false unless @stack.key?(request_method)
  stack = @stack[request_method]
  consumed = (@consumed[request_method] ||= [])
  stub, meta = matches?(stack, host, path, headers, body)
  if stub
    consumed << stack.delete(stub)
    return stub, meta
  end
  matches?(consumed, host, path, headers, body)
end

def matches?(stack, host, path, headers, body)

def matches?(stack, host, path, headers, body)
  stack.each do |stub|
    match_result, meta = stub.matches?(host, path, headers, body)
    return stub, meta if match_result
  end
  nil
end

def new_stub(request_method, path, headers = {}, body = nil, &block)

def new_stub(request_method, path, headers = {}, body = nil, &block)
  normalized_path, host =
    if path.is_a?(Regexp)
      path
    else
      [
        Faraday::Utils.normalize_path(path),
        Faraday::Utils.URI(path).host
      ]
    end
  stub = Stub.new(host, normalized_path, headers, body, block)
  (@stack[request_method] ||= []) << stub
end

def options(path, headers = {}, &block)

def options(path, headers = {}, &block)
  new_stub(:options, path, headers, &block)
end

def patch(path, body = nil, headers = {}, &block)

def patch(path, body = nil, headers = {}, &block)
  new_stub(:patch, path, headers, body, &block)
end

def post(path, body = nil, headers = {}, &block)

def post(path, body = nil, headers = {}, &block)
  new_stub(:post, path, headers, body, &block)
end

def put(path, body = nil, headers = {}, &block)

def put(path, body = nil, headers = {}, &block)
  new_stub(:put, path, headers, body, &block)
end

def verify_stubbed_calls

Raises an error if any of the stubbed calls have not been made.
def verify_stubbed_calls
  failed_stubs = []
  @stack.each do |method, stubs|
    next if stubs.empty?
    failed_stubs.concat(
      stubs.map do |stub|
        "Expected #{method} #{stub}."
      end
    )
  end
  raise failed_stubs.join(' ') unless failed_stubs.empty?
end