moduleFaradaymoduleAdapter# 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'#classTest<Middlewareattr_accessor:stubsdefself.loaded?()falseendclassStubsdefinitialize# {:get => [Stub, Stub]}@stack={}yieldselfifblock_given?enddefempty?@stack.empty?enddefmatch(request_method,path,body)returnfalseif!@stack.key?(request_method)stub=@stack[request_method].detect{|stub|stub.matches?(path,body)}@stack[request_method].deletestubenddefget(path,&block)new_stub(:get,path,&block)enddefhead(path,&block)new_stub(:head,path,&block)enddefpost(path,body=nil,&block)new_stub(:post,path,body,&block)enddefput(path,body=nil,&block)new_stub(:put,path,body,&block)enddefdelete(path,&block)new_stub(:delete,path,&block)enddefnew_stub(request_method,path,body=nil,&block)(@stack[request_method]||=[])<<Stub.new(path,body,block)end# Raises an error if any of the stubbed calls have not been made.defverify_stubbed_callsfailed_stubs=[]@stack.eachdo|method,stubs|unlessstubs.size==0failed_stubs.concat(stubs.map{|stub|"Expected #{method}#{stub}."})endendraisefailed_stubs.join(" ")unlessfailed_stubs.size==0endendclassStub<Struct.new(:path,:body,:block)defmatches?(request_path,request_body)request_path==path&&(body.to_s.size.zero?||request_body==body)enddefto_s"#{path}#{body}"endenddefinitializeapp,stubs=nil,&blocksuper(app)@stubs=stubs||Stubs.newconfigure(&block)ifblockenddefconfigureyieldstubsenddefrequest_uriurl(url.path!=""?url.path:"/")+(url.query?"?#{url.query}":"")enddefcall(env)ifstub=stubs.match(env[:method],request_uri(env[:url]),env[:body])status,headers,body=stub.block.call(env)env.update\:status=>status,:response_headers=>headers,:body=>bodyelseraise"no stubbed request for #{env[:method]}#{request_uri(env[:url])}#{env[:body]}"end@app.call(env)endendendend