Class: MnoeFaradayTestAdapter

Inherits:
Faraday::Adapter show all
Defined in:
lib/mno_enterprise/testing_support/mnoe_faraday_test_adapter.rb

Overview

Adapter and modified from: github.com/lostisland/faraday/blob/master/lib/faraday/adapter/test.rb

test = Faraday::Connection.new do

use MnoeFaradayTestAdapter 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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MnoeFaradayTestAdapter.



147
148
149
150
151
# File 'lib/mno_enterprise/testing_support/mnoe_faraday_test_adapter.rb', line 147

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.



17
18
19
# File 'lib/mno_enterprise/testing_support/mnoe_faraday_test_adapter.rb', line 17

def stubs
  @stubs
end

Instance Method Details

#call(env) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/mno_enterprise/testing_support/mnoe_faraday_test_adapter.rb', line 157

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:



153
154
155
# File 'lib/mno_enterprise/testing_support/mnoe_faraday_test_adapter.rb', line 153

def configure
  yield(stubs)
end