2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class MiddlewareTest < Test::Unit::TestCase
|
|
|
|
setup do
|
2009-10-14 07:01:06 -04:00
|
|
|
@app = mock_app(Sinatra::Application) {
|
2008-12-13 16:06:02 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|