class Faraday::Adapter::Test::Stubs
A stack of Stubs
Public Class Methods
# File lib/faraday/adapter/test.rb, line 70 def initialize(strict_mode: false) # { get: [Stub, Stub] } @stack = {} @consumed = {} @strict_mode = strict_mode @stubs_mutex = Monitor.new yield(self) if block_given? end
Public Instance Methods
# File lib/faraday/adapter/test.rb, line 122 def delete(path, headers = {}, &block) new_stub(:delete, path, headers, &block) end
# File lib/faraday/adapter/test.rb, line 79 def empty? @stack.empty? end
# File lib/faraday/adapter/test.rb, line 102 def get(path, headers = {}, &block) new_stub(:get, path, headers, &block) end
# File lib/faraday/adapter/test.rb, line 106 def head(path, headers = {}, &block) new_stub(:head, path, headers, &block) end
@param env [Faraday::Env]
# File lib/faraday/adapter/test.rb, line 84 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
# File lib/faraday/adapter/test.rb, line 126 def options(path, headers = {}, &block) new_stub(:options, path, headers, &block) end
# File lib/faraday/adapter/test.rb, line 118 def patch(path, body = nil, headers = {}, &block) new_stub(:patch, path, headers, body, &block) end
# File lib/faraday/adapter/test.rb, line 110 def post(path, body = nil, headers = {}, &block) new_stub(:post, path, headers, body, &block) end
# File lib/faraday/adapter/test.rb, line 114 def put(path, body = nil, headers = {}, &block) new_stub(:put, path, headers, body, &block) end
Set strict_mode. If the value is true, this adapter tries to find matched requests strictly, which means that all of a path, parameters, and headers must be the same as an actual request.
# File lib/faraday/adapter/test.rb, line 147 def strict_mode=(value) @strict_mode = value @stack.each_value do |stubs| stubs.each do |stub| stub.strict_mode = value end end end
Raises an error if any of the stubbed calls have not been made.
# File lib/faraday/adapter/test.rb, line 131 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
Protected Instance Methods
@param stack [Hash] @param env [Faraday::Env]
# File lib/faraday/adapter/test.rb, line 177 def matches?(stack, env) stack.each do |stub| match_result, meta = stub.matches?(env) return stub, meta if match_result end nil end
# File lib/faraday/adapter/test.rb, line 158 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