test = Faraday::Connection.new do
use Faraday::Adapter::Test do |stub| stub.get '/nigiri/sake.json' do [200, {}, 'hi world'] end end
end
resp = test.get ‘/nigiri/sake.json’ resp.body # => ‘hi world’
# File lib/faraday/adapter/test.rb, line 135 def initialize(app, stubs=nil, &block) super(app) @stubs = stubs || Stubs.new configure(&block) if block end
# File lib/faraday/adapter/test.rb, line 145 def call(env) super normalized_path = Faraday::Utils.normalize_path(env[:url]) if stub = stubs.match(env[:method], normalized_path, env[:body]) env[:params] = (query = env[:url].query) ? Faraday::Utils.parse_nested_query(query) : {} status, headers, body = stub.block.call(env) save_response(env, status, body, headers) else raise Stubs::NotFound, "no stubbed request for #{env[:method]} #{normalized_path} #{env[:body]}" end @app.call(env) end
# File lib/faraday/adapter/test.rb, line 141 def configure yield stubs end