aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch/mapper_test.rb
blob: 1596d23b1ea4f30583784020b53ac094b2566492 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
require "abstract_unit"

module ActionDispatch
  module Routing
    class MapperTest < ActiveSupport::TestCase
      class FakeSet < ActionDispatch::Routing::RouteSet
        def resources_path_names
          {}
        end

        def request_class
          ActionDispatch::Request
        end

        def dispatcher_class
          RouteSet::Dispatcher
        end

        def defaults
          routes.map(&:defaults)
        end

        def conditions
          routes.map(&:constraints)
        end

        def requirements
          routes.map(&:path).map(&:requirements)
        end

        def asts
          routes.map(&:path).map(&:spec)
        end
      end

      def test_initialize
        Mapper.new FakeSet.new
      end

      def test_scope_raises_on_anchor
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        assert_raises(ArgumentError) do
          mapper.scope(anchor: false) do
          end
        end
      end

      def test_blows_up_without_via
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        assert_raises(ArgumentError) do
          mapper.match "/", to: "posts#index", as: :main
        end
      end

      def test_unscoped_formatted
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.get "/foo", to: "posts#index", as: :main, format: true
        assert_equal({ controller: "posts", action: "index" },
                     fakeset.defaults.first)
        assert_equal "/foo.:format", fakeset.asts.first.to_s
      end

      def test_scoped_formatted
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.scope(format: true) do
          mapper.get "/foo", to: "posts#index", as: :main
        end
        assert_equal({ controller: "posts", action: "index" },
                     fakeset.defaults.first)
        assert_equal "/foo.:format", fakeset.asts.first.to_s
      end

      def test_random_keys
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.scope(omg: :awesome) do
          mapper.get "/", to: "posts#index", as: :main
        end
        assert_equal({ omg: :awesome, controller: "posts", action: "index" },
                     fakeset.defaults.first)
        assert_equal("GET", fakeset.routes.first.verb)
      end

      def test_mapping_requirements
        options = {}
        scope = Mapper::Scope.new({})
        ast = Journey::Parser.parse "/store/:name(*rest)"
        m = Mapper::Mapping.build(scope, FakeSet.new, ast, "foo", "bar", nil, [:get], nil, {}, true, options)
        assert_equal(/.+?/, m.requirements[:rest])
      end

      def test_via_scope
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.scope(via: :put) do
          mapper.match "/", to: "posts#index", as: :main
        end
        assert_equal("PUT", fakeset.routes.first.verb)
      end

      def test_to_scope
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.scope(to: "posts#index") do
          mapper.get :all
          mapper.post :most
        end

        assert_equal "posts#index", fakeset.routes.to_a[0].defaults[:to]
        assert_equal "posts#index", fakeset.routes.to_a[1].defaults[:to]
      end

      def test_map_slash
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.get "/", to: "posts#index", as: :main
        assert_equal "/", fakeset.asts.first.to_s
      end

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

        # FIXME: is this a desired behavior?
        mapper.get "/one/two/", to: "posts#index", as: :main
        assert_equal "/one/two(.:format)", fakeset.asts.first.to_s
      end

      def test_map_wildcard
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.get "/*path", to: "pages#show"
        assert_equal "/*path(.:format)", fakeset.asts.first.to_s
        assert_equal(/.+?/, fakeset.requirements.first[:path])
      end

      def test_map_wildcard_with_other_element
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.get "/*path/foo/:bar", to: "pages#show"
        assert_equal "/*path/foo/:bar(.:format)", fakeset.asts.first.to_s
        assert_equal(/.+?/, fakeset.requirements.first[:path])
      end

      def test_map_wildcard_with_multiple_wildcard
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.get "/*foo/*bar", to: "pages#show"
        assert_equal "/*foo/*bar(.:format)", fakeset.asts.first.to_s
        assert_equal(/.+?/, 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.get "/*path", to: "pages#show", format: false
        assert_equal "/*path", fakeset.asts.first.to_s
        assert_nil fakeset.requirements.first[:path]
      end

      def test_map_wildcard_with_format_true
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        mapper.get "/*path", to: "pages#show", format: true
        assert_equal "/*path.:format", fakeset.asts.first.to_s
      end

      def test_raising_error_when_path_is_not_passed
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        app = lambda { |env| [200, {}, [""]] }
        assert_raises ArgumentError do
          mapper.mount app
        end
      end

      def test_raising_error_when_rack_app_is_not_passed
        fakeset = FakeSet.new
        mapper = Mapper.new fakeset
        assert_raises ArgumentError do
          mapper.mount 10, as: "exciting"
        end

        assert_raises ArgumentError do
          mapper.mount as: "exciting"
        end
      end

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

        frozen = { foo: :bar }.freeze

        assert_nothing_raised do
          mapper.scope(defaults: frozen) do
            # pass
          end
        end
      end
    end
  end
end