class Faraday::Adapter::Test

resp.status # => 200
resp = test.post ‘/foo’, JSON.dump(name: ‘YK’, created_at: Time.now)
resp.status # => 200
resp = test.post ‘/bar’, ‘name=YK&word=call’
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
stub.strict_mode = true
# You can set strict_mode to exactly match the stubbed requests.
end
JSON.parse(request_body).slice(‘name’) == { ‘name’ => ‘YK’ } }) { [200, {}, ”]
stub.post(‘/foo’, ->(request_body) do
# In this case, the proc should return true or false.
# You can pass a proc as a stubbed body and check the request body in your way.
stub.post(‘/bar’, ‘name=YK&word=call’) { [200, {}, ”] }
# Test the request body is the same as the stubbed body
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)

Parameters:
  • env (Faraday::Env) --
def call(env)
  super
  env.request.params_encoder ||= Faraday::Utils.default_params_encoder
  env[:params] = env.params_encoder.decode(env[:url].query) || {}
  stub, meta = stubs.match(env)
  unless stub
    raise Stubs::NotFound, "no stubbed request for #{env[:method]} " \
                           "#{env[:url]} #{env[:body]}"
  end
  block_arity = stub.block.arity
  params = if block_arity >= 0
             [env, meta].take(block_arity)
           else
             [env, meta]
           end
  timeout = request_timeout(:open, env[:request])
  timeout ||= request_timeout(:read, env[:request])
  status, headers, body =
    if timeout
      ::Timeout.timeout(timeout, Faraday::TimeoutError) do
        stub.block.call(*params)
      end
    else
      stub.block.call(*params)
    end
  # We need to explicitly pass `reason_phrase = nil` here to avoid keyword args conflicts.
  #   See https://github.com/lostisland/faraday/issues/1444
  # TODO: remove `nil` explicit reason_phrase once Ruby 3.0 becomes minimum req. version
  save_response(env, status, body, headers, nil)
  @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