module Excon
def stub_for(request_params={})
-
(Hash
- response params to return from matched request or block to call with params)
Parameters:
-
request_params
(Hash
) -- request params to match against, omitted params match all
def stub_for(request_params={}) if (method = request_params.delete(:method)) request_params[:method] = method.to_s.downcase.to_sym end request_params[:query] = Utils.query_string(request_params)[1...] if request_params.key?(:query) Excon.stubs.each do |stub, response_params| captures = { headers: {} } headers_match = !stub.key?(:headers) || stub[:headers].keys.all? do |key| case value = stub[:headers][key] when Regexp case request_params[:headers][key] when String if (match = value.match(request_params[:headers][key])) captures[:headers][key] = match.captures end when Regexp # for unstub on regex params match = (value == request_params[:headers][key]) end match else value == request_params[:headers][key] end end non_headers_match = (stub.keys - [:headers]).all? do |key| case value = stub[key] when Regexp case request_params[key] when String if (match = value.match(request_params[key])) captures[key] = match.captures end when Regexp # for unstub on regex params match = (value == request_params[key]) end match else value == request_params[key] end end if headers_match && non_headers_match request_params[:captures] = captures return [stub, response_params] end end nil end