aboutsummaryrefslogblamecommitdiffstats
path: root/actionpack/test/dispatch/middleware_stack/middleware_test.rb
blob: 6366d37aa4fcf77199cb7dc3f39855cf6358c98d (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14













                                                  


                                                     


                                           




                                                     



           





                                                    
                           


                                                   



                                              

                                           
 
                                         
                                                                     


                                              

                                                  


                                              

                                                         
         


       
require 'abstract_unit'
require 'action_dispatch/middleware/stack'

module ActionDispatch
  class MiddlewareStack
    class MiddlewareTest < ActiveSupport::TestCase
      class Omg; end

      {
        'concrete'  => Omg,
        'anonymous' => Class.new
      }.each do |name, klass|

        define_method("test_#{name}_klass") do
          stack = ActionDispatch::MiddlewareStack.new
          stack.use klass
          assert_equal klass, stack.first.klass
        end

        define_method("test_#{name}_==") do
          stack = ActionDispatch::MiddlewareStack.new
          stack.use klass
          stack.use klass
          assert_equal 2, stack.size
          assert_equal stack.first, stack.last
        end

      end

      attr_reader :stack

      def setup
        @stack = ActionDispatch::MiddlewareStack.new
      end

      def test_string_class
        stack = ActionDispatch::MiddlewareStack.new
        stack.use Omg.name
        assert_equal Omg, stack.first.klass
      end

      def test_double_equal_works_with_classes
        k = Class.new
        stack.use k
        assert_operator stack.first, :==, k

        result = stack.first != Class.new
        assert result, 'middleware should not equal other anon class'
      end

      def test_double_equal_works_with_strings
        stack.use Omg
        assert_operator stack.first, :==, Omg.name
      end

      def test_double_equal_normalizes_strings
        stack.use Omg
        assert_operator stack.first, :==, "::#{Omg.name}"
      end
    end
  end
end