PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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