2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class MiddlewareTest < Minitest::Test
|
2009-03-26 11:42:13 -04:00
|
|
|
setup do
|
2012-05-21 17:21:59 -04:00
|
|
|
@app = mock_app(Sinatra::Application) do
|
|
|
|
get('/*')do
|
2009-01-30 20:27:17 -05:00
|
|
|
response.headers['X-Tests'] = env['test.ran'].
|
|
|
|
map { |n| n.split('::').last }.
|
|
|
|
join(', ')
|
2008-12-13 16:06:02 -05:00
|
|
|
env['PATH_INFO']
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class MockMiddleware < Struct.new(:app)
|
|
|
|
def call(env)
|
|
|
|
(env['test.ran'] ||= []) << self.class.to_s
|
|
|
|
app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class UpcaseMiddleware < MockMiddleware
|
|
|
|
def call(env)
|
|
|
|
env['PATH_INFO'] = env['PATH_INFO'].upcase
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is added with Sinatra::Application.use" do
|
|
|
|
@app.use UpcaseMiddleware
|
|
|
|
get '/hello-world'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal '/HELLO-WORLD', body
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class DowncaseMiddleware < MockMiddleware
|
|
|
|
def call(env)
|
|
|
|
env['PATH_INFO'] = env['PATH_INFO'].downcase
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-20 14:47:45 -05:00
|
|
|
it "runs in the order defined" do
|
2008-12-13 16:06:02 -05:00
|
|
|
@app.use UpcaseMiddleware
|
|
|
|
@app.use DowncaseMiddleware
|
|
|
|
get '/Foo'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal "/foo", body
|
|
|
|
assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-01-20 14:47:45 -05:00
|
|
|
it "resets the prebuilt pipeline when new middleware is added" do
|
2008-12-13 16:06:02 -05:00
|
|
|
@app.use UpcaseMiddleware
|
|
|
|
get '/Foo'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal "/FOO", body
|
2008-12-13 16:06:02 -05:00
|
|
|
@app.use DowncaseMiddleware
|
|
|
|
get '/Foo'
|
2009-01-14 17:00:26 -05:00
|
|
|
assert_equal '/foo', body
|
|
|
|
assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2009-02-27 01:02:36 -05:00
|
|
|
|
|
|
|
it "works when app is used as middleware" do
|
|
|
|
@app.use UpcaseMiddleware
|
|
|
|
@app = @app.new
|
|
|
|
get '/Foo'
|
|
|
|
assert_equal "/FOO", body
|
|
|
|
assert_equal "UpcaseMiddleware", response['X-Tests']
|
|
|
|
end
|
2018-12-15 14:43:16 -05:00
|
|
|
|
|
|
|
class FreezeMiddleware < MockMiddleware
|
|
|
|
def call(env)
|
|
|
|
req = Rack::Request.new(env)
|
|
|
|
req.update_param('bar', 'baz'.freeze)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works when middleware adds a frozen param" do
|
|
|
|
@app.use FreezeMiddleware
|
|
|
|
get '/Foo'
|
|
|
|
end
|
2019-04-11 04:19:35 -04:00
|
|
|
|
|
|
|
class SpecialConstsMiddleware < MockMiddleware
|
|
|
|
def call(env)
|
|
|
|
req = Rack::Request.new(env)
|
|
|
|
req.update_param('s', :s)
|
|
|
|
req.update_param('i', 1)
|
|
|
|
req.update_param('c', 3.to_c)
|
|
|
|
req.update_param('t', true)
|
|
|
|
req.update_param('f', false)
|
|
|
|
req.update_param('n', nil)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles params when the params contains true/false values" do
|
|
|
|
@app.use SpecialConstsMiddleware
|
|
|
|
get '/'
|
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|