2009-01-13 12:53:53 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
describe 'Sinatra::Base' do
|
|
|
|
it 'includes Rack::Utils' do
|
2009-01-14 17:00:26 -05:00
|
|
|
assert Sinatra::Base.included_modules.include?(Rack::Utils)
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'can be used as a Rack application' do
|
|
|
|
mock_app {
|
|
|
|
get '/' do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
}
|
2009-01-14 17:00:26 -05:00
|
|
|
assert @app.respond_to?(:call)
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
request = Rack::MockRequest.new(@app)
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'can be used as Rack middleware' do
|
|
|
|
app = lambda { |env| [200, {}, ['Goodbye World']] }
|
|
|
|
mock_middleware =
|
|
|
|
mock_app {
|
|
|
|
get '/' do
|
|
|
|
'Hello World'
|
|
|
|
end
|
|
|
|
get '/goodbye' do
|
|
|
|
@app.call(request.env)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
middleware = mock_middleware.new(app)
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_same app, middleware.app
|
2008-12-13 16:06:02 -05:00
|
|
|
|
|
|
|
request = Rack::MockRequest.new(middleware)
|
|
|
|
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
|
|
|
|
|
|
|
response = request.get('/goodbye')
|
2009-01-14 17:00:26 -05:00
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'Goodbye World', response.body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2009-01-08 21:35:00 -05:00
|
|
|
|
|
|
|
it 'can take multiple definitions of a route' do
|
|
|
|
app = mock_app {
|
2009-01-09 04:49:49 -05:00
|
|
|
user_agent(/Foo/)
|
2009-01-08 21:35:00 -05:00
|
|
|
get '/foo' do
|
|
|
|
'foo'
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/foo' do
|
|
|
|
'not foo'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
request = Rack::MockRequest.new(app)
|
|
|
|
response = request.get('/foo', 'HTTP_USER_AGENT' => 'Foo')
|
2009-01-14 17:00:26 -05:00
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'foo', response.body
|
2009-01-08 21:35:00 -05:00
|
|
|
|
|
|
|
request = Rack::MockRequest.new(app)
|
|
|
|
response = request.get('/foo')
|
2009-01-14 17:00:26 -05:00
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'not foo', response.body
|
2009-01-08 21:35:00 -05:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|