aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch/middleware_stack/middleware_test.rb
blob: 6366d37aa4fcf77199cb7dc3f39855cf6358c98d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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