Froze rails gems
[depot.git] / vendor / rails / actionpack / test / controller / routing_test.rb
1 require 'abstract_unit'
2 require 'controller/fake_controllers'
3 require 'action_controller/routing'
4
5 class MilestonesController < ActionController::Base
6 def index() head :ok end
7 alias_method :show, :index
8 def rescue_action(e) raise e end
9 end
10
11 RunTimeTests = ARGV.include? 'time'
12 ROUTING = ActionController::Routing
13
14 class ROUTING::RouteBuilder
15 attr_reader :warn_output
16
17 def warn(msg)
18 (@warn_output ||= []) << msg
19 end
20 end
21
22 # See RFC 3986, section 3.3 for allowed path characters.
23 class UriReservedCharactersRoutingTest < Test::Unit::TestCase
24 def setup
25 ActionController::Routing.use_controllers! ['controller']
26 @set = ActionController::Routing::RouteSet.new
27 @set.draw do |map|
28 map.connect ':controller/:action/:variable/*additional'
29 end
30
31 safe, unsafe = %w(: @ & = + $ , ;), %w(^ / ? # [ ])
32 hex = unsafe.map { |char| '%' + char.unpack('H2').first.upcase }
33
34 @segment = "#{safe.join}#{unsafe.join}".freeze
35 @escaped = "#{safe.join}#{hex.join}".freeze
36 end
37
38 def test_route_generation_escapes_unsafe_path_characters
39 assert_equal "/contr#{@segment}oller/act#{@escaped}ion/var#{@escaped}iable/add#{@escaped}itional-1/add#{@escaped}itional-2",
40 @set.generate(:controller => "contr#{@segment}oller",
41 :action => "act#{@segment}ion",
42 :variable => "var#{@segment}iable",
43 :additional => ["add#{@segment}itional-1", "add#{@segment}itional-2"])
44 end
45
46 def test_route_recognition_unescapes_path_components
47 options = { :controller => "controller",
48 :action => "act#{@segment}ion",
49 :variable => "var#{@segment}iable",
50 :additional => ["add#{@segment}itional-1", "add#{@segment}itional-2"] }
51 assert_equal options, @set.recognize_path("/controller/act#{@escaped}ion/var#{@escaped}iable/add#{@escaped}itional-1/add#{@escaped}itional-2")
52 end
53
54 def test_route_generation_allows_passing_non_string_values_to_generated_helper
55 assert_equal "/controller/action/variable/1/2", @set.generate(:controller => "controller",
56 :action => "action",
57 :variable => "variable",
58 :additional => [1, 2])
59 end
60 end
61
62 class SegmentTest < Test::Unit::TestCase
63 def test_first_segment_should_interpolate_for_structure
64 s = ROUTING::Segment.new
65 def s.interpolation_statement(array) 'hello' end
66 assert_equal 'hello', s.continue_string_structure([])
67 end
68
69 def test_interpolation_statement
70 s = ROUTING::StaticSegment.new("Hello")
71 assert_equal "Hello", eval(s.interpolation_statement([]))
72 assert_equal "HelloHello", eval(s.interpolation_statement([s]))
73
74 s2 = ROUTING::StaticSegment.new("-")
75 assert_equal "Hello-Hello", eval(s.interpolation_statement([s, s2]))
76
77 s3 = ROUTING::StaticSegment.new("World")
78 assert_equal "Hello-World", eval(s3.interpolation_statement([s, s2]))
79 end
80 end
81
82 class StaticSegmentTest < Test::Unit::TestCase
83 def test_interpolation_chunk_should_respect_raw
84 s = ROUTING::StaticSegment.new('Hello World')
85 assert !s.raw?
86 assert_equal 'Hello%20World', s.interpolation_chunk
87
88 s = ROUTING::StaticSegment.new('Hello World', :raw => true)
89 assert s.raw?
90 assert_equal 'Hello World', s.interpolation_chunk
91 end
92
93 def test_regexp_chunk_should_escape_specials
94 s = ROUTING::StaticSegment.new('Hello*World')
95 assert_equal 'Hello\*World', s.regexp_chunk
96
97 s = ROUTING::StaticSegment.new('HelloWorld')
98 assert_equal 'HelloWorld', s.regexp_chunk
99 end
100
101 def test_regexp_chunk_should_add_question_mark_for_optionals
102 s = ROUTING::StaticSegment.new("/", :optional => true)
103 assert_equal "/?", s.regexp_chunk
104
105 s = ROUTING::StaticSegment.new("hello", :optional => true)
106 assert_equal "(?:hello)?", s.regexp_chunk
107 end
108 end
109
110 class DynamicSegmentTest < Test::Unit::TestCase
111 def segment(options = {})
112 unless @segment
113 @segment = ROUTING::DynamicSegment.new(:a, options)
114 end
115 @segment
116 end
117
118 def test_extract_value
119 s = ROUTING::DynamicSegment.new(:a)
120
121 hash = {:a => '10', :b => '20'}
122 assert_equal '10', eval(s.extract_value)
123
124 hash = {:b => '20'}
125 assert_equal nil, eval(s.extract_value)
126
127 s.default = '20'
128 assert_equal '20', eval(s.extract_value)
129 end
130
131 def test_default_local_name
132 assert_equal 'a_value', segment.local_name,
133 "Unexpected name -- all value_check tests will fail!"
134 end
135
136 def test_presence_value_check
137 a_value = 10
138 assert eval(segment.value_check)
139 end
140
141 def test_regexp_value_check_rejects_nil
142 segment = segment(:regexp => /\d+/)
143
144 a_value = nil
145 assert !eval(segment.value_check)
146 end
147
148 def test_optional_regexp_value_check_should_accept_nil
149 segment = segment(:regexp => /\d+/, :optional => true)
150
151 a_value = nil
152 assert eval(segment.value_check)
153 end
154
155 def test_regexp_value_check_rejects_no_match
156 segment = segment(:regexp => /\d+/)
157
158 a_value = "Hello20World"
159 assert !eval(segment.value_check)
160
161 a_value = "20Hi"
162 assert !eval(segment.value_check)
163 end
164
165 def test_regexp_value_check_accepts_match
166 segment = segment(:regexp => /\d+/)
167 a_value = "30"
168 assert eval(segment.value_check)
169 end
170
171 def test_value_check_fails_on_nil
172 a_value = nil
173 assert ! eval(segment.value_check)
174 end
175
176 def test_optional_value_needs_no_check
177 segment = segment(:optional => true)
178
179 a_value = nil
180 assert_equal nil, segment.value_check
181 end
182
183 def test_regexp_value_check_should_accept_match_with_default
184 segment = segment(:regexp => /\d+/, :default => '200')
185
186 a_value = '100'
187 assert eval(segment.value_check)
188 end
189
190 def test_expiry_should_not_trigger_once_expired
191 expired = true
192 hash = merged = {:a => 2, :b => 3}
193 options = {:b => 3}
194 expire_on = Hash.new { raise 'No!!!' }
195
196 eval(segment.expiry_statement)
197 rescue RuntimeError
198 flunk "Expiry check should not have occurred!"
199 end
200
201 def test_expiry_should_occur_according_to_expire_on
202 expired = false
203 hash = merged = {:a => 2, :b => 3}
204 options = {:b => 3}
205
206 expire_on = {:b => true, :a => false}
207 eval(segment.expiry_statement)
208 assert !expired
209 assert_equal({:a => 2, :b => 3}, hash)
210
211 expire_on = {:b => true, :a => true}
212 eval(segment.expiry_statement)
213 assert expired
214 assert_equal({:b => 3}, hash)
215 end
216
217 def test_extraction_code_should_return_on_nil
218 hash = merged = {:b => 3}
219 options = {:b => 3}
220 a_value = nil
221
222 # Local jump because of return inside eval.
223 assert_raises(LocalJumpError) { eval(segment.extraction_code) }
224 end
225
226 def test_extraction_code_should_return_on_mismatch
227 segment = segment(:regexp => /\d+/)
228 hash = merged = {:a => 'Hi', :b => '3'}
229 options = {:b => '3'}
230 a_value = nil
231
232 # Local jump because of return inside eval.
233 assert_raises(LocalJumpError) { eval(segment.extraction_code) }
234 end
235
236 def test_extraction_code_should_accept_value_and_set_local
237 hash = merged = {:a => 'Hi', :b => '3'}
238 options = {:b => '3'}
239 a_value = nil
240 expired = true
241
242 eval(segment.extraction_code)
243 assert_equal 'Hi', a_value
244 end
245
246 def test_extraction_should_work_without_value_check
247 segment.default = 'hi'
248 hash = merged = {:b => '3'}
249 options = {:b => '3'}
250 a_value = nil
251 expired = true
252
253 eval(segment.extraction_code)
254 assert_equal 'hi', a_value
255 end
256
257 def test_extraction_code_should_perform_expiry
258 expired = false
259 hash = merged = {:a => 'Hi', :b => '3'}
260 options = {:b => '3'}
261 expire_on = {:a => true}
262 a_value = nil
263
264 eval(segment.extraction_code)
265 assert_equal 'Hi', a_value
266 assert expired
267 assert_equal options, hash
268 end
269
270 def test_interpolation_chunk_should_replace_value
271 a_value = 'Hi'
272 assert_equal a_value, eval(%("#{segment.interpolation_chunk}"))
273 end
274
275 def test_interpolation_chunk_should_accept_nil
276 a_value = nil
277 assert_equal '', eval(%("#{segment.interpolation_chunk('a_value')}"))
278 end
279
280 def test_value_regexp_should_be_nil_without_regexp
281 assert_equal nil, segment.value_regexp
282 end
283
284 def test_value_regexp_should_match_exacly
285 segment = segment(:regexp => /\d+/)
286 assert_no_match segment.value_regexp, "Hello 10 World"
287 assert_no_match segment.value_regexp, "Hello 10"
288 assert_no_match segment.value_regexp, "10 World"
289 assert_match segment.value_regexp, "10"
290 end
291
292 def test_regexp_chunk_should_return_string
293 segment = segment(:regexp => /\d+/)
294 assert_kind_of String, segment.regexp_chunk
295 end
296
297 def test_build_pattern_non_optional_with_no_captures
298 # Non optional
299 a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /\d+/)
300 assert_equal "(\\d+)stuff", a_segment.build_pattern('stuff')
301 end
302
303 def test_build_pattern_non_optional_with_captures
304 # Non optional
305 a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /(\d+)(.*?)/)
306 assert_equal "((\\d+)(.*?))stuff", a_segment.build_pattern('stuff')
307 end
308
309 def test_optionality_implied
310 a_segment = ROUTING::DynamicSegment.new(:id)
311 assert a_segment.optionality_implied?
312
313 a_segment = ROUTING::DynamicSegment.new(:action)
314 assert a_segment.optionality_implied?
315 end
316
317 def test_modifiers_must_be_handled_sensibly
318 a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/i)
319 assert_equal "((?i-mx:david|jamis))stuff", a_segment.build_pattern('stuff')
320 a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/x)
321 assert_equal "((?x-mi:david|jamis))stuff", a_segment.build_pattern('stuff')
322 a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/)
323 assert_equal "(david|jamis)stuff", a_segment.build_pattern('stuff')
324 end
325 end
326
327 class ControllerSegmentTest < Test::Unit::TestCase
328 def test_regexp_should_only_match_possible_controllers
329 ActionController::Routing.with_controllers %w(admin/accounts admin/users account pages) do
330 cs = ROUTING::ControllerSegment.new :controller
331 regexp = %r{\A#{cs.regexp_chunk}\Z}
332
333 ActionController::Routing.possible_controllers.each do |name|
334 assert_match regexp, name
335 assert_no_match regexp, "#{name}_fake"
336
337 match = regexp.match name
338 assert_equal name, match[1]
339 end
340 end
341 end
342 end
343
344 class RouteBuilderTest < Test::Unit::TestCase
345 def builder
346 @builder ||= ROUTING::RouteBuilder.new
347 end
348
349 def build(path, options)
350 builder.build(path, options)
351 end
352
353 def test_options_should_not_be_modified
354 requirements1 = { :id => /\w+/, :controller => /(?:[a-z](?:-?[a-z]+)*)/ }
355 requirements2 = requirements1.dup
356
357 assert_equal requirements1, requirements2
358
359 with_options(:controller => 'folder',
360 :requirements => requirements2) do |m|
361 m.build 'folders/new', :action => 'new'
362 end
363
364 assert_equal requirements1, requirements2
365 end
366
367 def test_segment_for_static
368 segment, rest = builder.segment_for 'ulysses'
369 assert_equal '', rest
370 assert_kind_of ROUTING::StaticSegment, segment
371 assert_equal 'ulysses', segment.value
372 end
373
374 def test_segment_for_action
375 segment, rest = builder.segment_for ':action'
376 assert_equal '', rest
377 assert_kind_of ROUTING::DynamicSegment, segment
378 assert_equal :action, segment.key
379 assert_equal 'index', segment.default
380 end
381
382 def test_segment_for_dynamic
383 segment, rest = builder.segment_for ':login'
384 assert_equal '', rest
385 assert_kind_of ROUTING::DynamicSegment, segment
386 assert_equal :login, segment.key
387 assert_equal nil, segment.default
388 assert ! segment.optional?
389 end
390
391 def test_segment_for_with_rest
392 segment, rest = builder.segment_for ':login/:action'
393 assert_equal :login, segment.key
394 assert_equal '/:action', rest
395 segment, rest = builder.segment_for rest
396 assert_equal '/', segment.value
397 assert_equal ':action', rest
398 segment, rest = builder.segment_for rest
399 assert_equal :action, segment.key
400 assert_equal '', rest
401 end
402
403 def test_segments_for
404 segments = builder.segments_for_route_path '/:controller/:action/:id'
405
406 assert_kind_of ROUTING::DividerSegment, segments[0]
407 assert_equal '/', segments[2].value
408
409 assert_kind_of ROUTING::DynamicSegment, segments[1]
410 assert_equal :controller, segments[1].key
411
412 assert_kind_of ROUTING::DividerSegment, segments[2]
413 assert_equal '/', segments[2].value
414
415 assert_kind_of ROUTING::DynamicSegment, segments[3]
416 assert_equal :action, segments[3].key
417
418 assert_kind_of ROUTING::DividerSegment, segments[4]
419 assert_equal '/', segments[4].value
420
421 assert_kind_of ROUTING::DynamicSegment, segments[5]
422 assert_equal :id, segments[5].key
423 end
424
425 def test_segment_for_action
426 s, r = builder.segment_for(':action/something/else')
427 assert_equal '/something/else', r
428 assert_equal :action, s.key
429 end
430
431 def test_action_default_should_not_trigger_on_prefix
432 s, r = builder.segment_for ':action_name/something/else'
433 assert_equal '/something/else', r
434 assert_equal :action_name, s.key
435 assert_equal nil, s.default
436 end
437
438 def test_divide_route_options
439 segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
440 defaults, requirements = builder.divide_route_options(segments,
441 :action => 'buy', :person => /\w+/, :car => /\w+/,
442 :defaults => {:person => nil, :car => nil}
443 )
444
445 assert_equal({:action => 'buy', :person => nil, :car => nil}, defaults)
446 assert_equal({:person => /\w+/, :car => /\w+/}, requirements)
447 end
448
449 def test_assign_route_options
450 segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
451 defaults = {:action => 'buy', :person => nil, :car => nil}
452 requirements = {:person => /\w+/, :car => /\w+/}
453
454 route_requirements = builder.assign_route_options(segments, defaults, requirements)
455 assert_equal({}, route_requirements)
456
457 assert_equal :action, segments[3].key
458 assert_equal 'buy', segments[3].default
459
460 assert_equal :person, segments[5].key
461 assert_equal %r/\w+/, segments[5].regexp
462 assert segments[5].optional?
463
464 assert_equal :car, segments[7].key
465 assert_equal %r/\w+/, segments[7].regexp
466 assert segments[7].optional?
467 end
468
469 def test_assign_route_options_with_anchor_chars
470 segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
471 defaults = {:action => 'buy', :person => nil, :car => nil}
472 requirements = {:person => /\w+/, :car => /^\w+$/}
473
474 assert_raises ArgumentError do
475 route_requirements = builder.assign_route_options(segments, defaults, requirements)
476 end
477
478 requirements[:car] = /[^\/]+/
479 route_requirements = builder.assign_route_options(segments, defaults, requirements)
480 end
481
482 def test_optional_segments_preceding_required_segments
483 segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
484 defaults = {:action => 'buy', :person => nil, :car => "model-t"}
485 assert builder.assign_route_options(segments, defaults, {}).empty?
486
487 0.upto(1) { |i| assert !segments[i].optional?, "segment #{i} is optional and it shouldn't be" }
488 assert segments[2].optional?
489
490 assert_equal nil, builder.warn_output # should only warn on the :person segment
491 end
492
493 def test_segmentation_of_dot_path
494 segments = builder.segments_for_route_path '/books/:action.rss'
495 assert builder.assign_route_options(segments, {}, {}).empty?
496 assert_equal 6, segments.length # "/", "books", "/", ":action", ".", "rss"
497 assert !segments.any? { |seg| seg.optional? }
498 end
499
500 def test_segmentation_of_dynamic_dot_path
501 segments = builder.segments_for_route_path '/books/:action.:format'
502 assert builder.assign_route_options(segments, {}, {}).empty?
503 assert_equal 6, segments.length # "/", "books", "/", ":action", ".", ":format"
504 assert !segments.any? { |seg| seg.optional? }
505 assert_kind_of ROUTING::DynamicSegment, segments.last
506 end
507
508 def test_assignment_of_default_options
509 segments = builder.segments_for_route_path '/:controller/:action/:id/'
510 action, id = segments[-4], segments[-2]
511
512 assert_equal :action, action.key
513 assert_equal :id, id.key
514 assert ! action.optional?
515 assert ! id.optional?
516
517 builder.assign_default_route_options(segments)
518
519 assert_equal 'index', action.default
520 assert action.optional?
521 assert id.optional?
522 end
523
524 def test_assignment_of_default_options_respects_existing_defaults
525 segments = builder.segments_for_route_path '/:controller/:action/:id/'
526 action, id = segments[-4], segments[-2]
527
528 assert_equal :action, action.key
529 assert_equal :id, id.key
530 action.default = 'show'
531 action.is_optional = true
532
533 id.default = 'Welcome'
534 id.is_optional = true
535
536 builder.assign_default_route_options(segments)
537
538 assert_equal 'show', action.default
539 assert action.optional?
540 assert_equal 'Welcome', id.default
541 assert id.optional?
542 end
543
544 def test_assignment_of_default_options_respects_regexps
545 segments = builder.segments_for_route_path '/:controller/:action/:id/'
546 action = segments[-4]
547
548 assert_equal :action, action.key
549 segments[-4] = ROUTING::DynamicSegment.new(:action, :regexp => /show|in/)
550
551 builder.assign_default_route_options(segments)
552
553 assert_equal nil, action.default
554 assert ! action.optional?
555 end
556
557 def test_assignment_of_is_optional_when_default
558 segments = builder.segments_for_route_path '/books/:action.rss'
559 assert_equal segments[3].key, :action
560 segments[3].default = 'changes'
561 builder.ensure_required_segments(segments)
562 assert ! segments[3].optional?
563 end
564
565 def test_is_optional_is_assigned_to_default_segments
566 segments = builder.segments_for_route_path '/books/:action'
567 builder.assign_route_options(segments, {:action => 'index'}, {})
568
569 assert_equal segments[3].key, :action
570 assert segments[3].optional?
571 assert_kind_of ROUTING::DividerSegment, segments[2]
572 assert segments[2].optional?
573 end
574
575 # XXX is optional not being set right?
576 # /blah/:defaulted_segment <-- is the second slash optional? it should be.
577
578 def test_route_build
579 ActionController::Routing.with_controllers %w(users pages) do
580 r = builder.build '/:controller/:action/:id/', :action => nil
581
582 [0, 2, 4].each do |i|
583 assert_kind_of ROUTING::DividerSegment, r.segments[i]
584 assert_equal '/', r.segments[i].value
585 assert r.segments[i].optional? if i > 1
586 end
587
588 assert_kind_of ROUTING::DynamicSegment, r.segments[1]
589 assert_equal :controller, r.segments[1].key
590 assert_equal nil, r.segments[1].default
591
592 assert_kind_of ROUTING::DynamicSegment, r.segments[3]
593 assert_equal :action, r.segments[3].key
594 assert_equal 'index', r.segments[3].default
595
596 assert_kind_of ROUTING::DynamicSegment, r.segments[5]
597 assert_equal :id, r.segments[5].key
598 assert r.segments[5].optional?
599 end
600 end
601
602 def test_slashes_are_implied
603 routes = [
604 builder.build('/:controller/:action/:id/', :action => nil),
605 builder.build('/:controller/:action/:id', :action => nil),
606 builder.build(':controller/:action/:id', :action => nil),
607 builder.build('/:controller/:action/:id/', :action => nil)
608 ]
609 expected = routes.first.segments.length
610 routes.each_with_index do |route, i|
611 found = route.segments.length
612 assert_equal expected, found, "Route #{i + 1} has #{found} segments, expected #{expected}"
613 end
614 end
615 end
616
617 class RoutingTest < Test::Unit::TestCase
618 def test_possible_controllers
619 true_controller_paths = ActionController::Routing.controller_paths
620
621 ActionController::Routing.use_controllers! nil
622
623 silence_warnings do
624 Object.send(:const_set, :RAILS_ROOT, File.dirname(__FILE__) + '/controller_fixtures')
625 end
626
627 ActionController::Routing.controller_paths = [
628 RAILS_ROOT, RAILS_ROOT + '/app/controllers', RAILS_ROOT + '/vendor/plugins/bad_plugin/lib'
629 ]
630
631 assert_equal ["admin/user", "plugin", "user"], ActionController::Routing.possible_controllers.sort
632 ensure
633 if true_controller_paths
634 ActionController::Routing.controller_paths = true_controller_paths
635 end
636 ActionController::Routing.use_controllers! nil
637 Object.send(:remove_const, :RAILS_ROOT) rescue nil
638 end
639
640 def test_possible_controllers_are_reset_on_each_load
641 true_possible_controllers = ActionController::Routing.possible_controllers
642 true_controller_paths = ActionController::Routing.controller_paths
643
644 ActionController::Routing.use_controllers! nil
645 root = File.dirname(__FILE__) + '/controller_fixtures'
646
647 ActionController::Routing.controller_paths = []
648 assert_equal [], ActionController::Routing.possible_controllers
649
650 ActionController::Routing.controller_paths = [
651 root, root + '/app/controllers', root + '/vendor/plugins/bad_plugin/lib'
652 ]
653 ActionController::Routing::Routes.load!
654
655 assert_equal ["admin/user", "plugin", "user"], ActionController::Routing.possible_controllers.sort
656 ensure
657 ActionController::Routing.controller_paths = true_controller_paths
658 ActionController::Routing.use_controllers! true_possible_controllers
659 Object.send(:remove_const, :RAILS_ROOT) rescue nil
660
661 ActionController::Routing::Routes.clear!
662 ActionController::Routing::Routes.load_routes!
663 end
664
665 def test_with_controllers
666 c = %w(admin/accounts admin/users account pages)
667 ActionController::Routing.with_controllers c do
668 assert_equal c, ActionController::Routing.possible_controllers
669 end
670 end
671
672 def test_normalize_unix_paths
673 load_paths = %w(. config/../app/controllers config/../app//helpers script/../config/../vendor/rails/actionpack/lib vendor/rails/railties/builtin/rails_info app/models lib script/../config/../foo/bar/../../app/models .foo/../.bar foo.bar/../config)
674 paths = ActionController::Routing.normalize_paths(load_paths)
675 assert_equal %w(vendor/rails/railties/builtin/rails_info vendor/rails/actionpack/lib app/controllers app/helpers app/models config .bar lib .), paths
676 end
677
678 def test_normalize_windows_paths
679 load_paths = %w(. config\\..\\app\\controllers config\\..\\app\\\\helpers script\\..\\config\\..\\vendor\\rails\\actionpack\\lib vendor\\rails\\railties\\builtin\\rails_info app\\models lib script\\..\\config\\..\\foo\\bar\\..\\..\\app\\models .foo\\..\\.bar foo.bar\\..\\config)
680 paths = ActionController::Routing.normalize_paths(load_paths)
681 assert_equal %w(vendor\\rails\\railties\\builtin\\rails_info vendor\\rails\\actionpack\\lib app\\controllers app\\helpers app\\models config .bar lib .), paths
682 end
683
684 def test_routing_helper_module
685 assert_kind_of Module, ActionController::Routing::Helpers
686
687 h = ActionController::Routing::Helpers
688 c = Class.new
689 assert ! c.ancestors.include?(h)
690 ActionController::Routing::Routes.install_helpers c
691 assert c.ancestors.include?(h)
692 end
693 end
694
695 uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
696 class MockController
697 attr_accessor :routes
698
699 def initialize(routes)
700 self.routes = routes
701 end
702
703 def url_for(options)
704 only_path = options.delete(:only_path)
705
706 port = options.delete(:port) || 80
707 port_string = port == 80 ? '' : ":#{port}"
708
709 protocol = options.delete(:protocol) || "http"
710 host = options.delete(:host) || "named.route.test"
711 anchor = "##{options.delete(:anchor)}" if options.key?(:anchor)
712
713 path = routes.generate(options)
714
715 only_path ? "#{path}#{anchor}" : "#{protocol}://#{host}#{port_string}#{path}#{anchor}"
716 end
717
718 def request
719 @request ||= MockRequest.new(:host => "named.route.test", :method => :get)
720 end
721 end
722
723 class MockRequest
724 attr_accessor :path, :path_parameters, :host, :subdomains, :domain, :method
725
726 def initialize(values={})
727 values.each { |key, value| send("#{key}=", value) }
728 if values[:host]
729 subdomain, self.domain = values[:host].split(/\./, 2)
730 self.subdomains = [subdomain]
731 end
732 end
733
734 def protocol
735 "http://"
736 end
737
738 def host_with_port
739 (subdomains * '.') + '.' + domain
740 end
741 end
742
743 class LegacyRouteSetTests < Test::Unit::TestCase
744 attr_reader :rs
745
746 def setup
747 # These tests assume optimisation is on, so re-enable it.
748 ActionController::Base.optimise_named_routes = true
749
750 @rs = ::ActionController::Routing::RouteSet.new
751 @rs.draw {|m| m.connect ':controller/:action/:id' }
752
753 ActionController::Routing.use_controllers! %w(content admin/user admin/news_feed)
754 end
755
756 def test_default_setup
757 assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content"))
758 assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list"))
759 assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10"))
760
761 assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, rs.recognize_path("/admin/user/show/10"))
762
763 assert_equal '/admin/user/show/10', rs.generate(:controller => 'admin/user', :action => 'show', :id => 10)
764
765 assert_equal '/admin/user/show', rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
766 assert_equal '/admin/user/list/10', rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
767
768 assert_equal '/admin/stuff', rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
769 assert_equal '/stuff', rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
770 end
771
772 def test_ignores_leading_slash
773 @rs.draw {|m| m.connect '/:controller/:action/:id'}
774 test_default_setup
775 end
776
777 def test_time_recognition
778 # We create many routes to make situation more realistic
779 @rs = ::ActionController::Routing::RouteSet.new
780 @rs.draw { |map|
781 map.frontpage '', :controller => 'search', :action => 'new'
782 map.resources :videos do |video|
783 video.resources :comments
784 video.resource :file, :controller => 'video_file'
785 video.resource :share, :controller => 'video_shares'
786 video.resource :abuse, :controller => 'video_abuses'
787 end
788 map.resources :abuses, :controller => 'video_abuses'
789 map.resources :video_uploads
790 map.resources :video_visits
791
792 map.resources :users do |user|
793 user.resource :settings
794 user.resources :videos
795 end
796 map.resources :channels do |channel|
797 channel.resources :videos, :controller => 'channel_videos'
798 end
799 map.resource :session
800 map.resource :lost_password
801 map.search 'search', :controller => 'search'
802 map.resources :pages
803 map.connect ':controller/:action/:id'
804 }
805 n = 1000
806 if RunTimeTests
807 GC.start
808 rectime = Benchmark.realtime do
809 n.times do
810 rs.recognize_path("/videos/1234567", {:method => :get})
811 rs.recognize_path("/videos/1234567/abuse", {:method => :get})
812 rs.recognize_path("/users/1234567/settings", {:method => :get})
813 rs.recognize_path("/channels/1234567", {:method => :get})
814 rs.recognize_path("/session/new", {:method => :get})
815 rs.recognize_path("/admin/user/show/10", {:method => :get})
816 end
817 end
818 puts "\n\nRecognition (#{rs.routes.size} routes):"
819 per_url = rectime / (n * 6)
820 puts "#{per_url * 1000} ms/url"
821 puts "#{1 / per_url} url/s\n\n"
822 end
823 end
824
825 def test_time_generation
826 n = 5000
827 if RunTimeTests
828 GC.start
829 pairs = [
830 [{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}],
831 [{:controller => 'content'}, {:controller => 'content', :action => 'index'}],
832 [{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}],
833 [{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}],
834 [{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}],
835 [{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}],
836 [{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}],
837 [{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}],
838 ]
839 p = nil
840 gentime = Benchmark.realtime do
841 n.times do
842 pairs.each {|(a, b)| rs.generate(a, b)}
843 end
844 end
845
846 puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)"
847 per_url = gentime / (n * 8)
848 puts "#{per_url * 1000} ms/url"
849 puts "#{1 / per_url} url/s\n\n"
850 end
851 end
852
853 def test_route_with_colon_first
854 rs.draw do |map|
855 map.connect '/:controller/:action/:id', :action => 'index', :id => nil
856 map.connect ':url', :controller => 'tiny_url', :action => 'translate'
857 end
858 end
859
860 def test_route_with_regexp_for_controller
861 rs.draw do |map|
862 map.connect ':controller/:admintoken/:action/:id', :controller => /admin\/.+/
863 map.connect ':controller/:action/:id'
864 end
865 assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"},
866 rs.recognize_path("/admin/user/foo"))
867 assert_equal({:controller => "content", :action => "foo"}, rs.recognize_path("/content/foo"))
868 assert_equal '/admin/user/foo', rs.generate(:controller => "admin/user", :admintoken => "foo", :action => "index")
869 assert_equal '/content/foo', rs.generate(:controller => "content", :action => "foo")
870 end
871
872 def test_route_with_regexp_and_dot
873 rs.draw do |map|
874 map.connect ':controller/:action/:file',
875 :controller => /admin|user/,
876 :action => /upload|download/,
877 :defaults => {:file => nil},
878 :requirements => {:file => %r{[^/]+(\.[^/]+)?}}
879 end
880 # Without a file extension
881 assert_equal '/user/download/file',
882 rs.generate(:controller => "user", :action => "download", :file => "file")
883 assert_equal(
884 {:controller => "user", :action => "download", :file => "file"},
885 rs.recognize_path("/user/download/file"))
886
887 # Now, let's try a file with an extension, really a dot (.)
888 assert_equal '/user/download/file.jpg',
889 rs.generate(
890 :controller => "user", :action => "download", :file => "file.jpg")
891 assert_equal(
892 {:controller => "user", :action => "download", :file => "file.jpg"},
893 rs.recognize_path("/user/download/file.jpg"))
894 end
895
896 def test_basic_named_route
897 rs.add_named_route :home, '', :controller => 'content', :action => 'list'
898 x = setup_for_named_route
899 assert_equal("http://named.route.test/",
900 x.send(:home_url))
901 end
902
903 def test_basic_named_route_with_relative_url_root
904 rs.add_named_route :home, '', :controller => 'content', :action => 'list'
905 x = setup_for_named_route
906 ActionController::Base.relative_url_root = "/foo"
907 assert_equal("http://named.route.test/foo/",
908 x.send(:home_url))
909 assert_equal "/foo/", x.send(:home_path)
910 ActionController::Base.relative_url_root = nil
911 end
912
913 def test_named_route_with_option
914 rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page'
915 x = setup_for_named_route
916 assert_equal("http://named.route.test/page/new%20stuff",
917 x.send(:page_url, :title => 'new stuff'))
918 end
919
920 def test_named_route_with_default
921 rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage'
922 x = setup_for_named_route
923 assert_equal("http://named.route.test/page/AboutRails",
924 x.send(:page_url, :title => "AboutRails"))
925
926 end
927
928 def test_named_route_with_name_prefix
929 rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :name_prefix => 'my_'
930 x = setup_for_named_route
931 assert_equal("http://named.route.test/page",
932 x.send(:my_page_url))
933 end
934
935 def test_named_route_with_path_prefix
936 rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :path_prefix => 'my'
937 x = setup_for_named_route
938 assert_equal("http://named.route.test/my/page",
939 x.send(:page_url))
940 end
941
942 def test_named_route_with_nested_controller
943 rs.add_named_route :users, 'admin/user', :controller => 'admin/user', :action => 'index'
944 x = setup_for_named_route
945 assert_equal("http://named.route.test/admin/user",
946 x.send(:users_url))
947 end
948
949 def test_optimised_named_route_call_never_uses_url_for
950 rs.add_named_route :users, 'admin/user', :controller => '/admin/user', :action => 'index'
951 rs.add_named_route :user, 'admin/user/:id', :controller=>'/admin/user', :action=>'show'
952 x = setup_for_named_route
953 x.expects(:url_for).never
954 x.send(:users_url)
955 x.send(:users_path)
956 x.send(:user_url, 2, :foo=>"bar")
957 x.send(:user_path, 3, :bar=>"foo")
958 end
959
960 def test_optimised_named_route_with_host
961 rs.add_named_route :pages, 'pages', :controller => 'content', :action => 'show_page', :host => 'foo.com'
962 x = setup_for_named_route
963 x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages).once
964 x.send(:pages_url)
965 end
966
967 def setup_for_named_route
968 klass = Class.new(MockController)
969 rs.install_helpers(klass)
970 klass.new(rs)
971 end
972
973 def test_named_route_without_hash
974 rs.draw do |map|
975 map.normal ':controller/:action/:id'
976 end
977 end
978
979 def test_named_route_root
980 rs.draw do |map|
981 map.root :controller => "hello"
982 end
983 x = setup_for_named_route
984 assert_equal("http://named.route.test/", x.send(:root_url))
985 assert_equal("/", x.send(:root_path))
986 end
987
988 def test_named_route_with_regexps
989 rs.draw do |map|
990 map.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show',
991 :year => /\d+/, :month => /\d+/, :day => /\d+/
992 map.connect ':controller/:action/:id'
993 end
994 x = setup_for_named_route
995 # assert_equal(
996 # {:controller => 'page', :action => 'show', :title => 'hi', :use_route => :article, :only_path => false},
997 # x.send(:article_url, :title => 'hi')
998 # )
999 assert_equal(
1000 "http://named.route.test/page/2005/6/10/hi",
1001 x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
1002 )
1003 end
1004
1005 def test_changing_controller
1006 assert_equal '/admin/stuff/show/10', rs.generate(
1007 {:controller => 'stuff', :action => 'show', :id => 10},
1008 {:controller => 'admin/user', :action => 'index'}
1009 )
1010 end
1011
1012 def test_paths_escaped
1013 rs.draw do |map|
1014 map.path 'file/*path', :controller => 'content', :action => 'show_file'
1015 map.connect ':controller/:action/:id'
1016 end
1017
1018 # No + to space in URI escaping, only for query params.
1019 results = rs.recognize_path "/file/hello+world/how+are+you%3F"
1020 assert results, "Recognition should have succeeded"
1021 assert_equal ['hello+world', 'how+are+you?'], results[:path]
1022
1023 # Use %20 for space instead.
1024 results = rs.recognize_path "/file/hello%20world/how%20are%20you%3F"
1025 assert results, "Recognition should have succeeded"
1026 assert_equal ['hello world', 'how are you?'], results[:path]
1027
1028 results = rs.recognize_path "/file"
1029 assert results, "Recognition should have succeeded"
1030 assert_equal [], results[:path]
1031 end
1032
1033 def test_paths_slashes_unescaped_with_ordered_parameters
1034 rs.add_named_route :path, '/file/*path', :controller => 'content'
1035
1036 # No / to %2F in URI, only for query params.
1037 x = setup_for_named_route
1038 assert_equal("/file/hello/world", x.send(:path_path, 'hello/world'))
1039 end
1040
1041 def test_non_controllers_cannot_be_matched
1042 rs.draw do |map|
1043 map.connect ':controller/:action/:id'
1044 end
1045 assert_raises(ActionController::RoutingError) { rs.recognize_path("/not_a/show/10") }
1046 end
1047
1048 def test_paths_do_not_accept_defaults
1049 assert_raises(ActionController::RoutingError) do
1050 rs.draw do |map|
1051 map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default)
1052 map.connect ':controller/:action/:id'
1053 end
1054 end
1055
1056 rs.draw do |map|
1057 map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => []
1058 map.connect ':controller/:action/:id'
1059 end
1060 end
1061
1062 def test_should_list_options_diff_when_routing_requirements_dont_match
1063 rs.draw do |map|
1064 map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
1065 end
1066 exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post") }
1067 assert_match /^post_url failed to generate/, exception.message
1068 from_match = exception.message.match(/from \{[^\}]+\}/).to_s
1069 assert_match /:bad_param=>"foo"/, from_match
1070 assert_match /:action=>"show"/, from_match
1071 assert_match /:controller=>"post"/, from_match
1072
1073 expected_match = exception.message.match(/expected: \{[^\}]+\}/).to_s
1074 assert_no_match /:bad_param=>"foo"/, expected_match
1075 assert_match /:action=>"show"/, expected_match
1076 assert_match /:controller=>"post"/, expected_match
1077
1078 diff_match = exception.message.match(/diff: \{[^\}]+\}/).to_s
1079 assert_match /:bad_param=>"foo"/, diff_match
1080 assert_no_match /:action=>"show"/, diff_match
1081 assert_no_match /:controller=>"post"/, diff_match
1082 end
1083
1084 # this specifies the case where your formerly would get a very confusing error message with an empty diff
1085 def test_should_have_better_error_message_when_options_diff_is_empty
1086 rs.draw do |map|
1087 map.content '/content/:query', :controller => 'content', :action => 'show'
1088 end
1089
1090 exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'content', :action => 'show', :use_route => "content") }
1091 assert_match %r[:action=>"show"], exception.message
1092 assert_match %r[:controller=>"content"], exception.message
1093 assert_match %r[you may have ambiguous routes, or you may need to supply additional parameters for this route], exception.message
1094 assert_match %r[content_url has the following required parameters: \["content", :query\] - are they all satisfied?], exception.message
1095 end
1096
1097 def test_dynamic_path_allowed
1098 rs.draw do |map|
1099 map.connect '*path', :controller => 'content', :action => 'show_file'
1100 end
1101
1102 assert_equal '/pages/boo', rs.generate(:controller => 'content', :action => 'show_file', :path => %w(pages boo))
1103 end
1104
1105 def test_dynamic_recall_paths_allowed
1106 rs.draw do |map|
1107 map.connect '*path', :controller => 'content', :action => 'show_file'
1108 end
1109
1110 recall_path = ActionController::Routing::PathSegment::Result.new(%w(pages boo))
1111 assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => recall_path)
1112 end
1113
1114 def test_backwards
1115 rs.draw do |map|
1116 map.connect 'page/:id/:action', :controller => 'pages', :action => 'show'
1117 map.connect ':controller/:action/:id'
1118 end
1119
1120 assert_equal '/page/20', rs.generate({:id => 20}, {:controller => 'pages', :action => 'show'})
1121 assert_equal '/page/20', rs.generate(:controller => 'pages', :id => 20, :action => 'show')
1122 assert_equal '/pages/boo', rs.generate(:controller => 'pages', :action => 'boo')
1123 end
1124
1125 def test_route_with_fixnum_default
1126 rs.draw do |map|
1127 map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
1128 map.connect ':controller/:action/:id'
1129 end
1130
1131 assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page')
1132 assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => 1)
1133 assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => '1')
1134 assert_equal '/page/10', rs.generate(:controller => 'content', :action => 'show_page', :id => 10)
1135
1136 assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page"))
1137 assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page/1"))
1138 assert_equal({:controller => "content", :action => 'show_page', :id => '10'}, rs.recognize_path("/page/10"))
1139 end
1140
1141 # For newer revision
1142 def test_route_with_text_default
1143 rs.draw do |map|
1144 map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
1145 map.connect ':controller/:action/:id'
1146 end
1147
1148 assert_equal '/page/foo', rs.generate(:controller => 'content', :action => 'show_page', :id => 'foo')
1149 assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, rs.recognize_path("/page/foo"))
1150
1151 token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian
1152 escaped_token = CGI::escape(token)
1153
1154 assert_equal '/page/' + escaped_token, rs.generate(:controller => 'content', :action => 'show_page', :id => token)
1155 assert_equal({:controller => "content", :action => 'show_page', :id => token}, rs.recognize_path("/page/#{escaped_token}"))
1156 end
1157
1158 def test_action_expiry
1159 assert_equal '/content', rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
1160 end
1161
1162 def test_recognition_with_uppercase_controller_name
1163 assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/Content"))
1164 assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/ConTent/list"))
1165 assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/CONTENT/show/10"))
1166
1167 # these used to work, before the routes rewrite, but support for this was pulled in the new version...
1168 #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/NewsFeed"))
1169 #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/News_Feed"))
1170 end
1171
1172 def test_requirement_should_prevent_optional_id
1173 rs.draw do |map|
1174 map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
1175 end
1176
1177 assert_equal '/post/10', rs.generate(:controller => 'post', :action => 'show', :id => 10)
1178
1179 assert_raises ActionController::RoutingError do
1180 rs.generate(:controller => 'post', :action => 'show')
1181 end
1182 end
1183
1184 def test_both_requirement_and_optional
1185 rs.draw do |map|
1186 map.blog('test/:year', :controller => 'post', :action => 'show',
1187 :defaults => { :year => nil },
1188 :requirements => { :year => /\d{4}/ }
1189 )
1190 map.connect ':controller/:action/:id'
1191 end
1192
1193 assert_equal '/test', rs.generate(:controller => 'post', :action => 'show')
1194 assert_equal '/test', rs.generate(:controller => 'post', :action => 'show', :year => nil)
1195
1196 x = setup_for_named_route
1197 assert_equal("http://named.route.test/test",
1198 x.send(:blog_url))
1199 end
1200
1201 def test_set_to_nil_forgets
1202 rs.draw do |map|
1203 map.connect 'pages/:year/:month/:day', :controller => 'content', :action => 'list_pages', :month => nil, :day => nil
1204 map.connect ':controller/:action/:id'
1205 end
1206
1207 assert_equal '/pages/2005',
1208 rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005)
1209 assert_equal '/pages/2005/6',
1210 rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6)
1211 assert_equal '/pages/2005/6/12',
1212 rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12)
1213
1214 assert_equal '/pages/2005/6/4',
1215 rs.generate({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
1216
1217 assert_equal '/pages/2005/6',
1218 rs.generate({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
1219
1220 assert_equal '/pages/2005',
1221 rs.generate({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
1222 end
1223
1224 def test_url_with_no_action_specified
1225 rs.draw do |map|
1226 map.connect '', :controller => 'content'
1227 map.connect ':controller/:action/:id'
1228 end
1229
1230 assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
1231 assert_equal '/', rs.generate(:controller => 'content')
1232 end
1233
1234 def test_named_url_with_no_action_specified
1235 rs.draw do |map|
1236 map.home '', :controller => 'content'
1237 map.connect ':controller/:action/:id'
1238 end
1239
1240 assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
1241 assert_equal '/', rs.generate(:controller => 'content')
1242
1243 x = setup_for_named_route
1244 assert_equal("http://named.route.test/",
1245 x.send(:home_url))
1246 end
1247
1248 def test_url_generated_when_forgetting_action
1249 [{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash|
1250 rs.draw do |map|
1251 map.home '', hash
1252 map.connect ':controller/:action/:id'
1253 end
1254 assert_equal '/', rs.generate({:action => nil}, {:controller => 'content', :action => 'hello'})
1255 assert_equal '/', rs.generate({:controller => 'content'})
1256 assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
1257 end
1258 end
1259
1260 def test_named_route_method
1261 rs.draw do |map|
1262 map.categories 'categories', :controller => 'content', :action => 'categories'
1263 map.connect ':controller/:action/:id'
1264 end
1265
1266 assert_equal '/categories', rs.generate(:controller => 'content', :action => 'categories')
1267 assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
1268 end
1269
1270 def test_named_routes_array
1271 test_named_route_method
1272 assert_equal [:categories], rs.named_routes.names
1273 end
1274
1275 def test_nil_defaults
1276 rs.draw do |map|
1277 map.connect 'journal',
1278 :controller => 'content',
1279 :action => 'list_journal',
1280 :date => nil, :user_id => nil
1281 map.connect ':controller/:action/:id'
1282 end
1283
1284 assert_equal '/journal', rs.generate(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil)
1285 end
1286
1287 def setup_request_method_routes_for(method)
1288 @request = ActionController::TestRequest.new
1289 @request.env["REQUEST_METHOD"] = method
1290 @request.request_uri = "/match"
1291
1292 rs.draw do |r|
1293 r.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get }
1294 r.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post }
1295 r.connect '/match', :controller => 'books', :action => 'put', :conditions => { :method => :put }
1296 r.connect '/match', :controller => 'books', :action => 'delete', :conditions => { :method => :delete }
1297 end
1298 end
1299
1300 %w(GET POST PUT DELETE).each do |request_method|
1301 define_method("test_request_method_recognized_with_#{request_method}") do
1302 begin
1303 Object.const_set(:BooksController, Class.new(ActionController::Base))
1304
1305 setup_request_method_routes_for(request_method)
1306
1307 assert_nothing_raised { rs.recognize(@request) }
1308 assert_equal request_method.downcase, @request.path_parameters[:action]
1309 ensure
1310 Object.send(:remove_const, :BooksController) rescue nil
1311 end
1312 end
1313 end
1314
1315 def test_recognize_array_of_methods
1316 Object.const_set(:BooksController, Class.new(ActionController::Base))
1317 rs.draw do |r|
1318 r.connect '/match', :controller => 'books', :action => 'get_or_post', :conditions => { :method => [:get, :post] }
1319 r.connect '/match', :controller => 'books', :action => 'not_get_or_post'
1320 end
1321
1322 @request = ActionController::TestRequest.new
1323 @request.env["REQUEST_METHOD"] = 'POST'
1324 @request.request_uri = "/match"
1325 assert_nothing_raised { rs.recognize(@request) }
1326 assert_equal 'get_or_post', @request.path_parameters[:action]
1327
1328 # have to recreate or else the RouteSet uses a cached version:
1329 @request = ActionController::TestRequest.new
1330 @request.env["REQUEST_METHOD"] = 'PUT'
1331 @request.request_uri = "/match"
1332 assert_nothing_raised { rs.recognize(@request) }
1333 assert_equal 'not_get_or_post', @request.path_parameters[:action]
1334 ensure
1335 Object.send(:remove_const, :BooksController) rescue nil
1336 end
1337
1338 def test_subpath_recognized
1339 Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
1340
1341 rs.draw do |r|
1342 r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
1343 r.connect '/items/:id/:action', :controller => 'subpath_books'
1344 r.connect '/posts/new/:action', :controller => 'subpath_books'
1345 r.connect '/posts/:id', :controller => 'subpath_books', :action => "show"
1346 end
1347
1348 hash = rs.recognize_path "/books/17/edit"
1349 assert_not_nil hash
1350 assert_equal %w(subpath_books 17 edit), [hash[:controller], hash[:id], hash[:action]]
1351
1352 hash = rs.recognize_path "/items/3/complete"
1353 assert_not_nil hash
1354 assert_equal %w(subpath_books 3 complete), [hash[:controller], hash[:id], hash[:action]]
1355
1356 hash = rs.recognize_path "/posts/new/preview"
1357 assert_not_nil hash
1358 assert_equal %w(subpath_books preview), [hash[:controller], hash[:action]]
1359
1360 hash = rs.recognize_path "/posts/7"
1361 assert_not_nil hash
1362 assert_equal %w(subpath_books show 7), [hash[:controller], hash[:action], hash[:id]]
1363 ensure
1364 Object.send(:remove_const, :SubpathBooksController) rescue nil
1365 end
1366
1367 def test_subpath_generated
1368 Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
1369
1370 rs.draw do |r|
1371 r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
1372 r.connect '/items/:id/:action', :controller => 'subpath_books'
1373 r.connect '/posts/new/:action', :controller => 'subpath_books'
1374 end
1375
1376 assert_equal "/books/7/edit", rs.generate(:controller => "subpath_books", :id => 7, :action => "edit")
1377 assert_equal "/items/15/complete", rs.generate(:controller => "subpath_books", :id => 15, :action => "complete")
1378 assert_equal "/posts/new/preview", rs.generate(:controller => "subpath_books", :action => "preview")
1379 ensure
1380 Object.send(:remove_const, :SubpathBooksController) rescue nil
1381 end
1382
1383 def test_failed_requirements_raises_exception_with_violated_requirements
1384 rs.draw do |r|
1385 r.foo_with_requirement 'foos/:id', :controller=>'foos', :requirements=>{:id=>/\d+/}
1386 end
1387
1388 x = setup_for_named_route
1389 assert_raises(ActionController::RoutingError) do
1390 x.send(:foo_with_requirement_url, "I am Against the requirements")
1391 end
1392 end
1393
1394 def test_routes_changed_correctly_after_clear
1395 ActionController::Base.optimise_named_routes = true
1396 rs = ::ActionController::Routing::RouteSet.new
1397 rs.draw do |r|
1398 r.connect 'ca', :controller => 'ca', :action => "aa"
1399 r.connect 'cb', :controller => 'cb', :action => "ab"
1400 r.connect 'cc', :controller => 'cc', :action => "ac"
1401 r.connect ':controller/:action/:id'
1402 r.connect ':controller/:action/:id.:format'
1403 end
1404
1405 hash = rs.recognize_path "/cc"
1406
1407 assert_not_nil hash
1408 assert_equal %w(cc ac), [hash[:controller], hash[:action]]
1409
1410 rs.draw do |r|
1411 r.connect 'cb', :controller => 'cb', :action => "ab"
1412 r.connect 'cc', :controller => 'cc', :action => "ac"
1413 r.connect ':controller/:action/:id'
1414 r.connect ':controller/:action/:id.:format'
1415 end
1416
1417 hash = rs.recognize_path "/cc"
1418
1419 assert_not_nil hash
1420 assert_equal %w(cc ac), [hash[:controller], hash[:action]]
1421
1422 end
1423 end
1424
1425 class RouteTest < Test::Unit::TestCase
1426 def setup
1427 @route = ROUTING::Route.new
1428 end
1429
1430 def slash_segment(is_optional = false)
1431 ROUTING::DividerSegment.new('/', :optional => is_optional)
1432 end
1433
1434 def default_route
1435 unless defined?(@default_route)
1436 segments = []
1437 segments << ROUTING::StaticSegment.new('/', :raw => true)
1438 segments << ROUTING::DynamicSegment.new(:controller)
1439 segments << slash_segment(:optional)
1440 segments << ROUTING::DynamicSegment.new(:action, :default => 'index', :optional => true)
1441 segments << slash_segment(:optional)
1442 segments << ROUTING::DynamicSegment.new(:id, :optional => true)
1443 segments << slash_segment(:optional)
1444 @default_route = ROUTING::Route.new(segments).freeze
1445 end
1446 @default_route
1447 end
1448
1449 def test_default_route_recognition
1450 expected = {:controller => 'accounts', :action => 'show', :id => '10'}
1451 assert_equal expected, default_route.recognize('/accounts/show/10')
1452 assert_equal expected, default_route.recognize('/accounts/show/10/')
1453
1454 expected[:id] = 'jamis'
1455 assert_equal expected, default_route.recognize('/accounts/show/jamis/')
1456
1457 expected.delete :id
1458 assert_equal expected, default_route.recognize('/accounts/show')
1459 assert_equal expected, default_route.recognize('/accounts/show/')
1460
1461 expected[:action] = 'index'
1462 assert_equal expected, default_route.recognize('/accounts/')
1463 assert_equal expected, default_route.recognize('/accounts')
1464
1465 assert_equal nil, default_route.recognize('/')
1466 assert_equal nil, default_route.recognize('/accounts/how/goood/it/is/to/be/free')
1467 end
1468
1469 def test_default_route_should_omit_default_action
1470 o = {:controller => 'accounts', :action => 'index'}
1471 assert_equal '/accounts', default_route.generate(o, o, {})
1472 end
1473
1474 def test_default_route_should_include_default_action_when_id_present
1475 o = {:controller => 'accounts', :action => 'index', :id => '20'}
1476 assert_equal '/accounts/index/20', default_route.generate(o, o, {})
1477 end
1478
1479 def test_default_route_should_work_with_action_but_no_id
1480 o = {:controller => 'accounts', :action => 'list_all'}
1481 assert_equal '/accounts/list_all', default_route.generate(o, o, {})
1482 end
1483
1484 def test_default_route_should_uri_escape_pluses
1485 expected = { :controller => 'accounts', :action => 'show', :id => 'hello world' }
1486 assert_equal expected, default_route.recognize('/accounts/show/hello world')
1487 assert_equal expected, default_route.recognize('/accounts/show/hello%20world')
1488 assert_equal '/accounts/show/hello%20world', default_route.generate(expected, expected, {})
1489
1490 expected[:id] = 'hello+world'
1491 assert_equal expected, default_route.recognize('/accounts/show/hello+world')
1492 assert_equal expected, default_route.recognize('/accounts/show/hello%2Bworld')
1493 assert_equal '/accounts/show/hello+world', default_route.generate(expected, expected, {})
1494 end
1495
1496 def test_matches_controller_and_action
1497 # requirement_for should only be called for the action and controller _once_
1498 @route.expects(:requirement_for).with(:controller).times(1).returns('pages')
1499 @route.expects(:requirement_for).with(:action).times(1).returns('show')
1500
1501 @route.requirements = {:controller => 'pages', :action => 'show'}
1502 assert @route.matches_controller_and_action?('pages', 'show')
1503 assert !@route.matches_controller_and_action?('not_pages', 'show')
1504 assert !@route.matches_controller_and_action?('pages', 'not_show')
1505 end
1506
1507 def test_parameter_shell
1508 page_url = ROUTING::Route.new
1509 page_url.requirements = {:controller => 'pages', :action => 'show', :id => /\d+/}
1510 assert_equal({:controller => 'pages', :action => 'show'}, page_url.parameter_shell)
1511 end
1512
1513 def test_defaults
1514 route = ROUTING::RouteBuilder.new.build '/users/:id.:format', :controller => "users", :action => "show", :format => "html"
1515 assert_equal(
1516 { :controller => "users", :action => "show", :format => "html" },
1517 route.defaults)
1518 end
1519
1520 def test_builder_complains_without_controller
1521 assert_raises(ArgumentError) do
1522 ROUTING::RouteBuilder.new.build '/contact', :contoller => "contact", :action => "index"
1523 end
1524 end
1525
1526 def test_significant_keys_for_default_route
1527 keys = default_route.significant_keys.sort_by {|k| k.to_s }
1528 assert_equal [:action, :controller, :id], keys
1529 end
1530
1531 def test_significant_keys
1532 segments = []
1533 segments << ROUTING::StaticSegment.new('/', :raw => true)
1534 segments << ROUTING::StaticSegment.new('user')
1535 segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
1536 segments << ROUTING::DynamicSegment.new(:user)
1537 segments << ROUTING::StaticSegment.new('/', :raw => true, :optional => true)
1538
1539 requirements = {:controller => 'users', :action => 'show'}
1540
1541 user_url = ROUTING::Route.new(segments, requirements)
1542 keys = user_url.significant_keys.sort_by { |k| k.to_s }
1543 assert_equal [:action, :controller, :user], keys
1544 end
1545
1546 def test_build_empty_query_string
1547 assert_equal '', @route.build_query_string({})
1548 end
1549
1550 def test_build_query_string_with_nil_value
1551 assert_equal '', @route.build_query_string({:x => nil})
1552 end
1553
1554 def test_simple_build_query_string
1555 assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => '1', :y => '2'))
1556 end
1557
1558 def test_convert_ints_build_query_string
1559 assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => 1, :y => 2))
1560 end
1561
1562 def test_escape_spaces_build_query_string
1563 assert_equal '?x=hello+world&y=goodbye+world', order_query_string(@route.build_query_string(:x => 'hello world', :y => 'goodbye world'))
1564 end
1565
1566 def test_expand_array_build_query_string
1567 assert_equal '?x%5B%5D=1&x%5B%5D=2', order_query_string(@route.build_query_string(:x => [1, 2]))
1568 end
1569
1570 def test_escape_spaces_build_query_string_selected_keys
1571 assert_equal '?x=hello+world', order_query_string(@route.build_query_string({:x => 'hello world', :y => 'goodbye world'}, [:x]))
1572 end
1573
1574 private
1575 def order_query_string(qs)
1576 '?' + qs[1..-1].split('&').sort.join('&')
1577 end
1578 end
1579
1580 class RouteSetTest < Test::Unit::TestCase
1581 def set
1582 @set ||= ROUTING::RouteSet.new
1583 end
1584
1585 def request
1586 @request ||= MockRequest.new(:host => "named.routes.test", :method => :get)
1587 end
1588
1589 def test_generate_extras
1590 set.draw { |m| m.connect ':controller/:action/:id' }
1591 path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
1592 assert_equal "/foo/bar/15", path
1593 assert_equal %w(that this), extras.map(&:to_s).sort
1594 end
1595
1596 def test_extra_keys
1597 set.draw { |m| m.connect ':controller/:action/:id' }
1598 extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
1599 assert_equal %w(that this), extras.map(&:to_s).sort
1600 end
1601
1602 def test_generate_extras_not_first
1603 set.draw do |map|
1604 map.connect ':controller/:action/:id.:format'
1605 map.connect ':controller/:action/:id'
1606 end
1607 path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
1608 assert_equal "/foo/bar/15", path
1609 assert_equal %w(that this), extras.map(&:to_s).sort
1610 end
1611
1612 def test_generate_not_first
1613 set.draw do |map|
1614 map.connect ':controller/:action/:id.:format'
1615 map.connect ':controller/:action/:id'
1616 end
1617 assert_equal "/foo/bar/15?this=hello", set.generate(:controller => "foo", :action => "bar", :id => 15, :this => "hello")
1618 end
1619
1620 def test_extra_keys_not_first
1621 set.draw do |map|
1622 map.connect ':controller/:action/:id.:format'
1623 map.connect ':controller/:action/:id'
1624 end
1625 extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
1626 assert_equal %w(that this), extras.map(&:to_s).sort
1627 end
1628
1629 def test_draw
1630 assert_equal 0, set.routes.size
1631 set.draw do |map|
1632 map.connect '/hello/world', :controller => 'a', :action => 'b'
1633 end
1634 assert_equal 1, set.routes.size
1635 end
1636
1637 def test_named_draw
1638 assert_equal 0, set.routes.size
1639 set.draw do |map|
1640 map.hello '/hello/world', :controller => 'a', :action => 'b'
1641 end
1642 assert_equal 1, set.routes.size
1643 assert_equal set.routes.first, set.named_routes[:hello]
1644 end
1645
1646 def test_later_named_routes_take_precedence
1647 set.draw do |map|
1648 map.hello '/hello/world', :controller => 'a', :action => 'b'
1649 map.hello '/hello', :controller => 'a', :action => 'b'
1650 end
1651 assert_equal set.routes.last, set.named_routes[:hello]
1652 end
1653
1654 def setup_named_route_test
1655 set.draw do |map|
1656 map.show '/people/:id', :controller => 'people', :action => 'show'
1657 map.index '/people', :controller => 'people', :action => 'index'
1658 map.multi '/people/go/:foo/:bar/joe/:id', :controller => 'people', :action => 'multi'
1659 map.users '/admin/users', :controller => 'admin/users', :action => 'index'
1660 end
1661
1662 klass = Class.new(MockController)
1663 set.install_helpers(klass)
1664 klass.new(set)
1665 end
1666
1667 def test_named_route_hash_access_method
1668 controller = setup_named_route_test
1669
1670 assert_equal(
1671 { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => false },
1672 controller.send(:hash_for_show_url, :id => 5))
1673
1674 assert_equal(
1675 { :controller => 'people', :action => 'index', :use_route => :index, :only_path => false },
1676 controller.send(:hash_for_index_url))
1677
1678 assert_equal(
1679 { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => true },
1680 controller.send(:hash_for_show_path, :id => 5)
1681 )
1682 end
1683
1684 def test_named_route_url_method
1685 controller = setup_named_route_test
1686
1687 assert_equal "http://named.route.test/people/5", controller.send(:show_url, :id => 5)
1688 assert_equal "/people/5", controller.send(:show_path, :id => 5)
1689
1690 assert_equal "http://named.route.test/people", controller.send(:index_url)
1691 assert_equal "/people", controller.send(:index_path)
1692
1693 assert_equal "http://named.route.test/admin/users", controller.send(:users_url)
1694 assert_equal '/admin/users', controller.send(:users_path)
1695 assert_equal '/admin/users', set.generate(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'})
1696 end
1697
1698 def test_named_route_url_method_with_anchor
1699 controller = setup_named_route_test
1700
1701 assert_equal "http://named.route.test/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location')
1702 assert_equal "/people/5#location", controller.send(:show_path, :id => 5, :anchor => 'location')
1703
1704 assert_equal "http://named.route.test/people#location", controller.send(:index_url, :anchor => 'location')
1705 assert_equal "/people#location", controller.send(:index_path, :anchor => 'location')
1706
1707 assert_equal "http://named.route.test/admin/users#location", controller.send(:users_url, :anchor => 'location')
1708 assert_equal '/admin/users#location', controller.send(:users_path, :anchor => 'location')
1709
1710 assert_equal "http://named.route.test/people/go/7/hello/joe/5#location",
1711 controller.send(:multi_url, 7, "hello", 5, :anchor => 'location')
1712
1713 assert_equal "http://named.route.test/people/go/7/hello/joe/5?baz=bar#location",
1714 controller.send(:multi_url, 7, "hello", 5, :baz => "bar", :anchor => 'location')
1715
1716 assert_equal "http://named.route.test/people?baz=bar#location",
1717 controller.send(:index_url, :baz => "bar", :anchor => 'location')
1718 end
1719
1720 def test_named_route_url_method_with_port
1721 controller = setup_named_route_test
1722 assert_equal "http://named.route.test:8080/people/5", controller.send(:show_url, 5, :port=>8080)
1723 end
1724
1725 def test_named_route_url_method_with_host
1726 controller = setup_named_route_test
1727 assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, :host=>"some.example.com")
1728 end
1729
1730 def test_named_route_url_method_with_protocol
1731 controller = setup_named_route_test
1732 assert_equal "https://named.route.test/people/5", controller.send(:show_url, 5, :protocol => "https")
1733 end
1734
1735 def test_named_route_url_method_with_ordered_parameters
1736 controller = setup_named_route_test
1737 assert_equal "http://named.route.test/people/go/7/hello/joe/5",
1738 controller.send(:multi_url, 7, "hello", 5)
1739 end
1740
1741 def test_named_route_url_method_with_ordered_parameters_and_hash
1742 controller = setup_named_route_test
1743 assert_equal "http://named.route.test/people/go/7/hello/joe/5?baz=bar",
1744 controller.send(:multi_url, 7, "hello", 5, :baz => "bar")
1745 end
1746
1747 def test_named_route_url_method_with_ordered_parameters_and_empty_hash
1748 controller = setup_named_route_test
1749 assert_equal "http://named.route.test/people/go/7/hello/joe/5",
1750 controller.send(:multi_url, 7, "hello", 5, {})
1751 end
1752
1753 def test_named_route_url_method_with_no_positional_arguments
1754 controller = setup_named_route_test
1755 assert_equal "http://named.route.test/people?baz=bar",
1756 controller.send(:index_url, :baz => "bar")
1757 end
1758
1759 def test_draw_default_route
1760 ActionController::Routing.with_controllers(['users']) do
1761 set.draw do |map|
1762 map.connect '/:controller/:action/:id'
1763 end
1764
1765 assert_equal 1, set.routes.size
1766 route = set.routes.first
1767
1768 assert route.segments.last.optional?
1769
1770 assert_equal '/users/show/10', set.generate(:controller => 'users', :action => 'show', :id => 10)
1771 assert_equal '/users/index/10', set.generate(:controller => 'users', :id => 10)
1772
1773 assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10'))
1774 assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10/'))
1775 end
1776 end
1777
1778 def test_draw_default_route_with_default_controller
1779 ActionController::Routing.with_controllers(['users']) do
1780 set.draw do |map|
1781 map.connect '/:controller/:action/:id', :controller => 'users'
1782 end
1783 assert_equal({:controller => 'users', :action => 'index'}, set.recognize_path('/'))
1784 end
1785 end
1786
1787 def test_route_with_parameter_shell
1788 ActionController::Routing.with_controllers(['users', 'pages']) do
1789 set.draw do |map|
1790 map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/
1791 map.connect '/:controller/:action/:id'
1792 end
1793
1794 assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages'))
1795 assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages/index'))
1796 assert_equal({:controller => 'pages', :action => 'list'}, set.recognize_path('/pages/list'))
1797
1798 assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/pages/show/10'))
1799 assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
1800 end
1801 end
1802
1803 def test_route_requirements_with_anchor_chars_are_invalid
1804 assert_raises ArgumentError do
1805 set.draw do |map|
1806 map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /^\d+/
1807 end
1808 end
1809 assert_raises ArgumentError do
1810 set.draw do |map|
1811 map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\A\d+/
1812 end
1813 end
1814 assert_raises ArgumentError do
1815 set.draw do |map|
1816 map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+$/
1817 end
1818 end
1819 assert_raises ArgumentError do
1820 set.draw do |map|
1821 map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\Z/
1822 end
1823 end
1824 assert_raises ArgumentError do
1825 set.draw do |map|
1826 map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\z/
1827 end
1828 end
1829 assert_nothing_raised do
1830 set.draw do |map|
1831 map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/, :name => /^(david|jamis)/
1832 end
1833 assert_raises ActionController::RoutingError do
1834 set.generate :controller => 'pages', :action => 'show', :id => 10
1835 end
1836 end
1837 end
1838
1839 def test_route_requirements_with_invalid_http_method_is_invalid
1840 assert_raises ArgumentError do
1841 set.draw do |map|
1842 map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :invalid}
1843 end
1844 end
1845 end
1846
1847 def test_route_requirements_with_head_method_condition_is_invalid
1848 assert_raises ArgumentError do
1849 set.draw do |map|
1850 map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :head}
1851 end
1852 end
1853 end
1854
1855 def test_non_path_route_requirements_match_all
1856 set.draw do |map|
1857 map.connect 'page/37s', :controller => 'pages', :action => 'show', :name => /(jamis|david)/
1858 end
1859 assert_equal '/page/37s', set.generate(:controller => 'pages', :action => 'show', :name => 'jamis')
1860 assert_raises ActionController::RoutingError do
1861 set.generate(:controller => 'pages', :action => 'show', :name => 'not_jamis')
1862 end
1863 assert_raises ActionController::RoutingError do
1864 set.generate(:controller => 'pages', :action => 'show', :name => 'nor_jamis_and_david')
1865 end
1866 end
1867
1868 def test_recognize_with_encoded_id_and_regex
1869 set.draw do |map|
1870 map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /[a-zA-Z0-9\+]+/
1871 end
1872
1873 assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
1874 assert_equal({:controller => 'pages', :action => 'show', :id => 'hello+world'}, set.recognize_path('/page/hello+world'))
1875 end
1876
1877 def test_recognize_with_conditions
1878 Object.const_set(:PeopleController, Class.new)
1879
1880 set.draw do |map|
1881 map.with_options(:controller => "people") do |people|
1882 people.people "/people", :action => "index", :conditions => { :method => :get }
1883 people.connect "/people", :action => "create", :conditions => { :method => :post }
1884 people.person "/people/:id", :action => "show", :conditions => { :method => :get }
1885 people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
1886 people.connect "/people/:id", :action => "destroy", :conditions => { :method => :delete }
1887 end
1888 end
1889
1890 request.path = "/people"
1891 request.method = :get
1892 assert_nothing_raised { set.recognize(request) }
1893 assert_equal("index", request.path_parameters[:action])
1894
1895 request.method = :post
1896 assert_nothing_raised { set.recognize(request) }
1897 assert_equal("create", request.path_parameters[:action])
1898
1899 request.method = :put
1900 assert_nothing_raised { set.recognize(request) }
1901 assert_equal("update", request.path_parameters[:action])
1902
1903 begin
1904 request.method = :bacon
1905 set.recognize(request)
1906 flunk 'Should have raised NotImplemented'
1907 rescue ActionController::NotImplemented => e
1908 assert_equal [:get, :post, :put, :delete], e.allowed_methods
1909 end
1910
1911 request.path = "/people/5"
1912 request.method = :get
1913 assert_nothing_raised { set.recognize(request) }
1914 assert_equal("show", request.path_parameters[:action])
1915 assert_equal("5", request.path_parameters[:id])
1916
1917 request.method = :put
1918 assert_nothing_raised { set.recognize(request) }
1919 assert_equal("update", request.path_parameters[:action])
1920 assert_equal("5", request.path_parameters[:id])
1921
1922 request.method = :delete
1923 assert_nothing_raised { set.recognize(request) }
1924 assert_equal("destroy", request.path_parameters[:action])
1925 assert_equal("5", request.path_parameters[:id])
1926
1927 begin
1928 request.method = :post
1929 set.recognize(request)
1930 flunk 'Should have raised MethodNotAllowed'
1931 rescue ActionController::MethodNotAllowed => e
1932 assert_equal [:get, :put, :delete], e.allowed_methods
1933 end
1934
1935 ensure
1936 Object.send(:remove_const, :PeopleController)
1937 end
1938
1939 def test_recognize_with_alias_in_conditions
1940 Object.const_set(:PeopleController, Class.new)
1941
1942 set.draw do |map|
1943 map.people "/people", :controller => 'people', :action => "index",
1944 :conditions => { :method => :get }
1945 map.root :people
1946 end
1947
1948 request.path = "/people"
1949 request.method = :get
1950 assert_nothing_raised { set.recognize(request) }
1951 assert_equal("people", request.path_parameters[:controller])
1952 assert_equal("index", request.path_parameters[:action])
1953
1954 request.path = "/"
1955 request.method = :get
1956 assert_nothing_raised { set.recognize(request) }
1957 assert_equal("people", request.path_parameters[:controller])
1958 assert_equal("index", request.path_parameters[:action])
1959 ensure
1960 Object.send(:remove_const, :PeopleController)
1961 end
1962
1963 def test_typo_recognition
1964 Object.const_set(:ArticlesController, Class.new)
1965
1966 set.draw do |map|
1967 map.connect 'articles/:year/:month/:day/:title',
1968 :controller => 'articles', :action => 'permalink',
1969 :year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/
1970 end
1971
1972 request.path = "/articles/2005/11/05/a-very-interesting-article"
1973 request.method = :get
1974 assert_nothing_raised { set.recognize(request) }
1975 assert_equal("permalink", request.path_parameters[:action])
1976 assert_equal("2005", request.path_parameters[:year])
1977 assert_equal("11", request.path_parameters[:month])
1978 assert_equal("05", request.path_parameters[:day])
1979 assert_equal("a-very-interesting-article", request.path_parameters[:title])
1980
1981 ensure
1982 Object.send(:remove_const, :ArticlesController)
1983 end
1984
1985 def test_routing_traversal_does_not_load_extra_classes
1986 assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
1987 set.draw do |map|
1988 map.connect '/profile', :controller => 'profile'
1989 end
1990
1991 request.path = '/profile'
1992
1993 set.recognize(request) rescue nil
1994
1995 assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
1996 end
1997
1998 def test_recognize_with_conditions_and_format
1999 Object.const_set(:PeopleController, Class.new)
2000
2001 set.draw do |map|
2002 map.with_options(:controller => "people") do |people|
2003 people.person "/people/:id", :action => "show", :conditions => { :method => :get }
2004 people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
2005 people.connect "/people/:id.:_format", :action => "show", :conditions => { :method => :get }
2006 end
2007 end
2008
2009 request.path = "/people/5"
2010 request.method = :get
2011 assert_nothing_raised { set.recognize(request) }
2012 assert_equal("show", request.path_parameters[:action])
2013 assert_equal("5", request.path_parameters[:id])
2014
2015 request.method = :put
2016 assert_nothing_raised { set.recognize(request) }
2017 assert_equal("update", request.path_parameters[:action])
2018
2019 request.path = "/people/5.png"
2020 request.method = :get
2021 assert_nothing_raised { set.recognize(request) }
2022 assert_equal("show", request.path_parameters[:action])
2023 assert_equal("5", request.path_parameters[:id])
2024 assert_equal("png", request.path_parameters[:_format])
2025 ensure
2026 Object.send(:remove_const, :PeopleController)
2027 end
2028
2029 def test_generate_with_default_action
2030 set.draw do |map|
2031 map.connect "/people", :controller => "people"
2032 map.connect "/people/list", :controller => "people", :action => "list"
2033 end
2034
2035 url = set.generate(:controller => "people", :action => "list")
2036 assert_equal "/people/list", url
2037 end
2038
2039 def test_root_map
2040 Object.const_set(:PeopleController, Class.new)
2041
2042 set.draw { |map| map.root :controller => "people" }
2043
2044 request.path = ""
2045 request.method = :get
2046 assert_nothing_raised { set.recognize(request) }
2047 assert_equal("people", request.path_parameters[:controller])
2048 assert_equal("index", request.path_parameters[:action])
2049 ensure
2050 Object.send(:remove_const, :PeopleController)
2051 end
2052
2053 def test_namespace
2054 Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
2055
2056 set.draw do |map|
2057
2058 map.namespace 'api' do |api|
2059 api.route 'inventory', :controller => "products", :action => 'inventory'
2060 end
2061
2062 end
2063
2064 request.path = "/api/inventory"
2065 request.method = :get
2066 assert_nothing_raised { set.recognize(request) }
2067 assert_equal("api/products", request.path_parameters[:controller])
2068 assert_equal("inventory", request.path_parameters[:action])
2069 ensure
2070 Object.send(:remove_const, :Api)
2071 end
2072
2073 def test_namespaced_root_map
2074 Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
2075
2076 set.draw do |map|
2077
2078 map.namespace 'api' do |api|
2079 api.root :controller => "products"
2080 end
2081
2082 end
2083
2084 request.path = "/api"
2085 request.method = :get
2086 assert_nothing_raised { set.recognize(request) }
2087 assert_equal("api/products", request.path_parameters[:controller])
2088 assert_equal("index", request.path_parameters[:action])
2089 ensure
2090 Object.send(:remove_const, :Api)
2091 end
2092
2093 def test_namespace_with_path_prefix
2094 Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) })
2095
2096 set.draw do |map|
2097
2098 map.namespace 'api', :path_prefix => 'prefix' do |api|
2099 api.route 'inventory', :controller => "products", :action => 'inventory'
2100 end
2101
2102 end
2103
2104 request.path = "/prefix/inventory"
2105 request.method = :get
2106 assert_nothing_raised { set.recognize(request) }
2107 assert_equal("api/products", request.path_parameters[:controller])
2108 assert_equal("inventory", request.path_parameters[:action])
2109 ensure
2110 Object.send(:remove_const, :Api)
2111 end
2112
2113 def test_generate_finds_best_fit
2114 set.draw do |map|
2115 map.connect "/people", :controller => "people", :action => "index"
2116 map.connect "/ws/people", :controller => "people", :action => "index", :ws => true
2117 end
2118
2119 url = set.generate(:controller => "people", :action => "index", :ws => true)
2120 assert_equal "/ws/people", url
2121 end
2122
2123 def test_generate_changes_controller_module
2124 set.draw { |map| map.connect ':controller/:action/:id' }
2125 current = { :controller => "bling/bloop", :action => "bap", :id => 9 }
2126 url = set.generate({:controller => "foo/bar", :action => "baz", :id => 7}, current)
2127 assert_equal "/foo/bar/baz/7", url
2128 end
2129
2130 def test_id_is_not_impossibly_sticky
2131 set.draw do |map|
2132 map.connect 'foo/:number', :controller => "people", :action => "index"
2133 map.connect ':controller/:action/:id'
2134 end
2135
2136 url = set.generate({:controller => "people", :action => "index", :number => 3},
2137 {:controller => "people", :action => "index", :id => "21"})
2138 assert_equal "/foo/3", url
2139 end
2140
2141 def test_id_is_sticky_when_it_ought_to_be
2142 set.draw do |map|
2143 map.connect ':controller/:id/:action'
2144 end
2145
2146 url = set.generate({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"})
2147 assert_equal "/people/7/destroy", url
2148 end
2149
2150 def test_use_static_path_when_possible
2151 set.draw do |map|
2152 map.connect 'about', :controller => "welcome", :action => "about"
2153 map.connect ':controller/:action/:id'
2154 end
2155
2156 url = set.generate({:controller => "welcome", :action => "about"},
2157 {:controller => "welcome", :action => "get", :id => "7"})
2158 assert_equal "/about", url
2159 end
2160
2161 def test_generate
2162 set.draw { |map| map.connect ':controller/:action/:id' }
2163
2164 args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
2165 assert_equal "/foo/bar/7?x=y", set.generate(args)
2166 assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args)
2167 assert_equal [:x], set.extra_keys(args)
2168 end
2169
2170 def test_generate_with_path_prefix
2171 set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => 'my' }
2172
2173 args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
2174 assert_equal "/my/foo/bar/7?x=y", set.generate(args)
2175 end
2176
2177 def test_named_routes_are_never_relative_to_modules
2178 set.draw do |map|
2179 map.connect "/connection/manage/:action", :controller => 'connection/manage'
2180 map.connect "/connection/connection", :controller => "connection/connection"
2181 map.family_connection "/connection", :controller => "connection"
2182 end
2183
2184 url = set.generate({:controller => "connection"}, {:controller => 'connection/manage'})
2185 assert_equal "/connection/connection", url
2186
2187 url = set.generate({:use_route => :family_connection, :controller => "connection"}, {:controller => 'connection/manage'})
2188 assert_equal "/connection", url
2189 end
2190
2191 def test_action_left_off_when_id_is_recalled
2192 set.draw do |map|
2193 map.connect ':controller/:action/:id'
2194 end
2195 assert_equal '/post', set.generate(
2196 {:controller => 'post', :action => 'index'},
2197 {:controller => 'post', :action => 'show', :id => '10'}
2198 )
2199 end
2200
2201 def test_query_params_will_be_shown_when_recalled
2202 set.draw do |map|
2203 map.connect 'show_post/:parameter', :controller => 'post', :action => 'show'
2204 map.connect ':controller/:action/:id'
2205 end
2206 assert_equal '/post/edit?parameter=1', set.generate(
2207 {:action => 'edit', :parameter => 1},
2208 {:controller => 'post', :action => 'show', :parameter => 1}
2209 )
2210 end
2211
2212 def test_expiry_determination_should_consider_values_with_to_param
2213 set.draw { |map| map.connect 'projects/:project_id/:controller/:action' }
2214 assert_equal '/projects/1/post/show', set.generate(
2215 {:action => 'show', :project_id => 1},
2216 {:controller => 'post', :action => 'show', :project_id => '1'})
2217 end
2218
2219 def test_generate_all
2220 set.draw do |map|
2221 map.connect 'show_post/:id', :controller => 'post', :action => 'show'
2222 map.connect ':controller/:action/:id'
2223 end
2224 all = set.generate(
2225 {:action => 'show', :id => 10, :generate_all => true},
2226 {:controller => 'post', :action => 'show'}
2227 )
2228 assert_equal 2, all.length
2229 assert_equal '/show_post/10', all.first
2230 assert_equal '/post/show/10', all.last
2231 end
2232
2233 def test_named_route_in_nested_resource
2234 set.draw do |map|
2235 map.resources :projects do |project|
2236 project.milestones 'milestones', :controller => 'milestones', :action => 'index'
2237 end
2238 end
2239
2240 request.path = "/projects/1/milestones"
2241 request.method = :get
2242 assert_nothing_raised { set.recognize(request) }
2243 assert_equal("milestones", request.path_parameters[:controller])
2244 assert_equal("index", request.path_parameters[:action])
2245 end
2246
2247 def test_setting_root_in_namespace_using_symbol
2248 assert_nothing_raised do
2249 set.draw do |map|
2250 map.namespace :admin do |admin|
2251 admin.root :controller => 'home'
2252 end
2253 end
2254 end
2255 end
2256
2257 def test_setting_root_in_namespace_using_string
2258 assert_nothing_raised do
2259 set.draw do |map|
2260 map.namespace 'admin' do |admin|
2261 admin.root :controller => 'home'
2262 end
2263 end
2264 end
2265 end
2266
2267 def test_route_requirements_with_unsupported_regexp_options_must_error
2268 assert_raises ArgumentError do
2269 set.draw do |map|
2270 map.connect 'page/:name', :controller => 'pages',
2271 :action => 'show',
2272 :requirements => {:name => /(david|jamis)/m}
2273 end
2274 end
2275 end
2276
2277 def test_route_requirements_with_supported_options_must_not_error
2278 assert_nothing_raised do
2279 set.draw do |map|
2280 map.connect 'page/:name', :controller => 'pages',
2281 :action => 'show',
2282 :requirements => {:name => /(david|jamis)/i}
2283 end
2284 end
2285 assert_nothing_raised do
2286 set.draw do |map|
2287 map.connect 'page/:name', :controller => 'pages',
2288 :action => 'show',
2289 :requirements => {:name => / # Desperately overcommented regexp
2290 ( #Either
2291 david #The Creator
2292 | #Or
2293 jamis #The Deployer
2294 )/x}
2295 end
2296 end
2297 end
2298
2299 def test_route_requirement_recognize_with_ignore_case
2300 set.draw do |map|
2301 map.connect 'page/:name', :controller => 'pages',
2302 :action => 'show',
2303 :requirements => {:name => /(david|jamis)/i}
2304 end
2305 assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
2306 assert_raises ActionController::RoutingError do
2307 set.recognize_path('/page/davidjamis')
2308 end
2309 assert_equal({:controller => 'pages', :action => 'show', :name => 'DAVID'}, set.recognize_path('/page/DAVID'))
2310 end
2311
2312 def test_route_requirement_generate_with_ignore_case
2313 set.draw do |map|
2314 map.connect 'page/:name', :controller => 'pages',
2315 :action => 'show',
2316 :requirements => {:name => /(david|jamis)/i}
2317 end
2318 url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
2319 assert_equal "/page/david", url
2320 assert_raises ActionController::RoutingError do
2321 url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
2322 end
2323 url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
2324 assert_equal "/page/JAMIS", url
2325 end
2326
2327 def test_route_requirement_recognize_with_extended_syntax
2328 set.draw do |map|
2329 map.connect 'page/:name', :controller => 'pages',
2330 :action => 'show',
2331 :requirements => {:name => / # Desperately overcommented regexp
2332 ( #Either
2333 david #The Creator
2334 | #Or
2335 jamis #The Deployer
2336 )/x}
2337 end
2338 assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
2339 assert_equal({:controller => 'pages', :action => 'show', :name => 'david'}, set.recognize_path('/page/david'))
2340 assert_raises ActionController::RoutingError do
2341 set.recognize_path('/page/david #The Creator')
2342 end
2343 assert_raises ActionController::RoutingError do
2344 set.recognize_path('/page/David')
2345 end
2346 end
2347
2348 def test_route_requirement_generate_with_extended_syntax
2349 set.draw do |map|
2350 map.connect 'page/:name', :controller => 'pages',
2351 :action => 'show',
2352 :requirements => {:name => / # Desperately overcommented regexp
2353 ( #Either
2354 david #The Creator
2355 | #Or
2356 jamis #The Deployer
2357 )/x}
2358 end
2359 url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
2360 assert_equal "/page/david", url
2361 assert_raises ActionController::RoutingError do
2362 url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
2363 end
2364 assert_raises ActionController::RoutingError do
2365 url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
2366 end
2367 end
2368
2369 def test_route_requirement_generate_with_xi_modifiers
2370 set.draw do |map|
2371 map.connect 'page/:name', :controller => 'pages',
2372 :action => 'show',
2373 :requirements => {:name => / # Desperately overcommented regexp
2374 ( #Either
2375 david #The Creator
2376 | #Or
2377 jamis #The Deployer
2378 )/xi}
2379 end
2380 url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
2381 assert_equal "/page/JAMIS", url
2382 end
2383
2384 def test_route_requirement_recognize_with_xi_modifiers
2385 set.draw do |map|
2386 map.connect 'page/:name', :controller => 'pages',
2387 :action => 'show',
2388 :requirements => {:name => / # Desperately overcommented regexp
2389 ( #Either
2390 david #The Creator
2391 | #Or
2392 jamis #The Deployer
2393 )/xi}
2394 end
2395 assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'}, set.recognize_path('/page/JAMIS'))
2396 end
2397 end
2398
2399 class RouteLoadingTest < Test::Unit::TestCase
2400 def setup
2401 routes.instance_variable_set '@routes_last_modified', nil
2402 silence_warnings { Object.const_set :RAILS_ROOT, '.' }
2403 ActionController::Routing::Routes.configuration_file = File.join(RAILS_ROOT, 'config', 'routes.rb')
2404
2405 @stat = stub_everything
2406 end
2407
2408 def teardown
2409 ActionController::Routing::Routes.configuration_file = nil
2410 Object.send :remove_const, :RAILS_ROOT
2411 end
2412
2413 def test_load
2414 File.expects(:stat).returns(@stat)
2415 routes.expects(:load).with(regexp_matches(/routes\.rb$/))
2416
2417 routes.reload
2418 end
2419
2420 def test_no_reload_when_not_modified
2421 @stat.expects(:mtime).times(2).returns(1)
2422 File.expects(:stat).times(2).returns(@stat)
2423 routes.expects(:load).with(regexp_matches(/routes\.rb$/)).at_most_once
2424
2425 2.times { routes.reload }
2426 end
2427
2428 def test_reload_when_modified
2429 @stat.expects(:mtime).at_least(2).returns(1, 2)
2430 File.expects(:stat).at_least(2).returns(@stat)
2431 routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)
2432
2433 2.times { routes.reload }
2434 end
2435
2436 def test_bang_forces_reload
2437 @stat.expects(:mtime).at_least(2).returns(1)
2438 File.expects(:stat).at_least(2).returns(@stat)
2439 routes.expects(:load).with(regexp_matches(/routes\.rb$/)).times(2)
2440
2441 2.times { routes.reload! }
2442 end
2443
2444 def test_adding_inflections_forces_reload
2445 ActiveSupport::Inflector::Inflections.instance.expects(:uncountable).with('equipment')
2446 routes.expects(:reload!)
2447
2448 ActiveSupport::Inflector.inflections { |inflect| inflect.uncountable('equipment') }
2449 end
2450
2451 def test_load_with_configuration
2452 routes.configuration_file = "foobarbaz"
2453 File.expects(:stat).returns(@stat)
2454 routes.expects(:load).with("foobarbaz")
2455
2456 routes.reload
2457 end
2458
2459 private
2460 def routes
2461 ActionController::Routing::Routes
2462 end
2463 end
2464 end