PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/scratch/deliciolytics.co.uk/vendor/rails/actionpack/test/controller/routing_test.rb

http://chrisroos.googlecode.com/
Ruby | 1777 lines | 1421 code | 331 blank | 25 comment | 10 complexity | 779dba68bef7f9016157b9ef72bf25b8 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. require 'abstract_unit'
  2. require 'controller/fake_controllers'
  3. class MilestonesController < ActionController::Base
  4. def index() head :ok end
  5. alias_method :show, :index
  6. def rescue_action(e) raise e end
  7. end
  8. RunTimeTests = ARGV.include? 'time'
  9. ROUTING = ActionController::Routing
  10. class ROUTING::RouteBuilder
  11. attr_reader :warn_output
  12. def warn(msg)
  13. (@warn_output ||= []) << msg
  14. end
  15. end
  16. # See RFC 3986, section 3.3 for allowed path characters.
  17. class UriReservedCharactersRoutingTest < Test::Unit::TestCase
  18. def setup
  19. ActionController::Routing.use_controllers! ['controller']
  20. @set = ActionController::Routing::RouteSet.new
  21. @set.draw do |map|
  22. map.connect ':controller/:action/:variable/*additional'
  23. end
  24. safe, unsafe = %w(: @ & = + $ , ;), %w(^ / ? # [ ])
  25. hex = unsafe.map { |char| '%' + char.unpack('H2').first.upcase }
  26. @segment = "#{safe.join}#{unsafe.join}".freeze
  27. @escaped = "#{safe.join}#{hex.join}".freeze
  28. end
  29. def test_route_generation_escapes_unsafe_path_characters
  30. assert_equal "/contr#{@segment}oller/act#{@escaped}ion/var#{@escaped}iable/add#{@escaped}itional-1/add#{@escaped}itional-2",
  31. @set.generate(:controller => "contr#{@segment}oller",
  32. :action => "act#{@segment}ion",
  33. :variable => "var#{@segment}iable",
  34. :additional => ["add#{@segment}itional-1", "add#{@segment}itional-2"])
  35. end
  36. def test_route_recognition_unescapes_path_components
  37. options = { :controller => "controller",
  38. :action => "act#{@segment}ion",
  39. :variable => "var#{@segment}iable",
  40. :additional => ["add#{@segment}itional-1", "add#{@segment}itional-2"] }
  41. assert_equal options, @set.recognize_path("/controller/act#{@escaped}ion/var#{@escaped}iable/add#{@escaped}itional-1/add#{@escaped}itional-2")
  42. end
  43. def test_route_generation_allows_passing_non_string_values_to_generated_helper
  44. assert_equal "/controller/action/variable/1/2", @set.generate(:controller => "controller",
  45. :action => "action",
  46. :variable => "variable",
  47. :additional => [1, 2])
  48. end
  49. end
  50. class SegmentTest < Test::Unit::TestCase
  51. def test_first_segment_should_interpolate_for_structure
  52. s = ROUTING::Segment.new
  53. def s.interpolation_statement(array) 'hello' end
  54. assert_equal 'hello', s.continue_string_structure([])
  55. end
  56. def test_interpolation_statement
  57. s = ROUTING::StaticSegment.new("Hello")
  58. assert_equal "Hello", eval(s.interpolation_statement([]))
  59. assert_equal "HelloHello", eval(s.interpolation_statement([s]))
  60. s2 = ROUTING::StaticSegment.new("-")
  61. assert_equal "Hello-Hello", eval(s.interpolation_statement([s, s2]))
  62. s3 = ROUTING::StaticSegment.new("World")
  63. assert_equal "Hello-World", eval(s3.interpolation_statement([s, s2]))
  64. end
  65. end
  66. class StaticSegmentTest < Test::Unit::TestCase
  67. def test_interpolation_chunk_should_respect_raw
  68. s = ROUTING::StaticSegment.new('Hello World')
  69. assert !s.raw?
  70. assert_equal 'Hello%20World', s.interpolation_chunk
  71. s = ROUTING::StaticSegment.new('Hello World', :raw => true)
  72. assert s.raw?
  73. assert_equal 'Hello World', s.interpolation_chunk
  74. end
  75. def test_regexp_chunk_should_escape_specials
  76. s = ROUTING::StaticSegment.new('Hello*World')
  77. assert_equal 'Hello\*World', s.regexp_chunk
  78. s = ROUTING::StaticSegment.new('HelloWorld')
  79. assert_equal 'HelloWorld', s.regexp_chunk
  80. end
  81. def test_regexp_chunk_should_add_question_mark_for_optionals
  82. s = ROUTING::StaticSegment.new("/", :optional => true)
  83. assert_equal "/?", s.regexp_chunk
  84. s = ROUTING::StaticSegment.new("hello", :optional => true)
  85. assert_equal "(?:hello)?", s.regexp_chunk
  86. end
  87. end
  88. class DynamicSegmentTest < Test::Unit::TestCase
  89. def segment(options = {})
  90. unless @segment
  91. @segment = ROUTING::DynamicSegment.new(:a, options)
  92. end
  93. @segment
  94. end
  95. def test_extract_value
  96. s = ROUTING::DynamicSegment.new(:a)
  97. hash = {:a => '10', :b => '20'}
  98. assert_equal '10', eval(s.extract_value)
  99. hash = {:b => '20'}
  100. assert_equal nil, eval(s.extract_value)
  101. s.default = '20'
  102. assert_equal '20', eval(s.extract_value)
  103. end
  104. def test_default_local_name
  105. assert_equal 'a_value', segment.local_name,
  106. "Unexpected name -- all value_check tests will fail!"
  107. end
  108. def test_presence_value_check
  109. a_value = 10
  110. assert eval(segment.value_check)
  111. end
  112. def test_regexp_value_check_rejects_nil
  113. segment = segment(:regexp => /\d+/)
  114. a_value = nil
  115. assert !eval(segment.value_check)
  116. end
  117. def test_optional_regexp_value_check_should_accept_nil
  118. segment = segment(:regexp => /\d+/, :optional => true)
  119. a_value = nil
  120. assert eval(segment.value_check)
  121. end
  122. def test_regexp_value_check_rejects_no_match
  123. segment = segment(:regexp => /\d+/)
  124. a_value = "Hello20World"
  125. assert !eval(segment.value_check)
  126. a_value = "20Hi"
  127. assert !eval(segment.value_check)
  128. end
  129. def test_regexp_value_check_accepts_match
  130. segment = segment(:regexp => /\d+/)
  131. a_value = "30"
  132. assert eval(segment.value_check)
  133. end
  134. def test_value_check_fails_on_nil
  135. a_value = nil
  136. assert ! eval(segment.value_check)
  137. end
  138. def test_optional_value_needs_no_check
  139. segment = segment(:optional => true)
  140. a_value = nil
  141. assert_equal nil, segment.value_check
  142. end
  143. def test_regexp_value_check_should_accept_match_with_default
  144. segment = segment(:regexp => /\d+/, :default => '200')
  145. a_value = '100'
  146. assert eval(segment.value_check)
  147. end
  148. def test_expiry_should_not_trigger_once_expired
  149. expired = true
  150. hash = merged = {:a => 2, :b => 3}
  151. options = {:b => 3}
  152. expire_on = Hash.new { raise 'No!!!' }
  153. eval(segment.expiry_statement)
  154. rescue RuntimeError
  155. flunk "Expiry check should not have occurred!"
  156. end
  157. def test_expiry_should_occur_according_to_expire_on
  158. expired = false
  159. hash = merged = {:a => 2, :b => 3}
  160. options = {:b => 3}
  161. expire_on = {:b => true, :a => false}
  162. eval(segment.expiry_statement)
  163. assert !expired
  164. assert_equal({:a => 2, :b => 3}, hash)
  165. expire_on = {:b => true, :a => true}
  166. eval(segment.expiry_statement)
  167. assert expired
  168. assert_equal({:b => 3}, hash)
  169. end
  170. def test_extraction_code_should_return_on_nil
  171. hash = merged = {:b => 3}
  172. options = {:b => 3}
  173. a_value = nil
  174. # Local jump because of return inside eval.
  175. assert_raise(LocalJumpError) { eval(segment.extraction_code) }
  176. end
  177. def test_extraction_code_should_return_on_mismatch
  178. segment = segment(:regexp => /\d+/)
  179. hash = merged = {:a => 'Hi', :b => '3'}
  180. options = {:b => '3'}
  181. a_value = nil
  182. # Local jump because of return inside eval.
  183. assert_raise(LocalJumpError) { eval(segment.extraction_code) }
  184. end
  185. def test_extraction_code_should_accept_value_and_set_local
  186. hash = merged = {:a => 'Hi', :b => '3'}
  187. options = {:b => '3'}
  188. a_value = nil
  189. expired = true
  190. eval(segment.extraction_code)
  191. assert_equal 'Hi', a_value
  192. end
  193. def test_extraction_should_work_without_value_check
  194. segment.default = 'hi'
  195. hash = merged = {:b => '3'}
  196. options = {:b => '3'}
  197. a_value = nil
  198. expired = true
  199. eval(segment.extraction_code)
  200. assert_equal 'hi', a_value
  201. end
  202. def test_extraction_code_should_perform_expiry
  203. expired = false
  204. hash = merged = {:a => 'Hi', :b => '3'}
  205. options = {:b => '3'}
  206. expire_on = {:a => true}
  207. a_value = nil
  208. eval(segment.extraction_code)
  209. assert_equal 'Hi', a_value
  210. assert expired
  211. assert_equal options, hash
  212. end
  213. def test_interpolation_chunk_should_replace_value
  214. a_value = 'Hi'
  215. assert_equal a_value, eval(%("#{segment.interpolation_chunk}"))
  216. end
  217. def test_interpolation_chunk_should_accept_nil
  218. a_value = nil
  219. assert_equal '', eval(%("#{segment.interpolation_chunk('a_value')}"))
  220. end
  221. def test_value_regexp_should_be_nil_without_regexp
  222. assert_equal nil, segment.value_regexp
  223. end
  224. def test_value_regexp_should_match_exacly
  225. segment = segment(:regexp => /\d+/)
  226. assert_no_match segment.value_regexp, "Hello 10 World"
  227. assert_no_match segment.value_regexp, "Hello 10"
  228. assert_no_match segment.value_regexp, "10 World"
  229. assert_match segment.value_regexp, "10"
  230. end
  231. def test_regexp_chunk_should_return_string
  232. segment = segment(:regexp => /\d+/)
  233. assert_kind_of String, segment.regexp_chunk
  234. end
  235. def test_build_pattern_non_optional_with_no_captures
  236. # Non optional
  237. a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /\d+/)
  238. assert_equal "(\\d+)stuff", a_segment.build_pattern('stuff')
  239. end
  240. def test_build_pattern_non_optional_with_captures
  241. # Non optional
  242. a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /(\d+)(.*?)/)
  243. assert_equal "((\\d+)(.*?))stuff", a_segment.build_pattern('stuff')
  244. end
  245. def test_optionality_implied
  246. a_segment = ROUTING::DynamicSegment.new(:id)
  247. assert a_segment.optionality_implied?
  248. a_segment = ROUTING::DynamicSegment.new(:action)
  249. assert a_segment.optionality_implied?
  250. end
  251. def test_modifiers_must_be_handled_sensibly
  252. a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/i)
  253. assert_equal "((?i-mx:david|jamis))stuff", a_segment.build_pattern('stuff')
  254. a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/x)
  255. assert_equal "((?x-mi:david|jamis))stuff", a_segment.build_pattern('stuff')
  256. a_segment = ROUTING::DynamicSegment.new(nil, :regexp => /david|jamis/)
  257. assert_equal "(david|jamis)stuff", a_segment.build_pattern('stuff')
  258. end
  259. end
  260. class ControllerSegmentTest < Test::Unit::TestCase
  261. def test_regexp_should_only_match_possible_controllers
  262. ActionController::Routing.with_controllers %w(admin/accounts admin/users account pages) do
  263. cs = ROUTING::ControllerSegment.new :controller
  264. regexp = %r{\A#{cs.regexp_chunk}\Z}
  265. ActionController::Routing.possible_controllers.each do |name|
  266. assert_match regexp, name
  267. assert_no_match regexp, "#{name}_fake"
  268. match = regexp.match name
  269. assert_equal name, match[1]
  270. end
  271. end
  272. end
  273. end
  274. class PathSegmentTest < Test::Unit::TestCase
  275. def segment(options = {})
  276. unless @segment
  277. @segment = ROUTING::PathSegment.new(:path, options)
  278. end
  279. @segment
  280. end
  281. def test_regexp_chunk_should_return_string
  282. segment = segment(:regexp => /[a-z]+/)
  283. assert_kind_of String, segment.regexp_chunk
  284. end
  285. def test_regexp_chunk_should_be_wrapped_with_parenthesis
  286. segment = segment(:regexp => /[a-z]+/)
  287. assert_equal "([a-z]+)", segment.regexp_chunk
  288. end
  289. def test_regexp_chunk_should_respect_options
  290. segment = segment(:regexp => /[a-z]+/i)
  291. assert_equal "((?i-mx:[a-z]+))", segment.regexp_chunk
  292. end
  293. end
  294. class RouteBuilderTest < Test::Unit::TestCase
  295. def builder
  296. @builder ||= ROUTING::RouteBuilder.new
  297. end
  298. def build(path, options)
  299. builder.build(path, options)
  300. end
  301. def test_options_should_not_be_modified
  302. requirements1 = { :id => /\w+/, :controller => /(?:[a-z](?:-?[a-z]+)*)/ }
  303. requirements2 = requirements1.dup
  304. assert_equal requirements1, requirements2
  305. with_options(:controller => 'folder',
  306. :requirements => requirements2) do |m|
  307. m.build 'folders/new', :action => 'new'
  308. end
  309. assert_equal requirements1, requirements2
  310. end
  311. def test_segment_for_static
  312. segment, rest = builder.segment_for 'ulysses'
  313. assert_equal '', rest
  314. assert_kind_of ROUTING::StaticSegment, segment
  315. assert_equal 'ulysses', segment.value
  316. end
  317. def test_segment_for_action
  318. segment, rest = builder.segment_for ':action'
  319. assert_equal '', rest
  320. assert_kind_of ROUTING::DynamicSegment, segment
  321. assert_equal :action, segment.key
  322. assert_equal 'index', segment.default
  323. end
  324. def test_segment_for_dynamic
  325. segment, rest = builder.segment_for ':login'
  326. assert_equal '', rest
  327. assert_kind_of ROUTING::DynamicSegment, segment
  328. assert_equal :login, segment.key
  329. assert_equal nil, segment.default
  330. assert ! segment.optional?
  331. end
  332. def test_segment_for_with_rest
  333. segment, rest = builder.segment_for ':login/:action'
  334. assert_equal :login, segment.key
  335. assert_equal '/:action', rest
  336. segment, rest = builder.segment_for rest
  337. assert_equal '/', segment.value
  338. assert_equal ':action', rest
  339. segment, rest = builder.segment_for rest
  340. assert_equal :action, segment.key
  341. assert_equal '', rest
  342. end
  343. def test_segments_for
  344. segments = builder.segments_for_route_path '/:controller/:action/:id'
  345. assert_kind_of ROUTING::DividerSegment, segments[0]
  346. assert_equal '/', segments[2].value
  347. assert_kind_of ROUTING::DynamicSegment, segments[1]
  348. assert_equal :controller, segments[1].key
  349. assert_kind_of ROUTING::DividerSegment, segments[2]
  350. assert_equal '/', segments[2].value
  351. assert_kind_of ROUTING::DynamicSegment, segments[3]
  352. assert_equal :action, segments[3].key
  353. assert_kind_of ROUTING::DividerSegment, segments[4]
  354. assert_equal '/', segments[4].value
  355. assert_kind_of ROUTING::DynamicSegment, segments[5]
  356. assert_equal :id, segments[5].key
  357. end
  358. def test_segment_for_action
  359. s, r = builder.segment_for(':action/something/else')
  360. assert_equal '/something/else', r
  361. assert_equal :action, s.key
  362. end
  363. def test_action_default_should_not_trigger_on_prefix
  364. s, r = builder.segment_for ':action_name/something/else'
  365. assert_equal '/something/else', r
  366. assert_equal :action_name, s.key
  367. assert_equal nil, s.default
  368. end
  369. def test_divide_route_options
  370. segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
  371. defaults, requirements = builder.divide_route_options(segments,
  372. :action => 'buy', :person => /\w+/, :car => /\w+/,
  373. :defaults => {:person => nil, :car => nil}
  374. )
  375. assert_equal({:action => 'buy', :person => nil, :car => nil}, defaults)
  376. assert_equal({:person => /\w+/, :car => /\w+/}, requirements)
  377. end
  378. def test_assign_route_options
  379. segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
  380. defaults = {:action => 'buy', :person => nil, :car => nil}
  381. requirements = {:person => /\w+/, :car => /\w+/}
  382. route_requirements = builder.assign_route_options(segments, defaults, requirements)
  383. assert_equal({}, route_requirements)
  384. assert_equal :action, segments[3].key
  385. assert_equal 'buy', segments[3].default
  386. assert_equal :person, segments[5].key
  387. assert_equal %r/\w+/, segments[5].regexp
  388. assert segments[5].optional?
  389. assert_equal :car, segments[7].key
  390. assert_equal %r/\w+/, segments[7].regexp
  391. assert segments[7].optional?
  392. end
  393. def test_assign_route_options_with_anchor_chars
  394. segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
  395. defaults = {:action => 'buy', :person => nil, :car => nil}
  396. requirements = {:person => /\w+/, :car => /^\w+$/}
  397. assert_raise ArgumentError do
  398. route_requirements = builder.assign_route_options(segments, defaults, requirements)
  399. end
  400. requirements[:car] = /[^\/]+/
  401. route_requirements = builder.assign_route_options(segments, defaults, requirements)
  402. end
  403. def test_optional_segments_preceding_required_segments
  404. segments = builder.segments_for_route_path '/cars/:action/:person/:car/'
  405. defaults = {:action => 'buy', :person => nil, :car => "model-t"}
  406. assert builder.assign_route_options(segments, defaults, {}).empty?
  407. 0.upto(1) { |i| assert !segments[i].optional?, "segment #{i} is optional and it shouldn't be" }
  408. assert segments[2].optional?
  409. assert_equal nil, builder.warn_output # should only warn on the :person segment
  410. end
  411. def test_segmentation_of_dot_path
  412. segments = builder.segments_for_route_path '/books/:action.rss'
  413. assert builder.assign_route_options(segments, {}, {}).empty?
  414. assert_equal 6, segments.length # "/", "books", "/", ":action", ".", "rss"
  415. assert !segments.any? { |seg| seg.optional? }
  416. end
  417. def test_segmentation_of_dynamic_dot_path
  418. segments = builder.segments_for_route_path '/books/:action.:format'
  419. assert builder.assign_route_options(segments, {}, {}).empty?
  420. assert_equal 6, segments.length # "/", "books", "/", ":action", ".", ":format"
  421. assert !segments.any? { |seg| seg.optional? }
  422. assert_kind_of ROUTING::DynamicSegment, segments.last
  423. end
  424. def test_assignment_of_default_options
  425. segments = builder.segments_for_route_path '/:controller/:action/:id/'
  426. action, id = segments[-4], segments[-2]
  427. assert_equal :action, action.key
  428. assert_equal :id, id.key
  429. assert ! action.optional?
  430. assert ! id.optional?
  431. builder.assign_default_route_options(segments)
  432. assert_equal 'index', action.default
  433. assert action.optional?
  434. assert id.optional?
  435. end
  436. def test_assignment_of_default_options_respects_existing_defaults
  437. segments = builder.segments_for_route_path '/:controller/:action/:id/'
  438. action, id = segments[-4], segments[-2]
  439. assert_equal :action, action.key
  440. assert_equal :id, id.key
  441. action.default = 'show'
  442. action.is_optional = true
  443. id.default = 'Welcome'
  444. id.is_optional = true
  445. builder.assign_default_route_options(segments)
  446. assert_equal 'show', action.default
  447. assert action.optional?
  448. assert_equal 'Welcome', id.default
  449. assert id.optional?
  450. end
  451. def test_assignment_of_default_options_respects_regexps
  452. segments = builder.segments_for_route_path '/:controller/:action/:id/'
  453. action = segments[-4]
  454. assert_equal :action, action.key
  455. segments[-4] = ROUTING::DynamicSegment.new(:action, :regexp => /show|in/)
  456. builder.assign_default_route_options(segments)
  457. assert_equal nil, action.default
  458. assert ! action.optional?
  459. end
  460. def test_assignment_of_is_optional_when_default
  461. segments = builder.segments_for_route_path '/books/:action.rss'
  462. assert_equal segments[3].key, :action
  463. segments[3].default = 'changes'
  464. builder.ensure_required_segments(segments)
  465. assert ! segments[3].optional?
  466. end
  467. def test_is_optional_is_assigned_to_default_segments
  468. segments = builder.segments_for_route_path '/books/:action'
  469. builder.assign_route_options(segments, {:action => 'index'}, {})
  470. assert_equal segments[3].key, :action
  471. assert segments[3].optional?
  472. assert_kind_of ROUTING::DividerSegment, segments[2]
  473. assert segments[2].optional?
  474. end
  475. # XXX is optional not being set right?
  476. # /blah/:defaulted_segment <-- is the second slash optional? it should be.
  477. def test_route_build
  478. ActionController::Routing.with_controllers %w(users pages) do
  479. r = builder.build '/:controller/:action/:id/', :action => nil
  480. [0, 2, 4].each do |i|
  481. assert_kind_of ROUTING::DividerSegment, r.segments[i]
  482. assert_equal '/', r.segments[i].value
  483. assert r.segments[i].optional? if i > 1
  484. end
  485. assert_kind_of ROUTING::DynamicSegment, r.segments[1]
  486. assert_equal :controller, r.segments[1].key
  487. assert_equal nil, r.segments[1].default
  488. assert_kind_of ROUTING::DynamicSegment, r.segments[3]
  489. assert_equal :action, r.segments[3].key
  490. assert_equal 'index', r.segments[3].default
  491. assert_kind_of ROUTING::DynamicSegment, r.segments[5]
  492. assert_equal :id, r.segments[5].key
  493. assert r.segments[5].optional?
  494. end
  495. end
  496. def test_slashes_are_implied
  497. routes = [
  498. builder.build('/:controller/:action/:id/', :action => nil),
  499. builder.build('/:controller/:action/:id', :action => nil),
  500. builder.build(':controller/:action/:id', :action => nil),
  501. builder.build('/:controller/:action/:id/', :action => nil)
  502. ]
  503. expected = routes.first.segments.length
  504. routes.each_with_index do |route, i|
  505. found = route.segments.length
  506. assert_equal expected, found, "Route #{i + 1} has #{found} segments, expected #{expected}"
  507. end
  508. end
  509. end
  510. class RoutingTest < Test::Unit::TestCase
  511. def test_possible_controllers
  512. true_controller_paths = ActionController::Routing.controller_paths
  513. ActionController::Routing.use_controllers! nil
  514. silence_warnings do
  515. Object.send(:const_set, :RAILS_ROOT, File.dirname(__FILE__) + '/controller_fixtures')
  516. end
  517. ActionController::Routing.controller_paths = [
  518. RAILS_ROOT, RAILS_ROOT + '/app/controllers', RAILS_ROOT + '/vendor/plugins/bad_plugin/lib'
  519. ]
  520. assert_equal ["admin/user", "plugin", "user"], ActionController::Routing.possible_controllers.sort
  521. ensure
  522. if true_controller_paths
  523. ActionController::Routing.controller_paths = true_controller_paths
  524. end
  525. ActionController::Routing.use_controllers! nil
  526. Object.send(:remove_const, :RAILS_ROOT) rescue nil
  527. end
  528. def test_possible_controllers_are_reset_on_each_load
  529. true_possible_controllers = ActionController::Routing.possible_controllers
  530. true_controller_paths = ActionController::Routing.controller_paths
  531. ActionController::Routing.use_controllers! nil
  532. root = File.dirname(__FILE__) + '/controller_fixtures'
  533. ActionController::Routing.controller_paths = []
  534. assert_equal [], ActionController::Routing.possible_controllers
  535. ActionController::Routing.controller_paths = [
  536. root, root + '/app/controllers', root + '/vendor/plugins/bad_plugin/lib'
  537. ]
  538. ActionController::Routing::Routes.load!
  539. assert_equal ["admin/user", "plugin", "user"], ActionController::Routing.possible_controllers.sort
  540. ensure
  541. ActionController::Routing.controller_paths = true_controller_paths
  542. ActionController::Routing.use_controllers! true_possible_controllers
  543. Object.send(:remove_const, :RAILS_ROOT) rescue nil
  544. ActionController::Routing::Routes.clear!
  545. ActionController::Routing::Routes.load_routes!
  546. end
  547. def test_with_controllers
  548. c = %w(admin/accounts admin/users account pages)
  549. ActionController::Routing.with_controllers c do
  550. assert_equal c, ActionController::Routing.possible_controllers
  551. end
  552. end
  553. def test_normalize_unix_paths
  554. 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)
  555. paths = ActionController::Routing.normalize_paths(load_paths)
  556. assert_equal %w(vendor/rails/railties/builtin/rails_info vendor/rails/actionpack/lib app/controllers app/helpers app/models config .bar lib .), paths
  557. end
  558. def test_normalize_windows_paths
  559. 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)
  560. paths = ActionController::Routing.normalize_paths(load_paths)
  561. assert_equal %w(vendor\\rails\\railties\\builtin\\rails_info vendor\\rails\\actionpack\\lib app\\controllers app\\helpers app\\models config .bar lib .), paths
  562. end
  563. def test_routing_helper_module
  564. assert_kind_of Module, ActionController::Routing::Helpers
  565. h = ActionController::Routing::Helpers
  566. c = Class.new
  567. assert ! c.ancestors.include?(h)
  568. ActionController::Routing::Routes.install_helpers c
  569. assert c.ancestors.include?(h)
  570. end
  571. end
  572. class MockController
  573. attr_accessor :routes
  574. def initialize(routes)
  575. self.routes = routes
  576. end
  577. def url_for(options)
  578. only_path = options.delete(:only_path)
  579. port = options.delete(:port) || 80
  580. port_string = port == 80 ? '' : ":#{port}"
  581. protocol = options.delete(:protocol) || "http"
  582. host = options.delete(:host) || "test.host"
  583. anchor = "##{options.delete(:anchor)}" if options.key?(:anchor)
  584. path = routes.generate(options)
  585. only_path ? "#{path}#{anchor}" : "#{protocol}://#{host}#{port_string}#{path}#{anchor}"
  586. end
  587. def request
  588. @request ||= ActionController::TestRequest.new
  589. end
  590. end
  591. class LegacyRouteSetTests < Test::Unit::TestCase
  592. attr_reader :rs
  593. def setup
  594. # These tests assume optimisation is on, so re-enable it.
  595. ActionController::Base.optimise_named_routes = true
  596. @rs = ::ActionController::Routing::RouteSet.new
  597. ActionController::Routing.use_controllers! %w(content admin/user admin/news_feed)
  598. end
  599. def teardown
  600. @rs.clear!
  601. end
  602. def test_default_setup
  603. @rs.draw {|m| m.connect ':controller/:action/:id' }
  604. assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content"))
  605. assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list"))
  606. assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10"))
  607. assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, rs.recognize_path("/admin/user/show/10"))
  608. assert_equal '/admin/user/show/10', rs.generate(:controller => 'admin/user', :action => 'show', :id => 10)
  609. assert_equal '/admin/user/show', rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
  610. assert_equal '/admin/user/list/10', rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
  611. assert_equal '/admin/stuff', rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
  612. assert_equal '/stuff', rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
  613. end
  614. def test_ignores_leading_slash
  615. @rs.clear!
  616. @rs.draw {|m| m.connect '/:controller/:action/:id'}
  617. test_default_setup
  618. end
  619. def test_time_recognition
  620. # We create many routes to make situation more realistic
  621. @rs = ::ActionController::Routing::RouteSet.new
  622. @rs.draw { |map|
  623. map.frontpage '', :controller => 'search', :action => 'new'
  624. map.resources :videos do |video|
  625. video.resources :comments
  626. video.resource :file, :controller => 'video_file'
  627. video.resource :share, :controller => 'video_shares'
  628. video.resource :abuse, :controller => 'video_abuses'
  629. end
  630. map.resources :abuses, :controller => 'video_abuses'
  631. map.resources :video_uploads
  632. map.resources :video_visits
  633. map.resources :users do |user|
  634. user.resource :settings
  635. user.resources :videos
  636. end
  637. map.resources :channels do |channel|
  638. channel.resources :videos, :controller => 'channel_videos'
  639. end
  640. map.resource :session
  641. map.resource :lost_password
  642. map.search 'search', :controller => 'search'
  643. map.resources :pages
  644. map.connect ':controller/:action/:id'
  645. }
  646. n = 1000
  647. if RunTimeTests
  648. GC.start
  649. rectime = Benchmark.realtime do
  650. n.times do
  651. rs.recognize_path("/videos/1234567", {:method => :get})
  652. rs.recognize_path("/videos/1234567/abuse", {:method => :get})
  653. rs.recognize_path("/users/1234567/settings", {:method => :get})
  654. rs.recognize_path("/channels/1234567", {:method => :get})
  655. rs.recognize_path("/session/new", {:method => :get})
  656. rs.recognize_path("/admin/user/show/10", {:method => :get})
  657. end
  658. end
  659. puts "\n\nRecognition (#{rs.routes.size} routes):"
  660. per_url = rectime / (n * 6)
  661. puts "#{per_url * 1000} ms/url"
  662. puts "#{1 / per_url} url/s\n\n"
  663. end
  664. end
  665. def test_time_generation
  666. n = 5000
  667. if RunTimeTests
  668. GC.start
  669. pairs = [
  670. [{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}],
  671. [{:controller => 'content'}, {:controller => 'content', :action => 'index'}],
  672. [{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}],
  673. [{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}],
  674. [{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}],
  675. [{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}],
  676. [{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}],
  677. [{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}],
  678. ]
  679. p = nil
  680. gentime = Benchmark.realtime do
  681. n.times do
  682. pairs.each {|(a, b)| rs.generate(a, b)}
  683. end
  684. end
  685. puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)"
  686. per_url = gentime / (n * 8)
  687. puts "#{per_url * 1000} ms/url"
  688. puts "#{1 / per_url} url/s\n\n"
  689. end
  690. end
  691. def test_route_with_colon_first
  692. rs.draw do |map|
  693. map.connect '/:controller/:action/:id', :action => 'index', :id => nil
  694. map.connect ':url', :controller => 'tiny_url', :action => 'translate'
  695. end
  696. end
  697. def test_route_with_regexp_for_controller
  698. rs.draw do |map|
  699. map.connect ':controller/:admintoken/:action/:id', :controller => /admin\/.+/
  700. map.connect ':controller/:action/:id'
  701. end
  702. assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"},
  703. rs.recognize_path("/admin/user/foo"))
  704. assert_equal({:controller => "content", :action => "foo"}, rs.recognize_path("/content/foo"))
  705. assert_equal '/admin/user/foo', rs.generate(:controller => "admin/user", :admintoken => "foo", :action => "index")
  706. assert_equal '/content/foo', rs.generate(:controller => "content", :action => "foo")
  707. end
  708. def test_route_with_regexp_and_captures_for_controller
  709. rs.draw do |map|
  710. map.connect ':controller/:action/:id', :controller => /admin\/(accounts|users)/
  711. end
  712. assert_equal({:controller => "admin/accounts", :action => "index"}, rs.recognize_path("/admin/accounts"))
  713. assert_equal({:controller => "admin/users", :action => "index"}, rs.recognize_path("/admin/users"))
  714. assert_raise(ActionController::RoutingError) { rs.recognize_path("/admin/products") }
  715. end
  716. def test_route_with_regexp_and_dot
  717. rs.draw do |map|
  718. map.connect ':controller/:action/:file',
  719. :controller => /admin|user/,
  720. :action => /upload|download/,
  721. :defaults => {:file => nil},
  722. :requirements => {:file => %r{[^/]+(\.[^/]+)?}}
  723. end
  724. # Without a file extension
  725. assert_equal '/user/download/file',
  726. rs.generate(:controller => "user", :action => "download", :file => "file")
  727. assert_equal(
  728. {:controller => "user", :action => "download", :file => "file"},
  729. rs.recognize_path("/user/download/file"))
  730. # Now, let's try a file with an extension, really a dot (.)
  731. assert_equal '/user/download/file.jpg',
  732. rs.generate(
  733. :controller => "user", :action => "download", :file => "file.jpg")
  734. assert_equal(
  735. {:controller => "user", :action => "download", :file => "file.jpg"},
  736. rs.recognize_path("/user/download/file.jpg"))
  737. end
  738. def test_basic_named_route
  739. rs.add_named_route :home, '', :controller => 'content', :action => 'list'
  740. x = setup_for_named_route
  741. assert_equal("http://test.host/",
  742. x.send(:home_url))
  743. end
  744. def test_basic_named_route_with_relative_url_root
  745. rs.add_named_route :home, '', :controller => 'content', :action => 'list'
  746. x = setup_for_named_route
  747. ActionController::Base.relative_url_root = "/foo"
  748. assert_equal("http://test.host/foo/",
  749. x.send(:home_url))
  750. assert_equal "/foo/", x.send(:home_path)
  751. ActionController::Base.relative_url_root = nil
  752. end
  753. def test_named_route_with_option
  754. rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page'
  755. x = setup_for_named_route
  756. assert_equal("http://test.host/page/new%20stuff",
  757. x.send(:page_url, :title => 'new stuff'))
  758. end
  759. def test_named_route_with_default
  760. rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage'
  761. x = setup_for_named_route
  762. assert_equal("http://test.host/page/AboutRails",
  763. x.send(:page_url, :title => "AboutRails"))
  764. end
  765. def test_named_route_with_name_prefix
  766. rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :name_prefix => 'my_'
  767. x = setup_for_named_route
  768. assert_equal("http://test.host/page",
  769. x.send(:my_page_url))
  770. end
  771. def test_named_route_with_path_prefix
  772. rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :path_prefix => 'my'
  773. x = setup_for_named_route
  774. assert_equal("http://test.host/my/page",
  775. x.send(:page_url))
  776. end
  777. def test_named_route_with_blank_path_prefix
  778. rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :path_prefix => ''
  779. x = setup_for_named_route
  780. assert_equal("http://test.host/page",
  781. x.send(:page_url))
  782. end
  783. def test_named_route_with_nested_controller
  784. rs.add_named_route :users, 'admin/user', :controller => 'admin/user', :action => 'index'
  785. x = setup_for_named_route
  786. assert_equal("http://test.host/admin/user",
  787. x.send(:users_url))
  788. end
  789. def test_optimised_named_route_call_never_uses_url_for
  790. rs.add_named_route :users, 'admin/user', :controller => '/admin/user', :action => 'index'
  791. rs.add_named_route :user, 'admin/user/:id', :controller=>'/admin/user', :action=>'show'
  792. x = setup_for_named_route
  793. x.expects(:url_for).never
  794. x.send(:users_url)
  795. x.send(:users_path)
  796. x.send(:user_url, 2, :foo=>"bar")
  797. x.send(:user_path, 3, :bar=>"foo")
  798. end
  799. def test_optimised_named_route_with_host
  800. rs.add_named_route :pages, 'pages', :controller => 'content', :action => 'show_page', :host => 'foo.com'
  801. x = setup_for_named_route
  802. x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages).once
  803. x.send(:pages_url)
  804. end
  805. def setup_for_named_route
  806. klass = Class.new(MockController)
  807. rs.install_helpers(klass)
  808. klass.new(rs)
  809. end
  810. def test_named_route_without_hash
  811. rs.draw do |map|
  812. map.normal ':controller/:action/:id'
  813. end
  814. end
  815. def test_named_route_root
  816. rs.draw do |map|
  817. map.root :controller => "hello"
  818. end
  819. x = setup_for_named_route
  820. assert_equal("http://test.host/", x.send(:root_url))
  821. assert_equal("/", x.send(:root_path))
  822. end
  823. def test_named_route_with_regexps
  824. rs.draw do |map|
  825. map.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show',
  826. :year => /\d+/, :month => /\d+/, :day => /\d+/
  827. map.connect ':controller/:action/:id'
  828. end
  829. x = setup_for_named_route
  830. # assert_equal(
  831. # {:controller => 'page', :action => 'show', :title => 'hi', :use_route => :article, :only_path => false},
  832. # x.send(:article_url, :title => 'hi')
  833. # )
  834. assert_equal(
  835. "http://test.host/page/2005/6/10/hi",
  836. x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
  837. )
  838. end
  839. def test_changing_controller
  840. @rs.draw {|m| m.connect ':controller/:action/:id' }
  841. assert_equal '/admin/stuff/show/10', rs.generate(
  842. {:controller => 'stuff', :action => 'show', :id => 10},
  843. {:controller => 'admin/user', :action => 'index'}
  844. )
  845. end
  846. def test_paths_escaped
  847. rs.draw do |map|
  848. map.path 'file/*path', :controller => 'content', :action => 'show_file'
  849. map.connect ':controller/:action/:id'
  850. end
  851. # No + to space in URI escaping, only for query params.
  852. results = rs.recognize_path "/file/hello+world/how+are+you%3F"
  853. assert results, "Recognition should have succeeded"
  854. assert_equal ['hello+world', 'how+are+you?'], results[:path]
  855. # Use %20 for space instead.
  856. results = rs.recognize_path "/file/hello%20world/how%20are%20you%3F"
  857. assert results, "Recognition should have succeeded"
  858. assert_equal ['hello world', 'how are you?'], results[:path]
  859. results = rs.recognize_path "/file"
  860. assert results, "Recognition should have succeeded"
  861. assert_equal [], results[:path]
  862. end
  863. def test_paths_slashes_unescaped_with_ordered_parameters
  864. rs.add_named_route :path, '/file/*path', :controller => 'content'
  865. # No / to %2F in URI, only for query params.
  866. x = setup_for_named_route
  867. assert_equal("/file/hello/world", x.send(:path_path, 'hello/world'))
  868. end
  869. def test_non_controllers_cannot_be_matched
  870. rs.draw do |map|
  871. map.connect ':controller/:action/:id'
  872. end
  873. assert_raise(ActionController::RoutingError) { rs.recognize_path("/not_a/show/10") }
  874. end
  875. def test_paths_do_not_accept_defaults
  876. assert_raise(ActionController::RoutingError) do
  877. rs.draw do |map|
  878. map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default)
  879. map.connect ':controller/:action/:id'
  880. end
  881. end
  882. rs.draw do |map|
  883. map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => []
  884. map.connect ':controller/:action/:id'
  885. end
  886. end
  887. def test_should_list_options_diff_when_routing_requirements_dont_match
  888. rs.draw do |map|
  889. map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
  890. end
  891. exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post") }
  892. assert_match /^post_url failed to generate/, exception.message
  893. from_match = exception.message.match(/from \{[^\}]+\}/).to_s
  894. assert_match /:bad_param=>"foo"/, from_match
  895. assert_match /:action=>"show"/, from_match
  896. assert_match /:controller=>"post"/, from_match
  897. expected_match = exception.message.match(/expected: \{[^\}]+\}/).to_s
  898. assert_no_match /:bad_param=>"foo"/, expected_match
  899. assert_match /:action=>"show"/, expected_match
  900. assert_match /:controller=>"post"/, expected_match
  901. diff_match = exception.message.match(/diff: \{[^\}]+\}/).to_s
  902. assert_match /:bad_param=>"foo"/, diff_match
  903. assert_no_match /:action=>"show"/, diff_match
  904. assert_no_match /:controller=>"post"/, diff_match
  905. end
  906. # this specifies the case where your formerly would get a very confusing error message with an empty diff
  907. def test_should_have_better_error_message_when_options_diff_is_empty
  908. rs.draw do |map|
  909. map.content '/content/:query', :controller => 'content', :action => 'show'
  910. end
  911. exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'content', :action => 'show', :use_route => "content") }
  912. assert_match %r[:action=>"show"], exception.message
  913. assert_match %r[:controller=>"content"], exception.message
  914. assert_match %r[you may have ambiguous routes, or you may need to supply additional parameters for this route], exception.message
  915. assert_match %r[content_url has the following required parameters: \["content", :query\] - are they all satisfied?], exception.message
  916. end
  917. def test_dynamic_path_allowed
  918. rs.draw do |map|
  919. map.connect '*path', :controller => 'content', :action => 'show_file'
  920. end
  921. assert_equal '/pages/boo', rs.generate(:controller => 'content', :action => 'show_file', :path => %w(pages boo))
  922. end
  923. def test_dynamic_recall_paths_allowed
  924. rs.draw do |map|
  925. map.connect '*path', :controller => 'content', :action => 'show_file'
  926. end
  927. recall_path = ActionController::Routing::PathSegment::Result.new(%w(pages boo))
  928. assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => recall_path)
  929. end
  930. def test_backwards
  931. rs.draw do |map|
  932. map.connect 'page/:id/:action', :controller => 'pages', :action => 'show'
  933. map.connect ':controller/:action/:id'
  934. end
  935. assert_equal '/page/20', rs.generate({:id => 20}, {:controller => 'pages', :action => 'show'})
  936. assert_equal '/page/20', rs.generate(:controller => 'pages', :id => 20, :action => 'show')
  937. assert_equal '/pages/boo', rs.generate(:controller => 'pages', :action => 'boo')
  938. end
  939. def test_route_with_fixnum_default
  940. rs.draw do |map|
  941. map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
  942. map.connect ':controller/:action/:id'
  943. end
  944. assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page')
  945. assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => 1)
  946. assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => '1')
  947. assert_equal '/page/10', rs.generate(:controller => 'content', :action => 'show_page', :id => 10)
  948. assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page"))
  949. assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page/1"))
  950. assert_equal({:controller => "content", :action => 'show_page', :id => '10'}, rs.recognize_path("/page/10"))
  951. end
  952. # For newer revision
  953. def test_route_with_text_default
  954. rs.draw do |map|
  955. map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
  956. map.connect ':controller/:action/:id'
  957. end
  958. assert_equal '/page/foo', rs.generate(:controller => 'content', :action => 'show_page', :id => 'foo')
  959. assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, rs.recognize_path("/page/foo"))
  960. token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian
  961. token.force_encoding("UTF-8") if token.respond_to?(:force_encoding)
  962. escaped_token = CGI::escape(token)
  963. assert_equal '/page/' + escaped_token, rs.generate(:controller => 'content', :action => 'show_page', :id => token)
  964. assert_equal({:controller => "content", :action => 'show_page', :id => token}, rs.recognize_path("/page/#{escaped_token}"))
  965. end
  966. def test_action_expiry
  967. @rs.draw {|m| m.connect ':controller/:action/:id' }
  968. assert_equal '/content', rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
  969. end
  970. def test_recognition_with_uppercase_controller_name
  971. @rs.draw {|m| m.connect ':controller/:action/:id' }
  972. assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/Content"))
  973. assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/ConTent/list"))
  974. assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/CONTENT/show/10"))
  975. # these used to work, before the routes rewrite, but support for this was pulled in the new version...
  976. #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/NewsFeed"))
  977. #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/News_Feed"))
  978. end
  979. def test_requirement_should_prevent_optional_id
  980. rs.draw do |map|
  981. map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
  982. end
  983. assert_equal '/post/10', rs.generate(:controller => 'post', :action => 'show', :id => 10)
  984. assert_raise ActionController::RoutingError do
  985. rs.generate(:controller => 'post', :action => 'show')
  986. end
  987. end
  988. def test_both_requirement_and_optional
  989. rs.draw do |map|
  990. map.blog('test/:year', :controller => 'post', :action => 'show',
  991. :defaults => { :year => nil },
  992. :requirements => { :year => /\d{4}/ }
  993. )
  994. map.connect ':controller/:action/:id'
  995. end
  996. assert_equal '/test', rs.generate(:controller => 'post', :action => 'show')
  997. assert_equal '/test', rs.generate(:controller => 'post', :action => 'show', :year => nil)
  998. x = setup_for_named_route
  999. assert_equal("http://test.host/test",
  1000. x.send(:blog_url))
  1001. end
  1002. def test_set_to_nil_forgets
  1003. rs.draw do |map|
  1004. map.connect 'pages/:year/:month/:day', :controller => 'content', :action => 'list_pages', :month => nil, :day => nil
  1005. map.connect ':controller/:action/:id'
  1006. end
  1007. assert_equal '/pages/2005',
  1008. rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005)
  1009. assert_equal '/pages/2005/6',
  1010. rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6)
  1011. assert_equal '/pages/2005/6/12',
  1012. rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12)
  1013. assert_equal '/pages/2005/6/4',
  1014. rs.generate({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
  1015. assert_equal '/pages/2005/6',
  1016. rs.generate({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
  1017. assert_equal '/pages/2005',
  1018. rs.generate({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
  1019. end
  1020. def test_url_with_no_action_specified
  1021. rs.draw do |map|
  1022. map.connect '', :controller => 'content'
  1023. map.connect ':controller/:action/:id'
  1024. end
  1025. assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
  1026. assert_equal '/', rs.generate(:controller => 'content')
  1027. end
  1028. def test_named_url_with_no_action_specified
  1029. rs.draw do |map|
  1030. map.home '', :controller => 'content'
  1031. map.connect ':controller/:action/:id'
  1032. end
  1033. assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
  1034. assert_equal '/', rs.generate(:controller => 'content')
  1035. x = setup_for_named_route
  1036. assert_equal("http://test.host/",
  1037. x.send(:home_url))
  1038. end
  1039. def test_url_generated_when_forgetting_action
  1040. [{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash|
  1041. rs.draw do |map|
  1042. map.home '', hash
  1043. map.connect ':controller/:action/:id'
  1044. end
  1045. assert_equal '/', rs.generate({:action => nil}, {:controller => 'content', :action => 'hello'})
  1046. assert_equal '/', rs.generate({:controller => 'content'})
  1047. assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
  1048. end
  1049. end
  1050. def test_named_route_method
  1051. rs.draw do |map|
  1052. map.categories 'categories', :controller => 'content', :action => 'categories'
  1053. map.connect ':controller/:action/:id'
  1054. end
  1055. assert_equal '/categories', rs.generate(:controller => 'content', :action => 'categories')
  1056. assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
  1057. end
  1058. def test_named_routes_array
  1059. test_named_route_method
  1060. assert_equal [:categories], rs.named_routes.names
  1061. end
  1062. def test_nil_defaults
  1063. rs.draw do |map|
  1064. map.connect 'journal',
  1065. :controller => 'content',
  1066. :action => 'list_journal',
  1067. :date => nil, :user_id => nil
  1068. map.connect ':controller/:action/:id'
  1069. end
  1070. assert_equal '/journal', rs.generate(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil)
  1071. end
  1072. def setup_request_method_routes_for(method)
  1073. @request = ActionController::TestRequest.new
  1074. @request.env["REQUEST_METHOD"] = method
  1075. @request.request_uri = "/match"
  1076. rs.draw do |r|
  1077. r.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get }
  1078. r.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post }
  1079. r.connect '/match', :controller => 'books', :action => 'put', :conditions => { :method => :put }
  1080. r.connect '/match', :controller => 'books', :action => 'delete', :conditions => { :method => :delete }
  1081. end
  1082. end
  1083. %w(GET POST PUT DELETE).each do |request_method|
  1084. define_method("test_request_method_recognized_with_#{request_method}") do
  1085. begin
  1086. Object.const_set(:BooksController, Class.new(ActionController::Base))
  1087. setup_request_method_routes_for(request_method)
  1088. assert_nothing_raised { rs.recognize(@request) }
  1089. assert_equal request_method.downcase, @request.path_parameters[:action]
  1090. ensure
  1091. Object.send(:remove_const, :BooksController) rescue nil
  1092. end
  1093. end
  1094. end
  1095. def test_recognize_array_of_methods
  1096. Object.const_set(:BooksController, Class.new(ActionController::Base))
  1097. rs.draw do |r|
  1098. r.connect '/match', :controller => 'books', :action => 'get_or_post', :conditions => { :method => [:get, :post] }
  1099. r.connect '/match', :controller => 'books', :action => 'not_get_or_post'
  1100. end
  1101. @request = ActionController::TestRequest.new
  1102. @request.env["REQUEST_METHOD"] = 'POST'
  1103. @request.request_uri = "/match"
  1104. assert_nothing_raised { rs.recognize(@request) }
  1105. assert_equal 'get_or_post', @request.path_parameters[:action]
  1106. # have to recreate or else the RouteSet uses a cached version:
  1107. @request = ActionController::TestRequest.new
  1108. @request.env["REQUEST_METHOD"] = 'PUT'
  1109. @request.request_uri = "/match"
  1110. assert_nothing_raised { rs.recognize(@request) }
  1111. assert_equal 'not_get_or_post', @request.path_parameters[:action]
  1112. ensure
  1113. Object.send(:remove_const, :BooksController) rescue nil
  1114. end
  1115. def test_subpath_recognized
  1116. Object.const_set(:SubpathBooksController, Class.new(ActionController::Base))
  1117. rs.draw do |r|
  1118. r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
  1119. r.connect '/items/:id/:action', :controller => 'subpath_books'
  1120. r.connect '/posts/new/:action', :controller => 'subpat…

Large files files are truncated, but you can click here to view the full file