class Faraday::Adapter::Test
resp.body # => ‘showing item: 2’
resp = test.get ‘/items/2’
resp.body # => ‘showing item: 1’
resp = test.get ‘/items/1’
resp.body # => ‘get’
resp = test.get ‘/showget’
resp.body # => ‘hi world’
resp = test.get ‘/resource.json’
end
end
end
]
“showing item: #{meta[1]}”
{‘Content-Type’ => ‘text/plain’},
[200,
# can be received
# in case regular expression is used, an instance of MatchData
stub.get /A/items/(d+)z/ do |env, meta|
# A regular expression can be used as matching filter
end
[200, {‘Content-Type’ => ‘text/plain’}, env.to_s]
stub.get ‘/showget’ do |env|
# response with content generated based on request
end
[200, {‘Content-Type’ => ‘application/json’}, ‘hi world’]
# return static content
stub.get ‘/resource.json’ do
# Define matcher to match the request
use Faraday::Adapter::Test do |stub|
test = Faraday::Connection.new do
@example
def call(env)
def call(env) super host = env[:url].host normalized_path = Faraday::Utils.normalize_path(env[:url]) params_encoder = env.request.params_encoder || Faraday::Utils.default_params_encoder stub, meta = stubs.match(env[:method], host, normalized_path, env.request_headers, env[:body]) unless stub raise Stubs::NotFound, "no stubbed request for #{env[:method]} "\ "#{normalized_path} #{env[:body]}" end env[:params] = if (query = env[:url].query) params_encoder.decode(query) else {} end block_arity = stub.block.arity status, headers, body = if block_arity >= 0 stub.block.call(*[env, meta].take(block_arity)) else stub.block.call(env, meta) end save_response(env, status, body, headers) @app.call(env) end
def configure
def configure yield(stubs) end
def initialize(app, stubs = nil, &block)
def initialize(app, stubs = nil, &block) super(app) @stubs = stubs || Stubs.new configure(&block) if block end