Class: Faraday::Adapter::Test

Inherits:
Faraday::Adapter show all
Defined in:
lib/faraday/adapter/test.rb

Overview

Examples:

test = Faraday::Connection.new do
  use Faraday::Adapter::Test do |stub|
    # Define matcher to match the request
    stub.get '/resource.json' do
      # return static content
      [200, {'Content-Type' => 'application/json'}, 'hi world']
    end

    # response with content generated based on request
    stub.get '/showget' do |env|
      [200, {'Content-Type' => 'text/plain'}, env[:method].to_s]
    end

    # A regular expression can be used as matching filter
    stub.get /\A\/items\/(\d+)\z/ do |env, meta|
      # in case regular expression is used, an instance of MatchData
      # can be received
      [200,
       {'Content-Type' => 'text/plain'},
       "showing item: #{meta[:match_data][1]}"
      ]
    end

   # Test the request body is the same as the stubbed body
   stub.post('/bar', 'name=YK&word=call') { [200, {}, ''] }

   # You can pass a proc as a stubbed body and check the request body in your way.
   # In this case, the proc should return true or false.
   stub.post('/foo', ->(request_body) do
     JSON.parse(request_body).slice('name') == { 'name' => 'YK' } }) { [200, {}, '']
   end

    # You can set strict_mode to exactly match the stubbed requests.
    stub.strict_mode = true
  end
end

resp = test.get '/resource.json'
resp.body # => 'hi world'

resp = test.get '/showget'
resp.body # => 'get'

resp = test.get '/items/1'
resp.body # => 'showing item: 1'

resp = test.get '/items/2'
resp.body # => 'showing item: 2'

resp = test.post '/bar', 'name=YK&word=call'
resp.status # => 200

resp = test.post '/foo', JSON.dump(name: 'YK', created_at: Time.now)
resp.status # => 200

Defined Under Namespace

Classes: Stub, Stubs

Constant Summary

Constants inherited from Faraday::Adapter

CONTENT_LENGTH

Instance Attribute Summary collapse

Attributes included from Parallelism

#supports_parallel

Instance Method Summary collapse

Methods inherited from Faraday::Adapter

#close, #connection

Methods included from MiddlewareRegistry

#lookup_middleware, #register_middleware, #registered_middleware, #unregister_middleware

Methods included from Parallelism

#inherited, #supports_parallel?

Constructor Details

#initialize(app, stubs = nil, &block) ⇒ Test

Returns a new instance of Test.



258
259
260
261
262
# File 'lib/faraday/adapter/test.rb', line 258

def initialize(app, stubs = nil, &block)
  super(app)
  @stubs = stubs || Stubs.new
  configure(&block) if block
end

Instance Attribute Details

#stubsObject

Returns the value of attribute stubs.



63
64
65
# File 'lib/faraday/adapter/test.rb', line 63

def stubs
  @stubs
end

Instance Method Details

#call(env) ⇒ Object

Parameters:



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/faraday/adapter/test.rb', line 269

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]} #{env[:headers]}"
  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

#configure {|stubs| ... } ⇒ Object

Yields:



264
265
266
# File 'lib/faraday/adapter/test.rb', line 264

def configure
  yield(stubs)
end