2011-05-11 09:44:02 +02:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2008-12-13 13:06:02 -08:00
|
|
|
|
2015-01-11 01:00:47 +05:30
|
|
|
class MiddlewareTest < Minitest::Test
|
2009-03-26 16:42:13 +01:00
|
|
|
setup do
|
2012-05-21 17:21:59 -04:00
|
|
|
@app = mock_app(Sinatra::Application) do
|
|
|
|
get('/*')do
|
2009-01-30 17:27:17 -08:00
|
|
|
response.headers['X-Tests'] = env['test.ran'].
|
|
|
|
map { |n| n.split('::').last }.
|
|
|
|
join(', ')
|
2008-12-13 13:06:02 -08:00
|
|
|
env['PATH_INFO']
|
|
|
|
end
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-13 13:06:02 -08: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 14:00:26 -08:00
|
|
|
assert ok?
|
|
|
|
assert_equal '/HELLO-WORLD', body
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
class DowncaseMiddleware < MockMiddleware
|
|
|
|
def call(env)
|
|
|
|
env['PATH_INFO'] = env['PATH_INFO'].downcase
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-20 20:47:45 +01:00
|
|
|
it "runs in the order defined" do
|
2008-12-13 13:06:02 -08:00
|
|
|
@app.use UpcaseMiddleware
|
|
|
|
@app.use DowncaseMiddleware
|
|
|
|
get '/Foo'
|
2009-01-14 14:00:26 -08:00
|
|
|
assert_equal "/foo", body
|
|
|
|
assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|
|
|
|
|
2009-01-20 20:47:45 +01:00
|
|
|
it "resets the prebuilt pipeline when new middleware is added" do
|
2008-12-13 13:06:02 -08:00
|
|
|
@app.use UpcaseMiddleware
|
|
|
|
get '/Foo'
|
2009-01-14 14:00:26 -08:00
|
|
|
assert_equal "/FOO", body
|
2008-12-13 13:06:02 -08:00
|
|
|
@app.use DowncaseMiddleware
|
|
|
|
get '/Foo'
|
2009-01-14 14:00:26 -08:00
|
|
|
assert_equal '/foo', body
|
|
|
|
assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|
2009-02-26 22:02:36 -08: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
|
2008-12-13 13:06:02 -08:00
|
|
|
end
|