diff options
author | kennyj <kennyj@gmail.com> | 2012-02-04 12:08:39 +0900 |
---|---|---|
committer | kennyj <kennyj@gmail.com> | 2012-02-04 22:59:13 +0900 |
commit | e7ec96989543b95d3459fcdab1f31466ec179233 (patch) | |
tree | 8029a2375cc63bc305d2d978e64b72cf79dfed5b /actionpack | |
parent | 7a72fdc7dcbb58a58b67944f8c1dfeae5cb6ed23 (diff) | |
download | rails-e7ec96989543b95d3459fcdab1f31466ec179233.tar.gz rails-e7ec96989543b95d3459fcdab1f31466ec179233.tar.bz2 rails-e7ec96989543b95d3459fcdab1f31466ec179233.zip |
Fix GH #4873. Allow swapping middleware of same class
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/stack.rb | 5 | ||||
-rw-r--r-- | actionpack/test/dispatch/middleware_stack_test.rb | 6 |
2 files changed, 9 insertions, 2 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index a4308f528c..28e8fbdab8 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -93,8 +93,9 @@ module ActionDispatch end def swap(target, *args, &block) - insert_before(target, *args, &block) - delete(target) + index = assert_index(target, :before) + insert(index, *args, &block) + middlewares.delete_at(index + 1) end def delete(target) diff --git a/actionpack/test/dispatch/middleware_stack_test.rb b/actionpack/test/dispatch/middleware_stack_test.rb index 831f3db3e2..4191ed1ff4 100644 --- a/actionpack/test/dispatch/middleware_stack_test.rb +++ b/actionpack/test/dispatch/middleware_stack_test.rb @@ -81,6 +81,12 @@ class MiddlewareStackTest < ActiveSupport::TestCase assert_equal BazMiddleware, @stack[0].klass end + test "swaps one middleware out for same middleware class" do + assert_equal FooMiddleware, @stack[0].klass + @stack.swap(FooMiddleware, FooMiddleware, Proc.new { |env| [500, {}, ['error!']] }) + assert_equal FooMiddleware, @stack[0].klass + end + test "raise an error on invalid index" do assert_raise RuntimeError do @stack.insert("HiyaMiddleware", BazMiddleware) |