Class: Alice::Adapter::Test

Inherits:
Middleware show all
Defined in:
lib/alice/adapter/test.rb

Overview

test = Alice::Connection.new do

use Alice::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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Middleware

#create_form_params, #full_path_for, #process_body_for_request, setup_parallel_manager

Constructor Details

#initialize(app, &block) ⇒ Test

Returns a new instance of Test.



64
65
66
67
# File 'lib/alice/adapter/test.rb', line 64

def initialize app, &block
  super(app)
  configure(&block) if block
end

Class Method Details

.loaded?Boolean

Returns:

  • (Boolean)


15
# File 'lib/alice/adapter/test.rb', line 15

def self.loaded?() false end

Instance Method Details

#call(env) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/alice/adapter/test.rb', line 77

def call(env)
  if stub = stubs.match(env[:method], env[:url].path)
    status, headers, body = stub.block.call(env)
    env.update \
      :status           => status,
      :response_headers => headers,
      :body             => body
  else
    env.update \
      :status           => 404,
      :response_headers => {},
      :body             => 'no stubbed requests'
  end
  @app.call(env)
end

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

Yields:



69
70
71
# File 'lib/alice/adapter/test.rb', line 69

def configure
  yield stubs
end

#stubsObject



73
74
75
# File 'lib/alice/adapter/test.rb', line 73

def stubs
  @stubs ||= Stubs.new
end