PageRenderTime 68ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

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