2023-02-12 13:09:35 -05:00
|
|
|
require_relative 'test_helper'
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class BaseTest < Minitest::Test
|
2009-03-26 11:42:13 -04:00
|
|
|
describe 'Sinatra::Base subclasses' do
|
|
|
|
class TestApp < Sinatra::Base
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { 'Hello World' }
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-22 03:53:43 -05:00
|
|
|
|
2020-12-17 14:35:52 -05:00
|
|
|
class TestKeywordArgumentInitializerApp < Sinatra::Base
|
|
|
|
def initialize(argument:)
|
|
|
|
@argument = argument
|
|
|
|
end
|
|
|
|
|
|
|
|
get('/') { "Hello World with Keyword Arguments: #{@argument}" }
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'include Rack::Utils' do
|
|
|
|
assert TestApp.included_modules.include?(Rack::Utils)
|
2009-02-22 03:53:43 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'processes requests with #call' do
|
|
|
|
assert TestApp.respond_to?(:call)
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
request = Rack::MockRequest.new(TestApp)
|
|
|
|
response = request.get('/')
|
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'Hello World', response.body
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class TestApp < Sinatra::Base
|
|
|
|
get '/state' do
|
2009-05-16 23:36:04 -04:00
|
|
|
@foo ||= "new"
|
2009-03-26 11:42:13 -04:00
|
|
|
body = "Foo: #{@foo}"
|
|
|
|
@foo = 'discard'
|
|
|
|
body
|
|
|
|
end
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'does not maintain state between requests' do
|
|
|
|
request = Rack::MockRequest.new(TestApp)
|
|
|
|
2.times do
|
|
|
|
response = request.get('/state')
|
|
|
|
assert response.ok?
|
2009-05-16 23:36:04 -04:00
|
|
|
assert_equal 'Foo: new', response.body
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-22 03:53:43 -05:00
|
|
|
end
|
2009-03-31 12:52:49 -04:00
|
|
|
|
|
|
|
it "passes the subclass to configure blocks" do
|
|
|
|
ref = nil
|
|
|
|
TestApp.configure { |app| ref = app }
|
|
|
|
assert_equal TestApp, ref
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows the configure block arg to be omitted and does not change context" do
|
|
|
|
context = nil
|
|
|
|
TestApp.configure { context = self }
|
|
|
|
assert_equal self, context
|
|
|
|
end
|
2020-12-17 14:35:52 -05:00
|
|
|
|
|
|
|
it "allows constructor to receive keyword arguments" do
|
|
|
|
app = TestKeywordArgumentInitializerApp.new(argument: "some argument")
|
|
|
|
request = Rack::MockRequest.new(app)
|
|
|
|
|
|
|
|
response = request.get('/')
|
|
|
|
|
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'Hello World with Keyword Arguments: some argument', response.body
|
|
|
|
end
|
2009-02-22 03:53:43 -05:00
|
|
|
end
|
|
|
|
|
2012-05-13 15:54:12 -04:00
|
|
|
describe "Sinatra::Base#new" do
|
|
|
|
it 'returns a wrapper' do
|
|
|
|
assert_equal Sinatra::Wrapper, Sinatra::Base.new.class
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'implements a nice inspect' do
|
|
|
|
assert_equal '#<Sinatra::Base app_file=nil>', Sinatra::Base.new.inspect
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'exposes settings' do
|
|
|
|
assert_equal Sinatra::Base.settings, Sinatra::Base.new.settings
|
|
|
|
end
|
|
|
|
|
2013-03-14 14:20:51 -04:00
|
|
|
it 'exposes helpers' do
|
2012-05-13 15:54:12 -04:00
|
|
|
assert_equal 'image/jpeg', Sinatra::Base.new.helpers.mime_type(:jpg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
describe "Sinatra::Base as Rack middleware" do
|
|
|
|
app = lambda { |env|
|
|
|
|
headers = {'X-Downstream' => 'true'}
|
|
|
|
headers['X-Route-Missing'] = env['sinatra.route-missing'] || ''
|
|
|
|
[210, headers, ['Hello from downstream']] }
|
|
|
|
|
|
|
|
class TestMiddleware < Sinatra::Base
|
2009-02-22 03:53:43 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'creates a middleware that responds to #call with .new' do
|
|
|
|
middleware = TestMiddleware.new(app)
|
|
|
|
assert middleware.respond_to?(:call)
|
|
|
|
end
|
2009-02-22 03:53:43 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'exposes the downstream app' do
|
2011-03-12 09:52:43 -05:00
|
|
|
middleware = TestMiddleware.new!(app)
|
2009-03-26 11:42:13 -04:00
|
|
|
assert_same app, middleware.app
|
|
|
|
end
|
2009-02-22 03:53:43 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class TestMiddleware < Sinatra::Base
|
|
|
|
def route_missing
|
|
|
|
env['sinatra.route-missing'] = '1'
|
|
|
|
super
|
|
|
|
end
|
2009-02-22 03:53:43 -05:00
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/') { 'Hello from middleware' }
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-02-22 03:53:43 -05:00
|
|
|
|
|
|
|
middleware = TestMiddleware.new(app)
|
2009-03-26 11:42:13 -04:00
|
|
|
request = Rack::MockRequest.new(middleware)
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'intercepts requests' do
|
|
|
|
response = request.get('/')
|
|
|
|
assert response.ok?
|
|
|
|
assert_equal 'Hello from middleware', response.body
|
2009-03-11 17:44:09 -04:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04: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
|
2009-02-22 03:53:43 -05:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'calls #route_missing before forwarding downstream' do
|
|
|
|
response = request.get('/missing')
|
|
|
|
assert_equal '1', response['X-Route-Missing']
|
|
|
|
end
|
2009-02-22 03:53:43 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class TestMiddleware < Sinatra::Base
|
2012-05-21 17:21:59 -04:00
|
|
|
get('/low-level-forward') { app.call(env) }
|
2009-03-26 11:42:13 -04:00
|
|
|
end
|
2009-03-11 17:44:09 -04:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
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
|
2009-02-22 03:53:43 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
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
|
2009-02-22 03:53:43 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it 'forwards the request downstream and integrates the response into the current context' do
|
|
|
|
response = request.get('/explicit-forward')
|
2009-02-22 03:53:43 -05:00
|
|
|
assert_equal 210, response.status
|
|
|
|
assert_equal 'true', response['X-Downstream']
|
2009-03-26 11:42:13 -04:00
|
|
|
assert_equal 'Hello after explicit forward', response.body
|
|
|
|
assert_equal '28', response['Content-Length']
|
2009-02-22 03:53:43 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
app_content_length = lambda {|env|
|
|
|
|
[200, {'Content-Length' => '16'}, 'From downstream!']}
|
2009-02-24 10:43:53 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class TestMiddlewareContentLength < Sinatra::Base
|
|
|
|
get '/forward' do
|
|
|
|
'From after explicit forward!'
|
|
|
|
end
|
2009-02-24 10:43:53 -05:00
|
|
|
end
|
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
middleware_content_length = TestMiddlewareContentLength.new(app_content_length)
|
|
|
|
request_content_length = Rack::MockRequest.new(middleware_content_length)
|
2009-02-24 10:43:53 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
it "sets content length for last response" do
|
|
|
|
response = request_content_length.get('/forward')
|
|
|
|
assert_equal '28', response['Content-Length']
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
end
|