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(strict_mode: false)
def initialize(strict_mode: false) # { get: [Stub, Stub] } @stack = {} @consumed = {} @strict_mode = strict_mode @stubs_mutex = Monitor.new yield(self) if block_given? end
def match(env)
-
env
(Faraday::Env
) --
def match(env) request_method = env[:method] return false unless @stack.key?(request_method) stack = @stack[request_method] consumed = (@consumed[request_method] ||= []) @stubs_mutex.synchronize do stub, meta = matches?(stack, env) if stub removed = stack.delete(stub) consumed << removed unless removed.nil? return stub, meta end end matches?(consumed, env) end
def matches?(stack, env)
-
env
(Faraday::Env
) -- -
stack
(Hash
) --
def matches?(stack, env) stack.each do |stub| match_result, meta = stub.matches?(env) 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 path, query = normalized_path.respond_to?(:split) ? normalized_path.split('?') : normalized_path headers = Utils::Headers.new(headers) stub = Stub.new(host, path, query, headers, body, @strict_mode, 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 strict_mode=(value)
Set strict_mode. If the value is true, this adapter tries to find matched requests strictly,
def strict_mode=(value) @strict_mode = value @stack.each do |_method, stubs| stubs.each do |stub| stub.strict_mode = value end end end
def verify_stubbed_calls
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