PageRenderTime 55ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/integration-tests/apps/rails2/frozen/vendor/rails/actionpack/test/controller/routing_test.rb

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