class Faraday::Adapter::Test::Stubs
A stack of Stubs
Public Class Methods
new(strict_mode: false) { |self| ... }
click to toggle source
# File lib/faraday/adapter/test.rb, line 53 def initialize(strict_mode: false) # { get: [Stub, Stub] } @stack = {} @consumed = {} @strict_mode = strict_mode yield(self) if block_given? end
Public Instance Methods
delete(path, headers = {}, &block)
click to toggle source
# File lib/faraday/adapter/test.rb, line 101 def delete(path, headers = {}, &block) new_stub(:delete, path, headers, &block) end
empty?()
click to toggle source
# File lib/faraday/adapter/test.rb, line 61 def empty? @stack.empty? end
get(path, headers = {}, &block)
click to toggle source
# File lib/faraday/adapter/test.rb, line 81 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 85 def head(path, headers = {}, &block) new_stub(:head, path, headers, &block) end
match(env)
click to toggle source
@param env [Faraday::Env]
# File lib/faraday/adapter/test.rb, line 66 def match(env) request_method = env[:method] return false unless @stack.key?(request_method) stack = @stack[request_method] consumed = (@consumed[request_method] ||= []) stub, meta = matches?(stack, env) if stub consumed << stack.delete(stub) return stub, meta end matches?(consumed, env) end
options(path, headers = {}, &block)
click to toggle source
# File lib/faraday/adapter/test.rb, line 105 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 97 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 89 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 93 def put(path, body = nil, headers = {}, &block) new_stub(:put, path, headers, body, &block) end
strict_mode=(value)
click to toggle source
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 126 def strict_mode=(value) @strict_mode = value @stack.each do |_method, stubs| stubs.each do |stub| stub.strict_mode = value end end 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 110 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
matches?(stack, env)
click to toggle source
@param stack [Hash] @param env [Faraday::Env]
# File lib/faraday/adapter/test.rb, line 156 def matches?(stack, env) stack.each do |stub| match_result, meta = stub.matches?(env) 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 137 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