PageRenderTime 36ms CodeModel.GetById 42ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/rails/actionpack/test/controller/routing_test.rb

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