Class: Faraday::Adapter::Test

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

Overview

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'

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

#save_response

Methods included from Faraday::AutoloadHelper

#all_loaded_constants, #autoload_all, #load_autoloaded_constants

Methods included from Parallelism

#inherited, #supports_parallel?

Methods inherited from Middleware

dependency, inherited, loaded?, new

Methods included from MiddlewareRegistry

#fetch_middleware, #load_middleware, #lookup_middleware, #middleware_mutex, #register_middleware

Constructor Details

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


134
135
136
137
138
# File 'lib/faraday/adapter/test.rb', line 134

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


15
16
17
# File 'lib/faraday/adapter/test.rb', line 15

def stubs
  @stubs
end

Instance Method Details

#call(env) ⇒ Object


144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/faraday/adapter/test.rb', line 144

def call(env)
  super
  normalized_path = Faraday::Utils.normalize_path(env[:url])
  params_encoder = env.request.params_encoder || Faraday::Utils.default_params_encoder

  if stub = stubs.match(env[:method], normalized_path, env.request_headers, env[:body])
    env[:params] = (query = env[:url].query) ?
      params_encoder.decode(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

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

Yields:


140
141
142
# File 'lib/faraday/adapter/test.rb', line 140

def configure
  yield(stubs)
end