rails_legacy_mapper 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,959 @@
1
+ require 'test_helper'
2
+
3
+ class RouteSetTest < ActiveSupport::TestCase
4
+ include RoutingTestHelpers
5
+
6
+ def default_set
7
+ @default_route_set ||= begin
8
+ set = ROUTING::RouteSet.new
9
+ set.draw do |map|
10
+ map.connect '/:controller/:action/:id/'
11
+ end
12
+ set
13
+ end
14
+ end
15
+
16
+ def default_recognize_path(path, env = {})
17
+ default_set.recognize_path(path, env)
18
+ end
19
+
20
+ def default_url_for(options, recall = nil)
21
+ default_set.send(:url_for, options.merge(:only_path => true, :_path_segments => recall))
22
+ end
23
+
24
+ def test_generate_extras
25
+ set.draw { |m| m.connect ':controller/:action/:id' }
26
+ path, extras = generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
27
+ assert_equal "/foo/bar/15", path
28
+ assert_equal %w(that this), extras.map { |e| e.to_s }.sort
29
+ end
30
+
31
+ def test_extra_keys
32
+ set.draw { |m| m.connect ':controller/:action/:id' }
33
+ extras = extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
34
+ assert_equal %w(that this), extras.map { |e| e.to_s }.sort
35
+ end
36
+
37
+ def test_generate_extras_not_first
38
+ set.draw do |map|
39
+ map.connect ':controller/:action/:id.:format'
40
+ map.connect ':controller/:action/:id'
41
+ end
42
+ path, extras = generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
43
+ assert_equal "/foo/bar/15", path
44
+ assert_equal %w(that this), extras.map { |e| e.to_s }.sort
45
+ end
46
+
47
+ def test_generate_not_first
48
+ set.draw do |map|
49
+ map.connect ':controller/:action/:id.:format'
50
+ map.connect ':controller/:action/:id'
51
+ end
52
+ assert_equal "/foo/bar/15?this=hello", url_for(:controller => "foo", :action => "bar", :id => 15, :this => "hello")
53
+ end
54
+
55
+ def test_extra_keys_not_first
56
+ set.draw do |map|
57
+ map.connect ':controller/:action/:id.:format'
58
+ map.connect ':controller/:action/:id'
59
+ end
60
+ extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
61
+ assert_equal %w(that this), extras.map { |e| e.to_s }.sort
62
+ end
63
+
64
+ def test_draw
65
+ assert_equal 0, set.routes.size
66
+ set.draw do |map|
67
+ map.connect '/hello/world', :controller => 'a', :action => 'b'
68
+ end
69
+ assert_equal 1, set.routes.size
70
+ end
71
+
72
+ def test_draw_symbol_controller_name
73
+ assert_equal 0, set.routes.size
74
+ set.draw do |map|
75
+ map.connect '/users/index', :controller => :users, :action => :index
76
+ end
77
+ params = recognize_path('/users/index', :method => :get)
78
+ assert_equal 1, set.routes.size
79
+ end
80
+
81
+ def test_named_draw
82
+ assert_equal 0, set.routes.size
83
+ set.draw do |map|
84
+ map.hello '/hello/world', :controller => 'a', :action => 'b'
85
+ end
86
+ assert_equal 1, set.routes.size
87
+ assert_equal set.routes.first, set.named_routes[:hello]
88
+ end
89
+
90
+ def test_later_named_routes_take_precedence
91
+ set.draw do |map|
92
+ map.hello '/hello/world', :controller => 'a', :action => 'b'
93
+ map.hello '/hello', :controller => 'a', :action => 'b'
94
+ end
95
+ assert_equal set.routes.last, set.named_routes[:hello]
96
+ end
97
+
98
+ def setup_named_route_test
99
+ set.draw do |map|
100
+ map.show '/people/:id', :controller => 'people', :action => 'show'
101
+ map.index '/people', :controller => 'people', :action => 'index'
102
+ map.multi '/people/go/:foo/:bar/joe/:id', :controller => 'people', :action => 'multi'
103
+ map.users '/admin/users', :controller => 'admin/users', :action => 'index'
104
+ end
105
+
106
+ MockController.build(set.url_helpers).new
107
+ end
108
+
109
+ def test_named_route_hash_access_method
110
+ controller = setup_named_route_test
111
+
112
+ assert_equal(
113
+ { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => false },
114
+ controller.send(:hash_for_show_url, :id => 5))
115
+
116
+ assert_equal(
117
+ { :controller => 'people', :action => 'index', :use_route => :index, :only_path => false },
118
+ controller.send(:hash_for_index_url))
119
+
120
+ assert_equal(
121
+ { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => true },
122
+ controller.send(:hash_for_show_path, :id => 5)
123
+ )
124
+ end
125
+
126
+ def test_named_route_url_method
127
+ controller = setup_named_route_test
128
+
129
+ assert_equal "http://test.host/people/5", controller.send(:show_url, :id => 5)
130
+ assert_equal "/people/5", controller.send(:show_path, :id => 5)
131
+
132
+ assert_equal "http://test.host/people", controller.send(:index_url)
133
+ assert_equal "/people", controller.send(:index_path)
134
+
135
+ assert_equal "http://test.host/admin/users", controller.send(:users_url)
136
+ assert_equal '/admin/users', controller.send(:users_path)
137
+ assert_equal '/admin/users', url_for(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'})
138
+ end
139
+
140
+ def test_named_route_url_method_with_anchor
141
+ controller = setup_named_route_test
142
+
143
+ assert_equal "http://test.host/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location')
144
+ assert_equal "/people/5#location", controller.send(:show_path, :id => 5, :anchor => 'location')
145
+
146
+ assert_equal "http://test.host/people#location", controller.send(:index_url, :anchor => 'location')
147
+ assert_equal "/people#location", controller.send(:index_path, :anchor => 'location')
148
+
149
+ assert_equal "http://test.host/admin/users#location", controller.send(:users_url, :anchor => 'location')
150
+ assert_equal '/admin/users#location', controller.send(:users_path, :anchor => 'location')
151
+
152
+ assert_equal "http://test.host/people/go/7/hello/joe/5#location",
153
+ controller.send(:multi_url, 7, "hello", 5, :anchor => 'location')
154
+
155
+ assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar#location",
156
+ controller.send(:multi_url, 7, "hello", 5, :baz => "bar", :anchor => 'location')
157
+
158
+ assert_equal "http://test.host/people?baz=bar#location",
159
+ controller.send(:index_url, :baz => "bar", :anchor => 'location')
160
+ end
161
+
162
+ def test_named_route_url_method_with_port
163
+ controller = setup_named_route_test
164
+ assert_equal "http://test.host:8080/people/5", controller.send(:show_url, 5, :port=>8080)
165
+ end
166
+
167
+ def test_named_route_url_method_with_host
168
+ controller = setup_named_route_test
169
+ assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, :host=>"some.example.com")
170
+ end
171
+
172
+ def test_named_route_url_method_with_protocol
173
+ controller = setup_named_route_test
174
+ assert_equal "https://test.host/people/5", controller.send(:show_url, 5, :protocol => "https")
175
+ end
176
+
177
+ def test_named_route_url_method_with_ordered_parameters
178
+ controller = setup_named_route_test
179
+ assert_equal "http://test.host/people/go/7/hello/joe/5",
180
+ controller.send(:multi_url, 7, "hello", 5)
181
+ end
182
+
183
+ def test_named_route_url_method_with_ordered_parameters_and_hash
184
+ controller = setup_named_route_test
185
+ assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar",
186
+ controller.send(:multi_url, 7, "hello", 5, :baz => "bar")
187
+ end
188
+
189
+ def test_named_route_url_method_with_ordered_parameters_and_empty_hash
190
+ controller = setup_named_route_test
191
+ assert_equal "http://test.host/people/go/7/hello/joe/5",
192
+ controller.send(:multi_url, 7, "hello", 5, {})
193
+ end
194
+
195
+ def test_named_route_url_method_with_no_positional_arguments
196
+ controller = setup_named_route_test
197
+ assert_equal "http://test.host/people?baz=bar",
198
+ controller.send(:index_url, :baz => "bar")
199
+ end
200
+
201
+ def test_draw_default_route
202
+ set.draw do |map|
203
+ map.connect '/:controller/:action/:id'
204
+ end
205
+
206
+ assert_equal 1, set.routes.size
207
+
208
+ assert_equal '/users/show/10', url_for(:controller => 'users', :action => 'show', :id => 10)
209
+ assert_equal '/users/index/10', url_for(:controller => 'users', :id => 10)
210
+
211
+ assert_equal({:controller => 'users', :action => 'index', :id => '10'}, recognize_path('/users/index/10'))
212
+ assert_equal({:controller => 'users', :action => 'index', :id => '10'}, recognize_path('/users/index/10/'))
213
+ end
214
+
215
+ def test_draw_default_route_with_default_controller
216
+ set.draw do |map|
217
+ map.connect '/:controller/:action/:id', :controller => 'users'
218
+ end
219
+ assert_equal({:controller => 'users', :action => 'index'}, recognize_path('/'))
220
+ end
221
+
222
+ def test_route_with_parameter_shell
223
+ set.draw do |map|
224
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/
225
+ map.connect '/:controller/:action/:id'
226
+ end
227
+
228
+ assert_equal({:controller => 'pages', :action => 'index'}, recognize_path('/pages'))
229
+ assert_equal({:controller => 'pages', :action => 'index'}, recognize_path('/pages/index'))
230
+ assert_equal({:controller => 'pages', :action => 'list'}, recognize_path('/pages/list'))
231
+
232
+ assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, recognize_path('/pages/show/10'))
233
+ assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, recognize_path('/page/10'))
234
+ end
235
+
236
+ def test_route_constraints_on_request_object_with_anchors_are_valid
237
+ assert_nothing_raised do
238
+ set.draw do
239
+ match 'page/:id' => 'pages#show', :constraints => { :host => /^foo$/ }
240
+ end
241
+ end
242
+ end
243
+
244
+ def test_route_constraints_with_anchor_chars_are_invalid
245
+ assert_raise ArgumentError do
246
+ set.draw do |map|
247
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /^\d+/
248
+ end
249
+ end
250
+ assert_raise ArgumentError do
251
+ set.draw do |map|
252
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\A\d+/
253
+ end
254
+ end
255
+ assert_raise ArgumentError do
256
+ set.draw do |map|
257
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+$/
258
+ end
259
+ end
260
+ assert_raise ArgumentError do
261
+ set.draw do |map|
262
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\Z/
263
+ end
264
+ end
265
+ assert_raise ArgumentError do
266
+ set.draw do |map|
267
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\z/
268
+ end
269
+ end
270
+ end
271
+
272
+ def test_route_requirements_with_invalid_http_method_is_invalid
273
+ assert_raise ArgumentError do
274
+ set.draw do |map|
275
+ map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :invalid}
276
+ end
277
+ end
278
+ end
279
+
280
+ def test_route_requirements_with_options_method_condition_is_valid
281
+ assert_nothing_raised do
282
+ set.draw do |map|
283
+ map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :options}
284
+ end
285
+ end
286
+ end
287
+
288
+ def test_route_requirements_with_head_method_condition_is_invalid
289
+ assert_raise ArgumentError do
290
+ set.draw do |map|
291
+ map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :head}
292
+ end
293
+ end
294
+ end
295
+
296
+ def test_recognize_with_encoded_id_and_regex
297
+ set.draw do |map|
298
+ map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /[a-zA-Z0-9\+]+/
299
+ end
300
+
301
+ assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, recognize_path('/page/10'))
302
+ assert_equal({:controller => 'pages', :action => 'show', :id => 'hello+world'}, recognize_path('/page/hello+world'))
303
+ end
304
+
305
+ def test_recognize_with_conditions
306
+ set.draw do |map|
307
+ map.with_options(:controller => "people") do |people|
308
+ people.people "/people", :action => "index", :conditions => { :method => :get }
309
+ people.connect "/people", :action => "create", :conditions => { :method => :post }
310
+ people.person "/people/:id", :action => "show", :conditions => { :method => :get }
311
+ people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
312
+ people.connect "/people/:id", :action => "destroy", :conditions => { :method => :delete }
313
+ end
314
+ end
315
+
316
+ params = recognize_path("/people", :method => :get)
317
+ assert_equal("index", params[:action])
318
+
319
+ params = recognize_path("/people", :method => :post)
320
+ assert_equal("create", params[:action])
321
+
322
+ params = recognize_path("/people", :method => :put)
323
+ assert_equal("update", params[:action])
324
+
325
+ assert_raise(ActionController::UnknownHttpMethod) {
326
+ recognize_path("/people", :method => :bacon)
327
+ }
328
+
329
+ params = recognize_path("/people/5", :method => :get)
330
+ assert_equal("show", params[:action])
331
+ assert_equal("5", params[:id])
332
+
333
+ params = recognize_path("/people/5", :method => :put)
334
+ assert_equal("update", params[:action])
335
+ assert_equal("5", params[:id])
336
+
337
+ params = recognize_path("/people/5", :method => :delete)
338
+ assert_equal("destroy", params[:action])
339
+ assert_equal("5", params[:id])
340
+
341
+ assert_raise(ActionController::RoutingError) {
342
+ recognize_path("/people/5", :method => :post)
343
+ }
344
+ end
345
+
346
+ def test_recognize_with_alias_in_conditions
347
+ set.draw do |map|
348
+ map.people "/people", :controller => 'people', :action => "index",
349
+ :conditions => { :method => :get }
350
+ map.root :people
351
+ end
352
+
353
+ params = recognize_path("/people", :method => :get)
354
+ assert_equal("people", params[:controller])
355
+ assert_equal("index", params[:action])
356
+
357
+ params = recognize_path("/", :method => :get)
358
+ assert_equal("people", params[:controller])
359
+ assert_equal("index", params[:action])
360
+ end
361
+
362
+ def test_typo_recognition
363
+ set.draw do |map|
364
+ map.connect 'articles/:year/:month/:day/:title',
365
+ :controller => 'articles', :action => 'permalink',
366
+ :year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/
367
+ end
368
+
369
+ params = recognize_path("/articles/2005/11/05/a-very-interesting-article", :method => :get)
370
+ assert_equal("permalink", params[:action])
371
+ assert_equal("2005", params[:year])
372
+ assert_equal("11", params[:month])
373
+ assert_equal("05", params[:day])
374
+ assert_equal("a-very-interesting-article", params[:title])
375
+ end
376
+
377
+ def test_routing_traversal_does_not_load_extra_classes
378
+ assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
379
+ set.draw do |map|
380
+ map.connect '/profile', :controller => 'profile'
381
+ end
382
+
383
+ params = recognize_path("/profile") rescue nil
384
+
385
+ assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
386
+ end
387
+
388
+ def test_recognize_with_conditions_and_format
389
+ set.draw do |map|
390
+ map.with_options(:controller => "people") do |people|
391
+ people.person "/people/:id", :action => "show", :conditions => { :method => :get }
392
+ people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
393
+ people.connect "/people/:id.:_format", :action => "show", :conditions => { :method => :get }
394
+ end
395
+ end
396
+
397
+ params = recognize_path("/people/5", :method => :get)
398
+ assert_equal("show", params[:action])
399
+ assert_equal("5", params[:id])
400
+
401
+ params = recognize_path("/people/5", :method => :put)
402
+ assert_equal("update", params[:action])
403
+
404
+ params = recognize_path("/people/5.png", :method => :get)
405
+ assert_equal("show", params[:action])
406
+ assert_equal("5", params[:id])
407
+ assert_equal("png", params[:_format])
408
+ end
409
+
410
+ def test_generate_with_default_action
411
+ set.draw do |map|
412
+ map.connect "/people", :controller => "people"
413
+ map.connect "/people/list", :controller => "people", :action => "list"
414
+ end
415
+
416
+ url = url_for(:controller => "people", :action => "list")
417
+ assert_equal "/people/list", url
418
+ end
419
+
420
+ def test_root_map
421
+ set.draw { |map| map.root :controller => "people" }
422
+
423
+ params = recognize_path("", :method => :get)
424
+ assert_equal("people", params[:controller])
425
+ assert_equal("index", params[:action])
426
+ end
427
+
428
+ def test_namespace
429
+ set.draw do |map|
430
+
431
+ map.namespace 'api' do |api|
432
+ api.route 'inventory', :controller => "products", :action => 'inventory'
433
+ end
434
+
435
+ end
436
+
437
+ params = recognize_path("/api/inventory", :method => :get)
438
+ assert_equal("api/products", params[:controller])
439
+ assert_equal("inventory", params[:action])
440
+ end
441
+
442
+ def test_namespaced_root_map
443
+ set.draw do |map|
444
+
445
+ map.namespace 'api' do |api|
446
+ api.root :controller => "products"
447
+ end
448
+
449
+ end
450
+
451
+ params = recognize_path("/api", :method => :get)
452
+ assert_equal("api/products", params[:controller])
453
+ assert_equal("index", params[:action])
454
+ end
455
+
456
+ def test_namespace_with_path_prefix
457
+ set.draw do |map|
458
+ map.namespace 'api', :path_prefix => 'prefix' do |api|
459
+ api.route 'inventory', :controller => "products", :action => 'inventory'
460
+ end
461
+ end
462
+
463
+ params = recognize_path("/prefix/inventory", :method => :get)
464
+ assert_equal("api/products", params[:controller])
465
+ assert_equal("inventory", params[:action])
466
+ end
467
+
468
+ def test_namespace_with_blank_path_prefix
469
+ set.draw do |map|
470
+ map.namespace 'api', :path_prefix => '' do |api|
471
+ api.route 'inventory', :controller => "products", :action => 'inventory'
472
+ end
473
+ end
474
+
475
+ params = recognize_path("/inventory", :method => :get)
476
+ assert_equal("api/products", params[:controller])
477
+ assert_equal("inventory", params[:action])
478
+ end
479
+
480
+ def test_generate_changes_controller_module
481
+ set.draw { |map| map.connect ':controller/:action/:id' }
482
+ current = { :controller => "bling/bloop", :action => "bap", :id => 9 }
483
+ url = url_for({:controller => "foo/bar", :action => "baz", :id => 7}, current)
484
+ assert_equal "/foo/bar/baz/7", url
485
+ end
486
+
487
+ def test_id_is_sticky_when_it_ought_to_be
488
+ set.draw do |map|
489
+ map.connect ':controller/:id/:action'
490
+ end
491
+
492
+ url = url_for({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"})
493
+ assert_equal "/people/7/destroy", url
494
+ end
495
+
496
+ def test_use_static_path_when_possible
497
+ set.draw do |map|
498
+ map.connect 'about', :controller => "welcome", :action => "about"
499
+ map.connect ':controller/:action/:id'
500
+ end
501
+
502
+ url = url_for({:controller => "welcome", :action => "about"},
503
+ {:controller => "welcome", :action => "get", :id => "7"})
504
+ assert_equal "/about", url
505
+ end
506
+
507
+ def test_generate
508
+ set.draw { |map| map.connect ':controller/:action/:id' }
509
+
510
+ args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
511
+ assert_equal "/foo/bar/7?x=y", url_for(args)
512
+ assert_equal ["/foo/bar/7", [:x]], generate_extras(args)
513
+ assert_equal [:x], set.extra_keys(args)
514
+ end
515
+
516
+ def test_generate_with_path_prefix
517
+ set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => 'my' }
518
+
519
+ args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
520
+ assert_equal "/my/foo/bar/7?x=y", url_for(args)
521
+ end
522
+
523
+ def test_generate_with_blank_path_prefix
524
+ set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => '' }
525
+
526
+ args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
527
+ assert_equal "/foo/bar/7?x=y", url_for(args)
528
+ end
529
+
530
+ def test_named_routes_are_never_relative_to_modules
531
+ set.draw do |map|
532
+ map.connect "/connection/manage/:action", :controller => 'connection/manage'
533
+ map.connect "/connection/connection", :controller => "connection/connection"
534
+ map.family_connection "/connection", :controller => "connection"
535
+ end
536
+
537
+ url = url_for({:controller => "connection"}, {:controller => 'connection/manage'})
538
+ assert_equal "/connection/connection", url
539
+
540
+ url = url_for({:use_route => :family_connection, :controller => "connection"}, {:controller => 'connection/manage'})
541
+ assert_equal "/connection", url
542
+ end
543
+
544
+ def test_action_left_off_when_id_is_recalled
545
+ set.draw do |map|
546
+ map.connect ':controller/:action/:id'
547
+ end
548
+ assert_equal '/books', url_for(
549
+ {:controller => 'books', :action => 'index'},
550
+ {:controller => 'books', :action => 'show', :id => '10'}
551
+ )
552
+ end
553
+
554
+ def test_query_params_will_be_shown_when_recalled
555
+ set.draw do |map|
556
+ map.connect 'show_weblog/:parameter', :controller => 'weblog', :action => 'show'
557
+ map.connect ':controller/:action/:id'
558
+ end
559
+ assert_equal '/weblog/edit?parameter=1', url_for(
560
+ {:action => 'edit', :parameter => 1},
561
+ {:controller => 'weblog', :action => 'show', :parameter => 1}
562
+ )
563
+ end
564
+
565
+ def test_format_is_not_inherit
566
+ set.draw do |map|
567
+ map.connect '/posts.:format', :controller => 'posts'
568
+ end
569
+
570
+ assert_equal '/posts', url_for(
571
+ {:controller => 'posts'},
572
+ {:controller => 'posts', :action => 'index', :format => 'xml'}
573
+ )
574
+
575
+ assert_equal '/posts.xml', url_for(
576
+ {:controller => 'posts', :format => 'xml'},
577
+ {:controller => 'posts', :action => 'index', :format => 'xml'}
578
+ )
579
+ end
580
+
581
+ def test_expiry_determination_should_consider_values_with_to_param
582
+ set.draw { |map| map.connect 'projects/:project_id/:controller/:action' }
583
+ assert_equal '/projects/1/weblog/show', url_for(
584
+ {:action => 'show', :project_id => 1},
585
+ {:controller => 'weblog', :action => 'show', :project_id => '1'})
586
+ end
587
+
588
+ def test_named_route_in_nested_resource
589
+ set.draw do |map|
590
+ map.resources :projects do |project|
591
+ project.milestones 'milestones', :controller => 'milestones', :action => 'index'
592
+ end
593
+ end
594
+
595
+ params = recognize_path("/projects/1/milestones", :method => :get)
596
+ assert_equal("milestones", params[:controller])
597
+ assert_equal("index", params[:action])
598
+ end
599
+
600
+ def test_setting_root_in_namespace_using_symbol
601
+ assert_nothing_raised do
602
+ set.draw do |map|
603
+ map.namespace :admin do |admin|
604
+ admin.root :controller => 'home'
605
+ end
606
+ end
607
+ end
608
+ end
609
+
610
+ def test_setting_root_in_namespace_using_string
611
+ assert_nothing_raised do
612
+ set.draw do |map|
613
+ map.namespace 'admin' do |admin|
614
+ admin.root :controller => 'home'
615
+ end
616
+ end
617
+ end
618
+ end
619
+
620
+ def test_route_requirements_with_unsupported_regexp_options_must_error
621
+ assert_raise ArgumentError do
622
+ set.draw do |map|
623
+ map.connect 'page/:name', :controller => 'pages',
624
+ :action => 'show',
625
+ :requirements => {:name => /(david|jamis)/m}
626
+ end
627
+ end
628
+ end
629
+
630
+ def test_route_requirements_with_supported_options_must_not_error
631
+ assert_nothing_raised do
632
+ set.draw do |map|
633
+ map.connect 'page/:name', :controller => 'pages',
634
+ :action => 'show',
635
+ :requirements => {:name => /(david|jamis)/i}
636
+ end
637
+ end
638
+ assert_nothing_raised do
639
+ set.draw do |map|
640
+ map.connect 'page/:name', :controller => 'pages',
641
+ :action => 'show',
642
+ :requirements => {:name => / # Desperately overcommented regexp
643
+ ( #Either
644
+ david #The Creator
645
+ | #Or
646
+ jamis #The Deployer
647
+ )/x}
648
+ end
649
+ end
650
+ end
651
+
652
+ def test_route_requirement_recognize_with_ignore_case
653
+ set.draw do |map|
654
+ map.connect 'page/:name', :controller => 'pages',
655
+ :action => 'show',
656
+ :requirements => {:name => /(david|jamis)/i}
657
+ end
658
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, recognize_path('/page/jamis'))
659
+ assert_raise ActionController::RoutingError do
660
+ recognize_path('/page/davidjamis')
661
+ end
662
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'DAVID'}, recognize_path('/page/DAVID'))
663
+ end
664
+
665
+ def test_route_requirement_generate_with_ignore_case
666
+ set.draw do |map|
667
+ map.connect 'page/:name', :controller => 'pages',
668
+ :action => 'show',
669
+ :requirements => {:name => /(david|jamis)/i}
670
+ end
671
+
672
+ url = url_for({:controller => 'pages', :action => 'show', :name => 'david'})
673
+ assert_equal "/page/david", url
674
+ assert_raise ActionController::RoutingError do
675
+ url = url_for({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
676
+ end
677
+ url = url_for({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
678
+ assert_equal "/page/JAMIS", url
679
+ end
680
+
681
+ def test_route_requirement_recognize_with_extended_syntax
682
+ set.draw do |map|
683
+ map.connect 'page/:name', :controller => 'pages',
684
+ :action => 'show',
685
+ :requirements => {:name => / # Desperately overcommented regexp
686
+ ( #Either
687
+ david #The Creator
688
+ | #Or
689
+ jamis #The Deployer
690
+ )/x}
691
+ end
692
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, recognize_path('/page/jamis'))
693
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'david'}, recognize_path('/page/david'))
694
+ assert_raise ActionController::RoutingError do
695
+ recognize_path('/page/david #The Creator')
696
+ end
697
+ assert_raise ActionController::RoutingError do
698
+ recognize_path('/page/David')
699
+ end
700
+ end
701
+
702
+ def test_route_requirement_generate_with_extended_syntax
703
+ set.draw do |map|
704
+ map.connect 'page/:name', :controller => 'pages',
705
+ :action => 'show',
706
+ :requirements => {:name => / # Desperately overcommented regexp
707
+ ( #Either
708
+ david #The Creator
709
+ | #Or
710
+ jamis #The Deployer
711
+ )/x}
712
+ end
713
+
714
+ url = url_for({:controller => 'pages', :action => 'show', :name => 'david'})
715
+ assert_equal "/page/david", url
716
+ assert_raise ActionController::RoutingError do
717
+ url = url_for({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
718
+ end
719
+ assert_raise ActionController::RoutingError do
720
+ url = url_for({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
721
+ end
722
+ end
723
+
724
+ def test_route_requirement_generate_with_xi_modifiers
725
+ set.draw do |map|
726
+ map.connect 'page/:name', :controller => 'pages',
727
+ :action => 'show',
728
+ :requirements => {:name => / # Desperately overcommented regexp
729
+ ( #Either
730
+ david #The Creator
731
+ | #Or
732
+ jamis #The Deployer
733
+ )/xi}
734
+ end
735
+
736
+ url = url_for({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
737
+ assert_equal "/page/JAMIS", url
738
+ end
739
+
740
+ def test_route_requirement_recognize_with_xi_modifiers
741
+ set.draw do |map|
742
+ map.connect 'page/:name', :controller => 'pages',
743
+ :action => 'show',
744
+ :requirements => {:name => / # Desperately overcommented regexp
745
+ ( #Either
746
+ david #The Creator
747
+ | #Or
748
+ jamis #The Deployer
749
+ )/xi}
750
+ end
751
+ assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'}, recognize_path('/page/JAMIS'))
752
+ end
753
+
754
+ def test_routes_with_symbols
755
+ set.draw do |map|
756
+ map.connect 'unnamed', :controller => :pages, :action => :show, :name => :as_symbol
757
+ map.named 'named', :controller => :pages, :action => :show, :name => :as_symbol
758
+ end
759
+ assert_equal({:controller => 'pages', :action => 'show', :name => :as_symbol}, recognize_path('/unnamed'))
760
+ assert_equal({:controller => 'pages', :action => 'show', :name => :as_symbol}, recognize_path('/named'))
761
+ end
762
+
763
+ def test_regexp_chunk_should_add_question_mark_for_optionals
764
+ set.draw do |map|
765
+ map.connect '/', :controller => 'foo'
766
+ map.connect '/hello', :controller => 'bar'
767
+ end
768
+
769
+ assert_equal '/', url_for(:controller => 'foo')
770
+ assert_equal '/hello', url_for(:controller => 'bar')
771
+
772
+ assert_equal({:controller => "foo", :action => "index"}, recognize_path('/'))
773
+ assert_equal({:controller => "bar", :action => "index"}, recognize_path('/hello'))
774
+ end
775
+
776
+ def test_assign_route_options_with_anchor_chars
777
+ set.draw do |map|
778
+ map.connect '/cars/:action/:person/:car/', :controller => 'cars'
779
+ end
780
+
781
+ assert_equal '/cars/buy/1/2', url_for(:controller => 'cars', :action => 'buy', :person => '1', :car => '2')
782
+
783
+ assert_equal({:controller => "cars", :action => "buy", :person => "1", :car => "2"}, recognize_path('/cars/buy/1/2'))
784
+ end
785
+
786
+ def test_segmentation_of_dot_path
787
+ set.draw do |map|
788
+ map.connect '/books/:action.rss', :controller => 'books'
789
+ end
790
+
791
+ assert_equal '/books/list.rss', url_for(:controller => 'books', :action => 'list')
792
+
793
+ assert_equal({:controller => "books", :action => "list"}, recognize_path('/books/list.rss'))
794
+ end
795
+
796
+ def test_segmentation_of_dynamic_dot_path
797
+ set.draw do |map|
798
+ map.connect '/books/:action.:format', :controller => 'books'
799
+ end
800
+
801
+ assert_equal '/books/list.rss', url_for(:controller => 'books', :action => 'list', :format => 'rss')
802
+ assert_equal '/books/list.xml', url_for(:controller => 'books', :action => 'list', :format => 'xml')
803
+ assert_equal '/books/list', url_for(:controller => 'books', :action => 'list')
804
+ assert_equal '/books', url_for(:controller => 'books', :action => 'index')
805
+
806
+ assert_equal({:controller => "books", :action => "list", :format => "rss"}, recognize_path('/books/list.rss'))
807
+ assert_equal({:controller => "books", :action => "list", :format => "xml"}, recognize_path('/books/list.xml'))
808
+ assert_equal({:controller => "books", :action => "list"}, recognize_path('/books/list'))
809
+ assert_equal({:controller => "books", :action => "index"}, recognize_path('/books'))
810
+ end
811
+
812
+ def test_slashes_are_implied
813
+ ['/:controller/:action/:id/', '/:controller/:action/:id',
814
+ ':controller/:action/:id', '/:controller/:action/:id/'
815
+ ].each do |path|
816
+ @set = nil
817
+ set.draw { |map| map.connect(path) }
818
+
819
+ assert_equal '/content', url_for(:controller => 'content', :action => 'index')
820
+ assert_equal '/content/list', url_for(:controller => 'content', :action => 'list')
821
+ assert_equal '/content/show/1', url_for(:controller => 'content', :action => 'show', :id => '1')
822
+
823
+ assert_equal({:controller => "content", :action => "index"}, recognize_path('/content'))
824
+ assert_equal({:controller => "content", :action => "index"}, recognize_path('/content/index'))
825
+ assert_equal({:controller => "content", :action => "list"}, recognize_path('/content/list'))
826
+ assert_equal({:controller => "content", :action => "show", :id => "1"}, recognize_path('/content/show/1'))
827
+ end
828
+ end
829
+
830
+ def test_default_route_recognition
831
+ expected = {:controller => 'pages', :action => 'show', :id => '10'}
832
+ assert_equal expected, default_recognize_path('/pages/show/10')
833
+ assert_equal expected, default_recognize_path('/pages/show/10/')
834
+
835
+ expected[:id] = 'jamis'
836
+ assert_equal expected, default_recognize_path('/pages/show/jamis/')
837
+
838
+ expected.delete :id
839
+ assert_equal expected, default_recognize_path('/pages/show')
840
+ assert_equal expected, default_recognize_path('/pages/show/')
841
+
842
+ expected[:action] = 'index'
843
+ assert_equal expected, default_recognize_path('/pages/')
844
+ assert_equal expected, default_recognize_path('/pages')
845
+
846
+ assert_raise(ActionController::RoutingError) { default_recognize_path('/') }
847
+ assert_raise(ActionController::RoutingError) { default_recognize_path('/pages/how/goood/it/is/to/be/free') }
848
+ end
849
+
850
+ def test_default_route_should_omit_default_action
851
+ assert_equal '/accounts', default_url_for({:controller => 'accounts', :action => 'index'})
852
+ end
853
+
854
+ def test_default_route_should_include_default_action_when_id_present
855
+ assert_equal '/accounts/index/20', default_url_for({:controller => 'accounts', :action => 'index', :id => '20'})
856
+ end
857
+
858
+ def test_default_route_should_work_with_action_but_no_id
859
+ assert_equal '/accounts/list_all', default_url_for({:controller => 'accounts', :action => 'list_all'})
860
+ end
861
+
862
+ def test_default_route_should_uri_escape_pluses
863
+ expected = { :controller => 'pages', :action => 'show', :id => 'hello world' }
864
+ assert_equal expected, default_recognize_path('/pages/show/hello%20world')
865
+ assert_equal '/pages/show/hello%20world', default_url_for(expected, expected)
866
+
867
+ expected[:id] = 'hello+world'
868
+ assert_equal expected, default_recognize_path('/pages/show/hello+world')
869
+ assert_equal expected, default_recognize_path('/pages/show/hello%2Bworld')
870
+ assert_equal '/pages/show/hello+world', default_url_for(expected, expected)
871
+ end
872
+
873
+ def test_build_empty_query_string
874
+ assert_uri_equal '/foo', default_url_for({:controller => 'foo'})
875
+ end
876
+
877
+ def test_build_query_string_with_nil_value
878
+ assert_uri_equal '/foo', default_url_for({:controller => 'foo', :x => nil})
879
+ end
880
+
881
+ def test_simple_build_query_string
882
+ assert_uri_equal '/foo?x=1&y=2', default_url_for({:controller => 'foo', :x => '1', :y => '2'})
883
+ end
884
+
885
+ def test_convert_ints_build_query_string
886
+ assert_uri_equal '/foo?x=1&y=2', default_url_for({:controller => 'foo', :x => 1, :y => 2})
887
+ end
888
+
889
+ def test_escape_spaces_build_query_string
890
+ assert_uri_equal '/foo?x=hello+world&y=goodbye+world', default_url_for({:controller => 'foo', :x => 'hello world', :y => 'goodbye world'})
891
+ end
892
+
893
+ def test_expand_array_build_query_string
894
+ assert_uri_equal '/foo?x%5B%5D=1&x%5B%5D=2', default_url_for({:controller => 'foo', :x => [1, 2]})
895
+ end
896
+
897
+ def test_escape_spaces_build_query_string_selected_keys
898
+ assert_uri_equal '/foo?x=hello+world', default_url_for({:controller => 'foo', :x => 'hello world'})
899
+ end
900
+
901
+ def test_generate_with_default_params
902
+ set.draw do |map|
903
+ map.connect 'dummy/page/:page', :controller => 'dummy'
904
+ map.connect 'dummy/dots/page.:page', :controller => 'dummy', :action => 'dots'
905
+ map.connect 'ibocorp/:page', :controller => 'ibocorp',
906
+ :requirements => { :page => /\d+/ },
907
+ :defaults => { :page => 1 }
908
+
909
+ map.connect ':controller/:action/:id'
910
+ end
911
+
912
+ assert_equal '/ibocorp', url_for({:controller => 'ibocorp', :page => 1})
913
+ end
914
+
915
+ def test_generate_with_optional_params_recalls_last_request
916
+ set.draw do |map|
917
+ map.connect "blog/", :controller => "blog", :action => "index"
918
+
919
+ map.connect "blog/:year/:month/:day",
920
+ :controller => "blog",
921
+ :action => "show_date",
922
+ :requirements => { :year => /(19|20)\d\d/, :month => /[01]?\d/, :day => /[0-3]?\d/ },
923
+ :day => nil, :month => nil
924
+
925
+ map.connect "blog/show/:id", :controller => "blog", :action => "show", :id => /\d+/
926
+ map.connect "blog/:controller/:action/:id"
927
+ map.connect "*anything", :controller => "blog", :action => "unknown_request"
928
+ end
929
+
930
+ assert_equal({:controller => "blog", :action => "index"}, recognize_path("/blog"))
931
+ assert_equal({:controller => "blog", :action => "show", :id => "123"}, recognize_path("/blog/show/123"))
932
+ assert_equal({:controller => "blog", :action => "show_date", :year => "2004"}, recognize_path("/blog/2004"))
933
+ assert_equal({:controller => "blog", :action => "show_date", :year => "2004", :month => "12"}, recognize_path("/blog/2004/12"))
934
+ assert_equal({:controller => "blog", :action => "show_date", :year => "2004", :month => "12", :day => "25"}, recognize_path("/blog/2004/12/25"))
935
+ assert_equal({:controller => "articles", :action => "edit", :id => "123"}, recognize_path("/blog/articles/edit/123"))
936
+ assert_equal({:controller => "articles", :action => "show_stats"}, recognize_path("/blog/articles/show_stats"))
937
+ assert_equal({:controller => "blog", :action => "unknown_request", :anything => ["blog", "wibble"]}, recognize_path("/blog/wibble"))
938
+ assert_equal({:controller => "blog", :action => "unknown_request", :anything => ["junk"]}, recognize_path("/junk"))
939
+
940
+ last_request = recognize_path("/blog/2006/07/28").freeze
941
+ assert_equal({:controller => "blog", :action => "show_date", :year => "2006", :month => "07", :day => "28"}, last_request)
942
+ assert_equal("/blog/2006/07/25", url_for({:day => 25}, last_request))
943
+ assert_equal("/blog/2005", url_for({:year => 2005}, last_request))
944
+ assert_equal("/blog/show/123", url_for({:action => "show" , :id => 123}, last_request))
945
+ assert_equal("/blog/2006", url_for({:year => 2006}, last_request))
946
+ assert_equal("/blog/2006", url_for({:year => 2006, :month => nil}, last_request))
947
+ end
948
+
949
+ private
950
+ def assert_uri_equal(expected, actual)
951
+ assert_equal(sort_query_string_params(expected), sort_query_string_params(actual))
952
+ end
953
+
954
+ def sort_query_string_params(uri)
955
+ path, qs = uri.split('?')
956
+ qs = qs.split('&').sort.join('&') if qs
957
+ qs ? "#{path}?#{qs}" : path
958
+ end
959
+ end