2011-05-28 11:51:14 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class DelegatorTest < Minitest::Test
|
2011-03-21 06:28:42 -04:00
|
|
|
class Mirror
|
|
|
|
attr_reader :last_call
|
|
|
|
def method_missing(*a, &b)
|
|
|
|
@last_call = [*a.map(&:to_s)]
|
|
|
|
@last_call << b if b
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.delegates(name)
|
|
|
|
it "delegates #{name}" do
|
|
|
|
m = mirror { send name }
|
2011-05-26 10:21:44 -04:00
|
|
|
assert_equal [name.to_s], m.last_call
|
2011-03-21 06:28:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "delegates #{name} with arguments" do
|
|
|
|
m = mirror { send name, "foo", "bar" }
|
2011-05-26 10:21:44 -04:00
|
|
|
assert_equal [name.to_s, "foo", "bar"], m.last_call
|
2011-03-21 06:28:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "delegates #{name} with block" do
|
|
|
|
block = proc { }
|
|
|
|
m = mirror { send(name, &block) }
|
2011-05-26 10:21:44 -04:00
|
|
|
assert_equal [name.to_s, block], m.last_call
|
2011-03-21 06:28:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@target_was = Sinatra::Delegator.target
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
Sinatra::Delegator.target = @target_was
|
|
|
|
end
|
|
|
|
|
|
|
|
def delegation_app(&block)
|
|
|
|
mock_app { Sinatra::Delegator.target = self }
|
|
|
|
delegate(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def mirror(&block)
|
|
|
|
mirror = Mirror.new
|
|
|
|
Sinatra::Delegator.target = mirror
|
|
|
|
delegate(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delegate(&block)
|
|
|
|
assert Sinatra::Delegator.target != Sinatra::Application
|
2011-03-31 07:02:04 -04:00
|
|
|
Object.new.extend(Sinatra::Delegator).instance_eval(&block) if block
|
2011-03-21 06:28:42 -04:00
|
|
|
Sinatra::Delegator.target
|
|
|
|
end
|
|
|
|
|
|
|
|
def target
|
|
|
|
Sinatra::Delegator.target
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'defaults to Sinatra::Application as target' do
|
2011-05-26 10:21:44 -04:00
|
|
|
assert_equal Sinatra::Application, Sinatra::Delegator.target
|
2011-03-21 06:28:42 -04:00
|
|
|
end
|
|
|
|
|
2014-08-12 06:34:00 -04:00
|
|
|
%w[get put post delete options patch link unlink].each do |verb|
|
2011-03-21 06:28:42 -04:00
|
|
|
it "delegates #{verb} correctly" do
|
|
|
|
delegation_app do
|
2012-05-21 17:21:59 -04:00
|
|
|
send(verb, '/hello') { 'Hello World' }
|
2011-03-21 06:28:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
request = Rack::MockRequest.new(@app)
|
|
|
|
response = request.request(verb.upcase, '/hello', {})
|
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'Hello World', response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "delegates head correctly" do
|
|
|
|
delegation_app do
|
|
|
|
head '/hello' do
|
|
|
|
response['X-Hello'] = 'World!'
|
|
|
|
'remove me'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
request = Rack::MockRequest.new(@app)
|
|
|
|
response = request.request('HEAD', '/hello', {})
|
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'World!', response['X-Hello']
|
|
|
|
assert_equal '', response.body
|
|
|
|
end
|
|
|
|
|
2021-03-20 16:22:36 -04:00
|
|
|
it "delegates before with keyword arguments correctly" do
|
|
|
|
delegation_app do
|
|
|
|
set(:foo) do |something|
|
|
|
|
something
|
|
|
|
end
|
|
|
|
before(foo: 'bar') do
|
|
|
|
:nothing
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-31 07:02:04 -04:00
|
|
|
it "registers extensions with the delegation target" do
|
|
|
|
app, mixin = mirror, Module.new
|
|
|
|
Sinatra.register mixin
|
2011-05-26 10:21:44 -04:00
|
|
|
assert_equal ["register", mixin.to_s], app.last_call
|
2011-03-31 07:02:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "registers helpers with the delegation target" do
|
|
|
|
app, mixin = mirror, Module.new
|
|
|
|
Sinatra.helpers mixin
|
2011-05-26 10:21:44 -04:00
|
|
|
assert_equal ["helpers", mixin.to_s], app.last_call
|
2011-03-31 07:02:04 -04:00
|
|
|
end
|
|
|
|
|
2012-05-29 15:31:21 -04:00
|
|
|
it "registers middleware with the delegation target" do
|
2011-03-31 07:06:08 -04:00
|
|
|
app, mixin = mirror, Module.new
|
|
|
|
Sinatra.use mixin
|
2011-05-26 10:21:44 -04:00
|
|
|
assert_equal ["use", mixin.to_s], app.last_call
|
2011-03-31 07:06:08 -04:00
|
|
|
end
|
|
|
|
|
2011-04-17 04:10:22 -04:00
|
|
|
it "should work with method_missing proxies for options" do
|
|
|
|
mixin = Module.new do
|
2011-04-17 04:30:21 -04:00
|
|
|
def respond_to?(method, *)
|
|
|
|
method.to_sym == :options or super
|
2011-04-17 04:10:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(method, *args, &block)
|
|
|
|
return super unless method.to_sym == :options
|
|
|
|
{:some => :option}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-17 04:30:21 -04:00
|
|
|
value = nil
|
|
|
|
mirror do
|
2011-04-17 04:10:22 -04:00
|
|
|
extend mixin
|
2011-04-17 04:30:21 -04:00
|
|
|
value = options
|
2011-04-17 04:10:22 -04:00
|
|
|
end
|
|
|
|
|
2011-04-17 04:30:21 -04:00
|
|
|
assert_equal({:some => :option}, value)
|
2011-04-17 04:19:49 -04:00
|
|
|
end
|
|
|
|
|
2011-04-30 04:19:01 -04:00
|
|
|
it "delegates crazy method names" do
|
|
|
|
Sinatra::Delegator.delegate "foo:bar:"
|
|
|
|
method = mirror { send "foo:bar:" }.last_call.first
|
|
|
|
assert_equal "foo:bar:", method
|
|
|
|
end
|
|
|
|
|
2011-03-21 06:28:42 -04:00
|
|
|
delegates 'get'
|
|
|
|
delegates 'patch'
|
|
|
|
delegates 'put'
|
|
|
|
delegates 'post'
|
|
|
|
delegates 'delete'
|
|
|
|
delegates 'head'
|
|
|
|
delegates 'options'
|
|
|
|
delegates 'template'
|
|
|
|
delegates 'layout'
|
|
|
|
delegates 'before'
|
|
|
|
delegates 'after'
|
|
|
|
delegates 'error'
|
|
|
|
delegates 'not_found'
|
|
|
|
delegates 'configure'
|
|
|
|
delegates 'set'
|
|
|
|
delegates 'mime_type'
|
|
|
|
delegates 'enable'
|
|
|
|
delegates 'disable'
|
|
|
|
delegates 'use'
|
|
|
|
delegates 'development?'
|
|
|
|
delegates 'test?'
|
|
|
|
delegates 'production?'
|
|
|
|
delegates 'helpers'
|
|
|
|
delegates 'settings'
|
|
|
|
end
|