Skip to content

Commit

Permalink
Merge pull request #4879 from kennyj/fix_4873
Browse files Browse the repository at this point in the history
Fix GH #4873. Allow swapping same class middleware.
  • Loading branch information
José Valim authored and josevalim committed Feb 4, 2012
1 parent 9aa4c6d commit 9cb0e12
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions actionpack/lib/action_dispatch/middleware/stack.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ def insert_after(index, *args, &block)
end end


def swap(target, *args, &block) def swap(target, *args, &block)
insert_before(target, *args, &block) index = assert_index(target, :before)
delete(target) insert(index, *args, &block)
middlewares.delete_at(index + 1)
end end


def delete(target) def delete(target)
Expand Down
6 changes: 6 additions & 0 deletions actionpack/test/dispatch/middleware_stack_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ def setup
assert_equal BazMiddleware, @stack[0].klass assert_equal BazMiddleware, @stack[0].klass
end 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 test "raise an error on invalid index" do
assert_raise RuntimeError do assert_raise RuntimeError do
@stack.insert("HiyaMiddleware", BazMiddleware) @stack.insert("HiyaMiddleware", BazMiddleware)
Expand Down

0 comments on commit 9cb0e12

Please sign in to comment.