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

Instance Attribute Summary collapse

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, stubs = nil, &block) ⇒ Test

Returns a new instance of Test.



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

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/alice/adapter/test.rb', line 15

def stubs
  @stubs
end

Class Method Details

.loaded?Boolean

Returns:

  • (Boolean)


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

def self.loaded?() false end

Instance Method Details

#call(env) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/alice/adapter/test.rb', line 82

def call(env)
  if stub = stubs.match(env[:method], request_uri(env[:url]), env[:body])
    status, headers, body = stub.block.call(env)
    env.update \
      :status           => status,
      :response_headers => headers,
      :body             => body
  else
    raise "no stubbed request for #{env[:method]} #{request_uri(env[:url])} #{env[:body]}"
  end
  @app.call(env)
end

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

Yields:



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

def configure
  yield stubs
end

#request_uri(url) ⇒ Object



77
78
79
80
# File 'lib/alice/adapter/test.rb', line 77

def request_uri url
  (url.path != "" ? url.path : "/") +
  (url.query ? "?#{url.query}" : "")
end