aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch/routing/route_set_test.rb
blob: 8bdb5733ddc7af2238dc9986259dac34ebb28b6d (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
require 'abstract_unit'

module ActionDispatch
  module Routing
    class RouteSetTest < ActiveSupport::TestCase
      class SimpleApp
        def initialize(response)
          @response = response
        end

        def call(env)
          [ 200, { 'Content-Type' => 'text/plain' }, [response] ]
        end
      end

      setup do
        @set = RouteSet.new
      end

      test "url helpers are added when route is added" do
        draw do
          get 'foo', to: SimpleApp.new('foo#index')
        end

        assert_equal '/foo', url_helpers.foo_path
        assert_raises NoMethodError do
          assert_equal '/bar', url_helpers.bar_path
        end

        draw do
          get 'foo', to: SimpleApp.new('foo#index')
          get 'bar', to: SimpleApp.new('bar#index')
        end

        assert_equal '/foo', url_helpers.foo_path
        assert_equal '/bar', url_helpers.bar_path
      end

      test "url helpers are updated when route is updated" do
        draw do
          get 'bar', to: SimpleApp.new('bar#index'), as: :bar
        end

        assert_equal '/bar', url_helpers.bar_path

        draw do
          get 'baz', to: SimpleApp.new('baz#index'), as: :bar
        end

        assert_equal '/baz', url_helpers.bar_path
      end

      test "url helpers are removed when route is removed" do
        draw do
          get 'foo', to: SimpleApp.new('foo#index')
          get 'bar', to: SimpleApp.new('bar#index')
        end

        assert_equal '/foo', url_helpers.foo_path
        assert_equal '/bar', url_helpers.bar_path

        draw do
          get 'foo', to: SimpleApp.new('foo#index')
        end

        assert_equal '/foo', url_helpers.foo_path
        assert_raises NoMethodError do
          assert_equal '/bar', url_helpers.bar_path
        end
      end

      test "only_path: true with *_url and no :host option" do
        draw do
          get 'foo', to: SimpleApp.new('foo#index')
        end

        assert_equal '/foo', url_helpers.foo_url(only_path: true)
      end

      test "only_path: false with *_url and no :host option" do
        draw do
          get 'foo', to: SimpleApp.new('foo#index')
        end

        assert_raises ArgumentError do
          assert_equal 'http://example.com/foo', url_helpers.foo_url(only_path: false)
        end
      end

      test "only_path: false with *_url and local :host option" do
        draw do
          get 'foo', to: SimpleApp.new('foo#index')
        end

        assert_equal 'http://example.com/foo', url_helpers.foo_url(only_path: false, host: 'example.com')
      end

      test "only_path: false with *_url and global :host option" do
        @set.default_url_options = { host: 'example.com' }

        draw do
          get 'foo', to: SimpleApp.new('foo#index')
        end

        assert_equal 'http://example.com/foo', url_helpers.foo_url(only_path: false)
      end

      test "only_path: true with *_path" do
        draw do
          get 'foo', to: SimpleApp.new('foo#index')
        end

        assert_deprecated do
          assert_equal '/foo', url_helpers.foo_path(only_path: true)
        end
      end

      test "only_path: false with *_path with global :host option" do
        @set.default_url_options = { host: 'example.com' }

        draw do
          get 'foo', to: SimpleApp.new('foo#index')
        end

        assert_deprecated do
          assert_equal 'http://example.com/foo', url_helpers.foo_path(only_path: false)
        end
      end

      test "only_path: false with *_path with local :host option" do
        draw do
          get 'foo', to: SimpleApp.new('foo#index')
        end

        assert_deprecated do
          assert_equal 'http://example.com/foo', url_helpers.foo_path(only_path: false, host: 'example.com')
        end
      end

      test "only_path: false with *_path with no :host option" do
        draw do
          get 'foo', to: SimpleApp.new('foo#index')
        end

        assert_deprecated do
          assert_raises ArgumentError do
            assert_equal 'http://example.com/foo', url_helpers.foo_path(only_path: false)
          end
        end
      end

      test "explicit keys win over implicit keys" do
        draw do
          resources :foo do
            resources :bar, to: SimpleApp.new('foo#show')
          end
        end

        assert_equal '/foo/1/bar/2', url_helpers.foo_bar_path(1, 2)
        assert_equal '/foo/1/bar/2', url_helpers.foo_bar_path(2, foo_id: 1)
      end

      test "having an optional scope with resources" do
        draw do
          scope "(/:foo)" do
            resources :users
          end
        end

        assert_equal '/users/1', url_helpers.user_path(1)
        assert_equal '/users/1', url_helpers.user_path(1, foo: nil)
        assert_equal '/a/users/1', url_helpers.user_path(1, foo: 'a')
      end

      test "stringified controller and action keys are properly symbolized" do
        draw do
          root 'foo#bar'
        end

        assert_deprecated do
          assert_equal '/', url_helpers.root_path('controller' => 'foo', 'action' => 'bar')
        end
      end

      test "mix of string and symbol keys are properly symbolized" do
        draw do
          root 'foo#bar'
        end

        assert_deprecated do
          assert_equal '/', url_helpers.root_path('controller' => 'foo', :action => 'bar')
        end
      end

      private
        def draw(&block)
          @set.draw(&block)
        end

        def url_helpers
          @set.url_helpers
        end
    end
  end
end