class Faraday::Adapter::Test::Stubs

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/faraday/adapter/test.rb, line 46
def initialize
  # {:get => [Stub, Stub]}
  @stack, @consumed = {}, {}
  yield(self) if block_given?
end

Public Instance Methods

delete(path, headers = {}, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 89
def delete(path, headers = {}, &block)
  new_stub(:delete, path, headers, &block)
end
empty?() click to toggle source
# File lib/faraday/adapter/test.rb, line 52
def empty?
  @stack.empty?
end
get(path, headers = {}, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 69
def get(path, headers = {}, &block)
  new_stub(:get, path, headers, &block)
end
head(path, headers = {}, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 73
def head(path, headers = {}, &block)
  new_stub(:head, path, headers, &block)
end
match(request_method, path, headers, body) click to toggle source
# File lib/faraday/adapter/test.rb, line 56
def match(request_method, path, headers, body)
  return false if !@stack.key?(request_method)
  stack = @stack[request_method]
  consumed = (@consumed[request_method] ||= [])

  stub, meta = matches?(stack, path, headers, body)
  if stub
    consumed << stack.delete(stub)
    return stub, meta
  end
  matches?(consumed, path, headers, body)
end
options(path, headers = {}, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 93
def options(path, headers = {}, &block)
  new_stub(:options, path, headers, &block)
end
patch(path, body=nil, headers = {}, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 85
def patch(path, body=nil, headers = {}, &block)
  new_stub(:patch, path, headers, body, &block)
end
post(path, body=nil, headers = {}, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 77
def post(path, body=nil, headers = {}, &block)
  new_stub(:post, path, headers, body, &block)
end
put(path, body=nil, headers = {}, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 81
def put(path, body=nil, headers = {}, &block)
  new_stub(:put, path, headers, body, &block)
end
verify_stubbed_calls() click to toggle source

Raises an error if any of the stubbed calls have not been made.

# File lib/faraday/adapter/test.rb, line 98
def verify_stubbed_calls
  failed_stubs = []
  @stack.each do |method, stubs|
    unless stubs.size == 0
      failed_stubs.concat(stubs.map {|stub|
        "Expected #{method} #{stub}."
      })
    end
  end
  raise failed_stubs.join(" ") unless failed_stubs.size == 0
end

Protected Instance Methods

matches?(stack, path, headers, body) click to toggle source
# File lib/faraday/adapter/test.rb, line 117
def matches?(stack, path, headers, body)
  stack.each do |stub|
    match_result, meta = stub.matches?(path, headers, body)
    return stub, meta if match_result
  end
  nil
end
new_stub(request_method, path, headers = {}, body=nil, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 112
def new_stub(request_method, path, headers = {}, body=nil, &block)
  normalized_path = path.is_a?(Regexp) ? path : Faraday::Utils.normalize_path(path)
  (@stack[request_method] ||= []) << Stub.new(normalized_path, headers, body, block)
end