2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2009-01-04 00:02:29 -05:00
|
|
|
|
|
|
|
class MiddlewareStackTest < ActiveSupport::TestCase
|
|
|
|
class FooMiddleware; end
|
|
|
|
class BarMiddleware; end
|
|
|
|
class BazMiddleware; end
|
2015-08-07 18:35:39 -04:00
|
|
|
class HiyaMiddleware; end
|
2010-09-11 17:30:21 -04:00
|
|
|
class BlockMiddleware
|
|
|
|
attr_reader :block
|
|
|
|
def initialize(&block)
|
|
|
|
@block = block
|
|
|
|
end
|
|
|
|
end
|
2009-01-04 00:02:29 -05:00
|
|
|
|
|
|
|
def setup
|
2009-01-27 19:54:01 -05:00
|
|
|
@stack = ActionDispatch::MiddlewareStack.new
|
2009-01-04 00:02:29 -05:00
|
|
|
@stack.use FooMiddleware
|
|
|
|
@stack.use BarMiddleware
|
|
|
|
end
|
|
|
|
|
2015-08-07 18:35:39 -04:00
|
|
|
def test_delete_works
|
|
|
|
assert_difference "@stack.size", -1 do
|
|
|
|
@stack.delete FooMiddleware
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-04 00:02:29 -05:00
|
|
|
test "use should push middleware as class onto the stack" do
|
|
|
|
assert_difference "@stack.size" do
|
|
|
|
@stack.use BazMiddleware
|
|
|
|
end
|
|
|
|
assert_equal BazMiddleware, @stack.last.klass
|
|
|
|
end
|
|
|
|
|
|
|
|
test "use should push middleware class with arguments onto the stack" do
|
|
|
|
assert_difference "@stack.size" do
|
2016-08-06 13:35:13 -04:00
|
|
|
@stack.use BazMiddleware, true, foo: "bar"
|
2009-01-04 00:02:29 -05:00
|
|
|
end
|
|
|
|
assert_equal BazMiddleware, @stack.last.klass
|
2016-08-16 03:30:11 -04:00
|
|
|
assert_equal([true, { foo: "bar" }], @stack.last.args)
|
2009-01-04 00:02:29 -05:00
|
|
|
end
|
2012-05-18 02:41:52 -04:00
|
|
|
|
2010-09-11 17:30:21 -04:00
|
|
|
test "use should push middleware class with block arguments onto the stack" do
|
|
|
|
proc = Proc.new {}
|
|
|
|
assert_difference "@stack.size" do
|
|
|
|
@stack.use(BlockMiddleware, &proc)
|
|
|
|
end
|
|
|
|
assert_equal BlockMiddleware, @stack.last.klass
|
|
|
|
assert_equal proc, @stack.last.block
|
|
|
|
end
|
2012-05-18 02:41:52 -04:00
|
|
|
|
2009-01-04 00:02:29 -05:00
|
|
|
test "insert inserts middleware at the integer index" do
|
|
|
|
@stack.insert(1, BazMiddleware)
|
|
|
|
assert_equal BazMiddleware, @stack[1].klass
|
|
|
|
end
|
|
|
|
|
|
|
|
test "insert_after inserts middleware after the integer index" do
|
|
|
|
@stack.insert_after(1, BazMiddleware)
|
|
|
|
assert_equal BazMiddleware, @stack[2].klass
|
|
|
|
end
|
|
|
|
|
|
|
|
test "insert_before inserts middleware before another middleware class" do
|
|
|
|
@stack.insert_before(BarMiddleware, BazMiddleware)
|
|
|
|
assert_equal BazMiddleware, @stack[1].klass
|
|
|
|
end
|
|
|
|
|
|
|
|
test "insert_after inserts middleware after another middleware class" do
|
|
|
|
@stack.insert_after(BarMiddleware, BazMiddleware)
|
|
|
|
assert_equal BazMiddleware, @stack[2].klass
|
|
|
|
end
|
|
|
|
|
2009-01-20 21:19:52 -05:00
|
|
|
test "swaps one middleware out for another" do
|
|
|
|
assert_equal FooMiddleware, @stack[0].klass
|
|
|
|
@stack.swap(FooMiddleware, BazMiddleware)
|
|
|
|
assert_equal BazMiddleware, @stack[0].klass
|
|
|
|
end
|
|
|
|
|
2012-02-03 22:08:39 -05:00
|
|
|
test "swaps one middleware out for same middleware class" do
|
|
|
|
assert_equal FooMiddleware, @stack[0].klass
|
2016-08-06 12:54:50 -04:00
|
|
|
@stack.swap(FooMiddleware, FooMiddleware, Proc.new { |env| [500, {}, ["error!"]] })
|
2012-02-03 22:08:39 -05:00
|
|
|
assert_equal FooMiddleware, @stack[0].klass
|
|
|
|
end
|
|
|
|
|
2012-05-18 02:41:52 -04:00
|
|
|
test "unshift adds a new middleware at the beginning of the stack" do
|
2016-06-19 21:41:11 -04:00
|
|
|
@stack.unshift MiddlewareStackTest::BazMiddleware
|
|
|
|
assert_equal BazMiddleware, @stack.first.klass
|
2012-05-18 02:41:52 -04:00
|
|
|
end
|
|
|
|
|
2010-07-21 09:17:04 -04:00
|
|
|
test "raise an error on invalid index" do
|
|
|
|
assert_raise RuntimeError do
|
2015-08-07 18:35:39 -04:00
|
|
|
@stack.insert(HiyaMiddleware, BazMiddleware)
|
2010-07-21 09:17:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise RuntimeError do
|
2015-08-07 18:35:39 -04:00
|
|
|
@stack.insert_after(HiyaMiddleware, BazMiddleware)
|
2010-07-21 09:17:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-21 18:56:54 -05:00
|
|
|
test "can check if Middleware are equal - Class" do
|
2015-12-22 14:09:26 -05:00
|
|
|
assert_equal @stack.last, BarMiddleware
|
|
|
|
end
|
|
|
|
|
|
|
|
test "includes a class" do
|
|
|
|
assert_equal true, @stack.include?(BarMiddleware)
|
2015-12-21 18:56:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "can check if Middleware are equal - Middleware" do
|
2015-12-22 14:09:26 -05:00
|
|
|
assert_equal @stack.last, @stack.last
|
2015-12-21 18:56:54 -05:00
|
|
|
end
|
|
|
|
|
2015-12-22 14:09:26 -05:00
|
|
|
test "includes a middleware" do
|
|
|
|
assert_equal true, @stack.include?(ActionDispatch::MiddlewareStack::Middleware.new(BarMiddleware, nil, nil))
|
2015-12-21 18:56:54 -05:00
|
|
|
end
|
2009-01-04 00:02:29 -05:00
|
|
|
end
|