class Faraday::Adapter::Test::Stubs

Public Class Methods

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

Public Instance Methods

delete(path, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 65
def delete(path, &block)
  new_stub(:delete, path, &block)
end
empty?() click to toggle source
# File lib/faraday/adapter/test.rb, line 27
def empty?
  @stack.empty?
end
get(path, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 45
def get(path, &block)
  new_stub(:get, path, &block)
end
head(path, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 49
def head(path, &block)
  new_stub(:head, path, &block)
end
match(request_method, path, body) click to toggle source
# File lib/faraday/adapter/test.rb, line 31
def match(request_method, path, body)
  return false if !@stack.key?(request_method)
  stack = @stack[request_method]
  consumed = (@consumed[request_method] ||= [])
  path = normalize_path(path)

  if stub = matches?(stack, path, body)
    consumed << stack.delete(stub)
    stub
  else
    matches?(consumed, path, body)
  end
end
options(path, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 69
def options(path, &block)
  new_stub(:options, path, &block)
end
patch(path, body=nil, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 61
def patch(path, body=nil, &block)
  new_stub(:patch, path, body, &block)
end
post(path, body=nil, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 53
def post(path, body=nil, &block)
  new_stub(:post, path, body, &block)
end
put(path, body=nil, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 57
def put(path, body=nil, &block)
  new_stub(:put, path, 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 74
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, body) click to toggle source
# File lib/faraday/adapter/test.rb, line 92
def matches?(stack, path, body)
  stack.detect { |stub| stub.matches?(path, body) }
end
new_stub(request_method, path, body=nil, &block) click to toggle source
# File lib/faraday/adapter/test.rb, line 88
def new_stub(request_method, path, body=nil, &block)
  (@stack[request_method] ||= []) << Stub.new(normalize_path(path), body, block)
end
normalize_path(path) click to toggle source

ensure leading + trailing slash

# File lib/faraday/adapter/test.rb, line 97
def normalize_path(path)
  path = '/' + path if path.index('/') != 0
  path = path.sub('?', '/?')
  path = path + '/' unless $&
  path.gsub('//', '/')
end