class Faraday::Adapter::Test::Stub

Stub request

Public Instance Methods

headers_match?(request_headers) click to toggle source
# File lib/faraday/adapter/test.rb, line 206
def headers_match?(request_headers)
  if strict_mode
    headers_with_user_agent = headers.dup.tap do |hs|
      # NOTE: Set User-Agent in case it's not set when creating Stubs.
      #       Users would not want to set Faraday's User-Agent explicitly.
      hs[:user_agent] ||= Connection::USER_AGENT
    end
    return Set.new(headers_with_user_agent) == Set.new(request_headers)
  end

  headers.keys.all? do |key|
    request_headers[key] == headers[key]
  end
end
matches?(env) click to toggle source

@param env [Faraday::Env]

# File lib/faraday/adapter/test.rb, line 168
def matches?(env)
  request_host = env[:url].host
  request_path = Faraday::Utils.normalize_path(env[:url].path)
  request_headers = env.request_headers
  request_body = env[:body]

  # meta is a hash used as carrier
  # that will be yielded to consumer block
  meta = {}
  [(host.nil? || host == request_host) &&
    path_match?(request_path, meta) &&
    params_match?(env) &&
    (body.to_s.size.zero? || request_body == body) &&
    headers_match?(request_headers), meta]
end
params_match?(env) click to toggle source

@param env [Faraday::Env]

# File lib/faraday/adapter/test.rb, line 193
def params_match?(env)
  request_params = env[:params]
  params = env.params_encoder.decode(query) || {}

  if strict_mode
    return Set.new(params) == Set.new(request_params)
  end

  params.keys.all? do |key|
    request_params[key] == params[key]
  end
end
path_match?(request_path, meta) click to toggle source
# File lib/faraday/adapter/test.rb, line 184
def path_match?(request_path, meta)
  if path.is_a?(Regexp)
    !!(meta[:match_data] = path.match(request_path))
  else
    path == request_path
  end
end
to_s() click to toggle source
# File lib/faraday/adapter/test.rb, line 221
def to_s
  "#{path} #{body}"
end