2009-01-13 12:53:53 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-02-22 03:53:43 -05:00
|
|
|
describe 'Sinatra::Base subclasses' do
|
|
|
|
|
|
|
|
class TestApp < Sinatra::Base
|
|
|
|
get '/' do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'include Rack::Utils' do
|
|
|
|
assert TestApp.included_modules.include?(Rack::Utils)
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-02-22 03:53:43 -05:00
|
|
|
it 'processes requests with #call' do
|
|
|
|
assert TestApp.respond_to?(:call)
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-02-22 03:53:43 -05:00
|
|
|
request = Rack::MockRequest.new(TestApp)
|
2008-12-13 16:06:02 -05:00
|
|
|
response = request.get('/')
|
2009-01-14 17:00:26 -05:00
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'Hello World', response.body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-02-22 03:53:43 -05:00
|
|
|
class TestApp < Sinatra::Base
|
|
|
|
get '/state' do
|
|
|
|
body = "Foo: #{@foo}"
|
|
|
|
@foo = 'discard'
|
|
|
|
body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not maintain state between requests' do
|
|
|
|
request = Rack::MockRequest.new(TestApp)
|
|
|
|
2.times do
|
|
|
|
response = request.get('/state')
|
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'Foo: ', response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Sinatra::Base as Rack middleware" do
|
|
|
|
|
|
|
|
app = lambda { |env|
|
|
|
|
[210, {'X-Downstream' => 'true'}, ['Hello from downstream']] }
|
|
|
|
|
|
|
|
class TestMiddleware < Sinatra::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a middleware that responds to #call with .new' do
|
|
|
|
middleware = TestMiddleware.new(app)
|
|
|
|
assert middleware.respond_to?(:call)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'exposes the downstream app' do
|
|
|
|
middleware = TestMiddleware.new(app)
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_same app, middleware.app
|
2009-02-22 03:53:43 -05:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-02-22 03:53:43 -05:00
|
|
|
class TestMiddleware < Sinatra::Base
|
|
|
|
get '/' do
|
|
|
|
'Hello from middleware'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
middleware = TestMiddleware.new(app)
|
|
|
|
request = Rack::MockRequest.new(middleware)
|
|
|
|
|
|
|
|
it 'intercepts requests' do
|
2008-12-13 16:06:02 -05:00
|
|
|
response = request.get('/')
|
2009-01-14 17:00:26 -05:00
|
|
|
assert response.ok?
|
2009-02-22 03:53:43 -05:00
|
|
|
assert_equal 'Hello from middleware', response.body
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-02-22 03:53:43 -05:00
|
|
|
it 'automatically forwards requests downstream when no matching route found' do
|
|
|
|
response = request.get('/missing')
|
|
|
|
assert_equal 210, response.status
|
|
|
|
assert_equal 'Hello from downstream', response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestMiddleware < Sinatra::Base
|
|
|
|
get '/low-level-forward' do
|
|
|
|
app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can call the downstream app directly and return result' do
|
|
|
|
response = request.get('/low-level-forward')
|
|
|
|
assert_equal 210, response.status
|
|
|
|
assert_equal 'true', response['X-Downstream']
|
|
|
|
assert_equal 'Hello from downstream', response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
class TestMiddleware < Sinatra::Base
|
|
|
|
get '/explicit-forward' do
|
|
|
|
response['X-Middleware'] = 'true'
|
|
|
|
res = forward
|
|
|
|
assert_nil res
|
|
|
|
assert_equal 210, response.status
|
|
|
|
assert_equal 'true', response['X-Downstream']
|
|
|
|
assert_equal ['Hello from downstream'], response.body
|
|
|
|
'Hello after explicit forward'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'forwards the request downstream and integrates the response into the current context' do
|
|
|
|
response = request.get('/explicit-forward')
|
|
|
|
assert_equal 210, response.status
|
|
|
|
assert_equal 'true', response['X-Downstream']
|
|
|
|
assert_equal 'Hello after explicit forward', response.body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
end
|