aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch/mapper_test.rb
blob: b6c08ffc33d3c260c4623f68fd9b1a42c8538e27 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'abstract_unit'

module ActionDispatch
  module Routing
    class MapperTest < ActiveSupport::TestCase
      class FakeSet
        attr_reader :routes

        def initialize
          @routes = []
        end

        def resources_path_names
          {}
        end

        def request_class
          ActionDispatch::Request
        end

        def add_route(*args)
          routes << args
        end

        def conditions
          routes.map { |x| x[1] }
        end

        def requirements
          routes.map { |x| x[2] }
        end
      end

      def test_initialize
        Mapper.new FakeSet.new
      end

      def test_map_slash
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.match '/', :to => 'posts#index', :as => :main
        assert_equal '/', fakeset.conditions.first[:path_info]
      end

      def test_map_more_slashes
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset

        # FIXME: is this a desired behavior?
        mapper.match '/one/two/', :to => 'posts#index', :as => :main
        assert_equal '/one/two(.:format)', fakeset.conditions.first[:path_info]
      end

      def test_map_wildcard
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.match '/*path', :to => 'pages#show'
        assert_equal '/*path(.:format)', fakeset.conditions.first[:path_info]
        assert_equal(/.+?/, fakeset.requirements.first[:path])
      end

      def test_map_wildcard_with_other_element
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.match '/*path/foo/:bar', :to => 'pages#show'
        assert_equal '/*path/foo/:bar(.:format)', fakeset.conditions.first[:path_info]
        assert_nil fakeset.requirements.first[:path]
      end

      def test_map_wildcard_with_multiple_wildcard
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.match '/*foo/*bar', :to => 'pages#show'
        assert_equal '/*foo/*bar(.:format)', fakeset.conditions.first[:path_info]
        assert_nil fakeset.requirements.first[:foo]
        assert_equal(/.+?/, fakeset.requirements.first[:bar])
      end

      def test_map_wildcard_with_format_false
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.match '/*path', :to => 'pages#show', :format => false
        assert_equal '/*path', fakeset.conditions.first[:path_info]
        assert_nil fakeset.requirements.first[:path]
      end
    end
  end
end