PageRenderTime 66ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Languages/Ruby/Tests/Libraries/Rails-3.0.0/actionpack/test/controller/routing_test.rb

http://github.com/IronLanguages/main
Ruby | 1914 lines | 1564 code | 326 blank | 24 comment | 4 complexity | 88e9885e627af2734d4d2c564fa74ec2 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception

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

  1. # encoding: utf-8
  2. require 'abstract_unit'
  3. require 'controller/fake_controllers'
  4. require 'active_support/dependencies'
  5. class MilestonesController < ActionController::Base
  6. def index() head :ok end
  7. alias_method :show, :index
  8. def rescue_action(e) raise e end
  9. end
  10. ROUTING = ActionController::Routing
  11. # See RFC 3986, section 3.3 for allowed path characters.
  12. class UriReservedCharactersRoutingTest < Test::Unit::TestCase
  13. def setup
  14. @set = ActionController::Routing::RouteSet.new
  15. @set.draw do |map|
  16. map.connect ':controller/:action/:variable/*additional'
  17. end
  18. safe, unsafe = %w(: @ & = + $ , ;), %w(^ ? # [ ])
  19. hex = unsafe.map { |char| '%' + char.unpack('H2').first.upcase }
  20. @segment = "#{safe.join}#{unsafe.join}".freeze
  21. @escaped = "#{safe.join}#{hex.join}".freeze
  22. end
  23. def test_route_generation_escapes_unsafe_path_characters
  24. @set.generate(:controller => "content", :action => "act#{@segment}ion", :variable => "variable", :additional => "foo")
  25. assert_equal "/content/act#{@escaped}ion/var#{@escaped}iable/add#{@escaped}itional-1/add#{@escaped}itional-2",
  26. @set.generate(:controller => "content",
  27. :action => "act#{@segment}ion",
  28. :variable => "var#{@segment}iable",
  29. :additional => ["add#{@segment}itional-1", "add#{@segment}itional-2"])
  30. end
  31. def test_route_recognition_unescapes_path_components
  32. options = { :controller => "content",
  33. :action => "act#{@segment}ion",
  34. :variable => "var#{@segment}iable",
  35. :additional => ["add#{@segment}itional-1", "add#{@segment}itional-2"] }
  36. assert_equal options, @set.recognize_path("/content/act#{@escaped}ion/var#{@escaped}iable/add#{@escaped}itional-1/add#{@escaped}itional-2")
  37. end
  38. def test_route_generation_allows_passing_non_string_values_to_generated_helper
  39. assert_equal "/content/action/variable/1/2", @set.generate(:controller => "content",
  40. :action => "action",
  41. :variable => "variable",
  42. :additional => [1, 2])
  43. end
  44. end
  45. class MockController
  46. def self.build(helpers)
  47. Class.new do
  48. def url_for(options)
  49. options[:protocol] ||= "http"
  50. options[:host] ||= "test.host"
  51. super(options)
  52. end
  53. include helpers
  54. end
  55. end
  56. end
  57. class LegacyRouteSetTests < Test::Unit::TestCase
  58. attr_reader :rs
  59. def setup
  60. @rs = ::ActionController::Routing::RouteSet.new
  61. end
  62. def teardown
  63. @rs.clear!
  64. end
  65. def test_default_setup
  66. @rs.draw {|m| m.connect ':controller/:action/:id' }
  67. assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content"))
  68. assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list"))
  69. assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10"))
  70. assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, rs.recognize_path("/admin/user/show/10"))
  71. assert_equal '/admin/user/show/10', rs.generate(:controller => 'admin/user', :action => 'show', :id => 10)
  72. assert_equal '/admin/user/show', rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
  73. assert_equal '/admin/user/list/10', rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'})
  74. assert_equal '/admin/stuff', rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
  75. assert_equal '/stuff', rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'})
  76. end
  77. def test_ignores_leading_slash
  78. @rs.clear!
  79. @rs.draw {|m| m.connect '/:controller/:action/:id'}
  80. test_default_setup
  81. end
  82. def test_time_recognition
  83. # We create many routes to make situation more realistic
  84. @rs = ::ActionController::Routing::RouteSet.new
  85. @rs.draw { |map|
  86. map.frontpage '', :controller => 'search', :action => 'new'
  87. map.resources :videos do |video|
  88. video.resources :comments
  89. video.resource :file, :controller => 'video_file'
  90. video.resource :share, :controller => 'video_shares'
  91. video.resource :abuse, :controller => 'video_abuses'
  92. end
  93. map.resources :abuses, :controller => 'video_abuses'
  94. map.resources :video_uploads
  95. map.resources :video_visits
  96. map.resources :users do |user|
  97. user.resource :settings
  98. user.resources :videos
  99. end
  100. map.resources :channels do |channel|
  101. channel.resources :videos, :controller => 'channel_videos'
  102. end
  103. map.resource :session
  104. map.resource :lost_password
  105. map.search 'search', :controller => 'search'
  106. map.resources :pages
  107. map.connect ':controller/:action/:id'
  108. }
  109. end
  110. def test_route_with_colon_first
  111. rs.draw do |map|
  112. map.connect '/:controller/:action/:id', :action => 'index', :id => nil
  113. map.connect ':url', :controller => 'tiny_url', :action => 'translate'
  114. end
  115. end
  116. def test_route_with_regexp_for_controller
  117. rs.draw do |map|
  118. map.connect ':controller/:admintoken/:action/:id', :controller => /admin\/.+/
  119. map.connect ':controller/:action/:id'
  120. end
  121. assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"},
  122. rs.recognize_path("/admin/user/foo"))
  123. assert_equal({:controller => "content", :action => "foo"}, rs.recognize_path("/content/foo"))
  124. assert_equal '/admin/user/foo', rs.generate(:controller => "admin/user", :admintoken => "foo", :action => "index")
  125. assert_equal '/content/foo', rs.generate(:controller => "content", :action => "foo")
  126. end
  127. def test_route_with_regexp_and_captures_for_controller
  128. rs.draw do |map|
  129. map.connect ':controller/:action/:id', :controller => /admin\/(accounts|users)/
  130. end
  131. assert_equal({:controller => "admin/accounts", :action => "index"}, rs.recognize_path("/admin/accounts"))
  132. assert_equal({:controller => "admin/users", :action => "index"}, rs.recognize_path("/admin/users"))
  133. assert_raise(ActionController::RoutingError) { rs.recognize_path("/admin/products") }
  134. end
  135. def test_route_with_regexp_and_dot
  136. rs.draw do |map|
  137. map.connect ':controller/:action/:file',
  138. :controller => /admin|user/,
  139. :action => /upload|download/,
  140. :defaults => {:file => nil},
  141. :requirements => {:file => %r{[^/]+(\.[^/]+)?}}
  142. end
  143. # Without a file extension
  144. assert_equal '/user/download/file',
  145. rs.generate(:controller => "user", :action => "download", :file => "file")
  146. assert_equal(
  147. {:controller => "user", :action => "download", :file => "file"},
  148. rs.recognize_path("/user/download/file"))
  149. # Now, let's try a file with an extension, really a dot (.)
  150. assert_equal '/user/download/file.jpg',
  151. rs.generate(
  152. :controller => "user", :action => "download", :file => "file.jpg")
  153. assert_equal(
  154. {:controller => "user", :action => "download", :file => "file.jpg"},
  155. rs.recognize_path("/user/download/file.jpg"))
  156. end
  157. def test_basic_named_route
  158. rs.draw do |map|
  159. map.home '', :controller => 'content', :action => 'list'
  160. end
  161. x = setup_for_named_route
  162. assert_equal("http://test.host/",
  163. x.send(:home_url))
  164. end
  165. def test_named_route_with_option
  166. rs.draw do |map|
  167. map.page 'page/:title', :controller => 'content', :action => 'show_page'
  168. end
  169. x = setup_for_named_route
  170. assert_equal("http://test.host/page/new%20stuff",
  171. x.send(:page_url, :title => 'new stuff'))
  172. end
  173. def test_named_route_with_default
  174. rs.draw do |map|
  175. map.page 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage'
  176. end
  177. x = setup_for_named_route
  178. assert_equal("http://test.host/page/AboutRails",
  179. x.send(:page_url, :title => "AboutRails"))
  180. end
  181. def test_named_route_with_name_prefix
  182. rs.draw do |map|
  183. map.page 'page', :controller => 'content', :action => 'show_page', :name_prefix => 'my_'
  184. end
  185. x = setup_for_named_route
  186. assert_equal("http://test.host/page",
  187. x.send(:my_page_url))
  188. end
  189. def test_named_route_with_path_prefix
  190. rs.draw do |map|
  191. map.page 'page', :controller => 'content', :action => 'show_page', :path_prefix => 'my'
  192. end
  193. x = setup_for_named_route
  194. assert_equal("http://test.host/my/page",
  195. x.send(:page_url))
  196. end
  197. def test_named_route_with_blank_path_prefix
  198. rs.draw do |map|
  199. map.page 'page', :controller => 'content', :action => 'show_page', :path_prefix => ''
  200. end
  201. x = setup_for_named_route
  202. assert_equal("http://test.host/page",
  203. x.send(:page_url))
  204. end
  205. def test_named_route_with_nested_controller
  206. rs.draw do |map|
  207. map.users 'admin/user', :controller => 'admin/user', :action => 'index'
  208. end
  209. x = setup_for_named_route
  210. assert_equal("http://test.host/admin/user",
  211. x.send(:users_url))
  212. end
  213. def test_optimised_named_route_with_host
  214. rs.draw do |map|
  215. map.pages 'pages', :controller => 'content', :action => 'show_page', :host => 'foo.com'
  216. end
  217. x = setup_for_named_route
  218. x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages).once
  219. x.send(:pages_url)
  220. end
  221. def setup_for_named_route
  222. MockController.build(rs.url_helpers).new
  223. end
  224. def test_named_route_without_hash
  225. rs.draw do |map|
  226. map.normal ':controller/:action/:id'
  227. end
  228. end
  229. def test_named_route_root
  230. rs.draw do |map|
  231. map.root :controller => "hello"
  232. end
  233. x = setup_for_named_route
  234. assert_equal("http://test.host/", x.send(:root_url))
  235. assert_equal("/", x.send(:root_path))
  236. end
  237. def test_named_route_with_regexps
  238. rs.draw do |map|
  239. map.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show',
  240. :year => /\d+/, :month => /\d+/, :day => /\d+/
  241. map.connect ':controller/:action/:id'
  242. end
  243. x = setup_for_named_route
  244. # assert_equal(
  245. # {:controller => 'page', :action => 'show', :title => 'hi', :use_route => :article, :only_path => false},
  246. # x.send(:article_url, :title => 'hi')
  247. # )
  248. assert_equal(
  249. "http://test.host/page/2005/6/10/hi",
  250. x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
  251. )
  252. end
  253. def test_changing_controller
  254. @rs.draw {|m| m.connect ':controller/:action/:id' }
  255. assert_equal '/admin/stuff/show/10', rs.generate(
  256. {:controller => 'stuff', :action => 'show', :id => 10},
  257. {:controller => 'admin/user', :action => 'index'}
  258. )
  259. end
  260. def test_paths_escaped
  261. rs.draw do |map|
  262. map.path 'file/*path', :controller => 'content', :action => 'show_file'
  263. map.connect ':controller/:action/:id'
  264. end
  265. # No + to space in URI escaping, only for query params.
  266. results = rs.recognize_path "/file/hello+world/how+are+you%3F"
  267. assert results, "Recognition should have succeeded"
  268. assert_equal ['hello+world', 'how+are+you?'], results[:path]
  269. # Use %20 for space instead.
  270. results = rs.recognize_path "/file/hello%20world/how%20are%20you%3F"
  271. assert results, "Recognition should have succeeded"
  272. assert_equal ['hello world', 'how are you?'], results[:path]
  273. end
  274. def test_paths_slashes_unescaped_with_ordered_parameters
  275. rs.draw do |map|
  276. map.path '/file/*path', :controller => 'content'
  277. end
  278. # No / to %2F in URI, only for query params.
  279. x = setup_for_named_route
  280. assert_equal("/file/hello/world", x.send(:path_path, ['hello', 'world']))
  281. end
  282. def test_non_controllers_cannot_be_matched
  283. rs.draw do |map|
  284. map.connect ':controller/:action/:id'
  285. end
  286. assert_raise(ActionController::RoutingError) { rs.recognize_path("/not_a/show/10") }
  287. end
  288. def test_paths_do_not_accept_defaults
  289. assert_raise(ActionController::RoutingError) do
  290. rs.draw do |map|
  291. map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default)
  292. map.connect ':controller/:action/:id'
  293. end
  294. end
  295. rs.draw do |map|
  296. map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => []
  297. map.connect ':controller/:action/:id'
  298. end
  299. end
  300. def test_should_list_options_diff_when_routing_requirements_dont_match
  301. rs.draw do |map|
  302. map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
  303. end
  304. assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post") }
  305. end
  306. def test_dynamic_path_allowed
  307. rs.draw do |map|
  308. map.connect '*path', :controller => 'content', :action => 'show_file'
  309. end
  310. assert_equal '/pages/boo', rs.generate(:controller => 'content', :action => 'show_file', :path => %w(pages boo))
  311. end
  312. def test_dynamic_recall_paths_allowed
  313. rs.draw do |map|
  314. map.connect '*path', :controller => 'content', :action => 'show_file'
  315. end
  316. assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => %w(pages boo))
  317. end
  318. def test_backwards
  319. rs.draw do |map|
  320. map.connect 'page/:id/:action', :controller => 'pages', :action => 'show'
  321. map.connect ':controller/:action/:id'
  322. end
  323. assert_equal '/page/20', rs.generate({:id => 20}, {:controller => 'pages', :action => 'show'})
  324. assert_equal '/page/20', rs.generate(:controller => 'pages', :id => 20, :action => 'show')
  325. assert_equal '/pages/boo', rs.generate(:controller => 'pages', :action => 'boo')
  326. end
  327. def test_route_with_fixnum_default
  328. rs.draw do |map|
  329. map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
  330. map.connect ':controller/:action/:id'
  331. end
  332. assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page')
  333. assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => 1)
  334. assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => '1')
  335. assert_equal '/page/10', rs.generate(:controller => 'content', :action => 'show_page', :id => 10)
  336. assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page"))
  337. assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page/1"))
  338. assert_equal({:controller => "content", :action => 'show_page', :id => '10'}, rs.recognize_path("/page/10"))
  339. end
  340. # For newer revision
  341. def test_route_with_text_default
  342. rs.draw do |map|
  343. map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1
  344. map.connect ':controller/:action/:id'
  345. end
  346. assert_equal '/page/foo', rs.generate(:controller => 'content', :action => 'show_page', :id => 'foo')
  347. assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, rs.recognize_path("/page/foo"))
  348. token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian
  349. token.force_encoding(Encoding::BINARY) if token.respond_to?(:force_encoding)
  350. escaped_token = CGI::escape(token)
  351. assert_equal '/page/' + escaped_token, rs.generate(:controller => 'content', :action => 'show_page', :id => token)
  352. assert_equal({:controller => "content", :action => 'show_page', :id => token}, rs.recognize_path("/page/#{escaped_token}"))
  353. end
  354. def test_action_expiry
  355. @rs.draw {|m| m.connect ':controller/:action/:id' }
  356. assert_equal '/content', rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'})
  357. end
  358. def test_requirement_should_prevent_optional_id
  359. rs.draw do |map|
  360. map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/}
  361. end
  362. assert_equal '/post/10', rs.generate(:controller => 'post', :action => 'show', :id => 10)
  363. assert_raise ActionController::RoutingError do
  364. rs.generate(:controller => 'post', :action => 'show')
  365. end
  366. end
  367. def test_both_requirement_and_optional
  368. rs.draw do |map|
  369. map.blog('test/:year', :controller => 'post', :action => 'show',
  370. :defaults => { :year => nil },
  371. :requirements => { :year => /\d{4}/ }
  372. )
  373. map.connect ':controller/:action/:id'
  374. end
  375. assert_equal '/test', rs.generate(:controller => 'post', :action => 'show')
  376. assert_equal '/test', rs.generate(:controller => 'post', :action => 'show', :year => nil)
  377. x = setup_for_named_route
  378. assert_equal("http://test.host/test",
  379. x.send(:blog_url))
  380. end
  381. def test_set_to_nil_forgets
  382. rs.draw do |map|
  383. map.connect 'pages/:year/:month/:day', :controller => 'content', :action => 'list_pages', :month => nil, :day => nil
  384. map.connect ':controller/:action/:id'
  385. end
  386. assert_equal '/pages/2005',
  387. rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005)
  388. assert_equal '/pages/2005/6',
  389. rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6)
  390. assert_equal '/pages/2005/6/12',
  391. rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12)
  392. assert_equal '/pages/2005/6/4',
  393. rs.generate({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
  394. assert_equal '/pages/2005/6',
  395. rs.generate({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
  396. assert_equal '/pages/2005',
  397. rs.generate({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'})
  398. end
  399. def test_url_with_no_action_specified
  400. rs.draw do |map|
  401. map.connect '', :controller => 'content'
  402. map.connect ':controller/:action/:id'
  403. end
  404. assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
  405. assert_equal '/', rs.generate(:controller => 'content')
  406. end
  407. def test_named_url_with_no_action_specified
  408. rs.draw do |map|
  409. map.home '', :controller => 'content'
  410. map.connect ':controller/:action/:id'
  411. end
  412. assert_equal '/', rs.generate(:controller => 'content', :action => 'index')
  413. assert_equal '/', rs.generate(:controller => 'content')
  414. x = setup_for_named_route
  415. assert_equal("http://test.host/",
  416. x.send(:home_url))
  417. end
  418. def test_url_generated_when_forgetting_action
  419. [{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash|
  420. rs.draw do |map|
  421. map.home '', hash
  422. map.connect ':controller/:action/:id'
  423. end
  424. assert_equal '/', rs.generate({:action => nil}, {:controller => 'content', :action => 'hello'})
  425. assert_equal '/', rs.generate({:controller => 'content'})
  426. assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
  427. end
  428. end
  429. def test_named_route_method
  430. rs.draw do |map|
  431. map.categories 'categories', :controller => 'content', :action => 'categories'
  432. map.connect ':controller/:action/:id'
  433. end
  434. assert_equal '/categories', rs.generate(:controller => 'content', :action => 'categories')
  435. assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'})
  436. end
  437. def test_named_routes_array
  438. test_named_route_method
  439. assert_equal [:categories], rs.named_routes.names
  440. end
  441. def test_nil_defaults
  442. rs.draw do |map|
  443. map.connect 'journal',
  444. :controller => 'content',
  445. :action => 'list_journal',
  446. :date => nil, :user_id => nil
  447. map.connect ':controller/:action/:id'
  448. end
  449. assert_equal '/journal', rs.generate(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil)
  450. end
  451. def setup_request_method_routes_for(method)
  452. rs.draw do |r|
  453. r.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get }
  454. r.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post }
  455. r.connect '/match', :controller => 'books', :action => 'put', :conditions => { :method => :put }
  456. r.connect '/match', :controller => 'books', :action => 'delete', :conditions => { :method => :delete }
  457. end
  458. end
  459. %w(GET POST PUT DELETE).each do |request_method|
  460. define_method("test_request_method_recognized_with_#{request_method}") do
  461. setup_request_method_routes_for(request_method)
  462. params = rs.recognize_path("/match", :method => request_method)
  463. assert_equal request_method.downcase, params[:action]
  464. end
  465. end
  466. def test_recognize_array_of_methods
  467. rs.draw do |r|
  468. r.connect '/match', :controller => 'books', :action => 'get_or_post', :conditions => { :method => [:get, :post] }
  469. r.connect '/match', :controller => 'books', :action => 'not_get_or_post'
  470. end
  471. params = rs.recognize_path("/match", :method => :post)
  472. assert_equal 'get_or_post', params[:action]
  473. params = rs.recognize_path("/match", :method => :put)
  474. assert_equal 'not_get_or_post', params[:action]
  475. end
  476. def test_subpath_recognized
  477. rs.draw do |r|
  478. r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
  479. r.connect '/items/:id/:action', :controller => 'subpath_books'
  480. r.connect '/posts/new/:action', :controller => 'subpath_books'
  481. r.connect '/posts/:id', :controller => 'subpath_books', :action => "show"
  482. end
  483. hash = rs.recognize_path "/books/17/edit"
  484. assert_not_nil hash
  485. assert_equal %w(subpath_books 17 edit), [hash[:controller], hash[:id], hash[:action]]
  486. hash = rs.recognize_path "/items/3/complete"
  487. assert_not_nil hash
  488. assert_equal %w(subpath_books 3 complete), [hash[:controller], hash[:id], hash[:action]]
  489. hash = rs.recognize_path "/posts/new/preview"
  490. assert_not_nil hash
  491. assert_equal %w(subpath_books preview), [hash[:controller], hash[:action]]
  492. hash = rs.recognize_path "/posts/7"
  493. assert_not_nil hash
  494. assert_equal %w(subpath_books show 7), [hash[:controller], hash[:action], hash[:id]]
  495. end
  496. def test_subpath_generated
  497. rs.draw do |r|
  498. r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit'
  499. r.connect '/items/:id/:action', :controller => 'subpath_books'
  500. r.connect '/posts/new/:action', :controller => 'subpath_books'
  501. end
  502. assert_equal "/books/7/edit", rs.generate(:controller => "subpath_books", :id => 7, :action => "edit")
  503. assert_equal "/items/15/complete", rs.generate(:controller => "subpath_books", :id => 15, :action => "complete")
  504. assert_equal "/posts/new/preview", rs.generate(:controller => "subpath_books", :action => "preview")
  505. end
  506. def test_failed_requirements_raises_exception_with_violated_requirements
  507. rs.draw do |r|
  508. r.foo_with_requirement 'foos/:id', :controller=>'foos', :requirements=>{:id=>/\d+/}
  509. end
  510. x = setup_for_named_route
  511. assert_raise(ActionController::RoutingError) do
  512. x.send(:foo_with_requirement_url, "I am Against the requirements")
  513. end
  514. end
  515. def test_routes_changed_correctly_after_clear
  516. rs = ::ActionController::Routing::RouteSet.new
  517. rs.draw do |r|
  518. r.connect 'ca', :controller => 'ca', :action => "aa"
  519. r.connect 'cb', :controller => 'cb', :action => "ab"
  520. r.connect 'cc', :controller => 'cc', :action => "ac"
  521. r.connect ':controller/:action/:id'
  522. r.connect ':controller/:action/:id.:format'
  523. end
  524. hash = rs.recognize_path "/cc"
  525. assert_not_nil hash
  526. assert_equal %w(cc ac), [hash[:controller], hash[:action]]
  527. rs.draw do |r|
  528. r.connect 'cb', :controller => 'cb', :action => "ab"
  529. r.connect 'cc', :controller => 'cc', :action => "ac"
  530. r.connect ':controller/:action/:id'
  531. r.connect ':controller/:action/:id.:format'
  532. end
  533. hash = rs.recognize_path "/cc"
  534. assert_not_nil hash
  535. assert_equal %w(cc ac), [hash[:controller], hash[:action]]
  536. end
  537. end
  538. class RouteSetTest < ActiveSupport::TestCase
  539. def set
  540. @set ||= ROUTING::RouteSet.new
  541. end
  542. def request
  543. @request ||= ActionController::TestRequest.new
  544. end
  545. def default_route_set
  546. @default_route_set ||= begin
  547. set = ROUTING::RouteSet.new
  548. set.draw do |map|
  549. map.connect '/:controller/:action/:id/'
  550. end
  551. set
  552. end
  553. end
  554. def test_generate_extras
  555. set.draw { |m| m.connect ':controller/:action/:id' }
  556. path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
  557. assert_equal "/foo/bar/15", path
  558. assert_equal %w(that this), extras.map { |e| e.to_s }.sort
  559. end
  560. def test_extra_keys
  561. set.draw { |m| m.connect ':controller/:action/:id' }
  562. extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
  563. assert_equal %w(that this), extras.map { |e| e.to_s }.sort
  564. end
  565. def test_generate_extras_not_first
  566. set.draw do |map|
  567. map.connect ':controller/:action/:id.:format'
  568. map.connect ':controller/:action/:id'
  569. end
  570. path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
  571. assert_equal "/foo/bar/15", path
  572. assert_equal %w(that this), extras.map { |e| e.to_s }.sort
  573. end
  574. def test_generate_not_first
  575. set.draw do |map|
  576. map.connect ':controller/:action/:id.:format'
  577. map.connect ':controller/:action/:id'
  578. end
  579. assert_equal "/foo/bar/15?this=hello", set.generate(:controller => "foo", :action => "bar", :id => 15, :this => "hello")
  580. end
  581. def test_extra_keys_not_first
  582. set.draw do |map|
  583. map.connect ':controller/:action/:id.:format'
  584. map.connect ':controller/:action/:id'
  585. end
  586. extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world")
  587. assert_equal %w(that this), extras.map { |e| e.to_s }.sort
  588. end
  589. def test_draw
  590. assert_equal 0, set.routes.size
  591. set.draw do |map|
  592. map.connect '/hello/world', :controller => 'a', :action => 'b'
  593. end
  594. assert_equal 1, set.routes.size
  595. end
  596. def test_draw_symbol_controller_name
  597. assert_equal 0, set.routes.size
  598. set.draw do |map|
  599. map.connect '/users/index', :controller => :users, :action => :index
  600. end
  601. params = set.recognize_path('/users/index', :method => :get)
  602. assert_equal 1, set.routes.size
  603. end
  604. def test_named_draw
  605. assert_equal 0, set.routes.size
  606. set.draw do |map|
  607. map.hello '/hello/world', :controller => 'a', :action => 'b'
  608. end
  609. assert_equal 1, set.routes.size
  610. assert_equal set.routes.first, set.named_routes[:hello]
  611. end
  612. def test_later_named_routes_take_precedence
  613. set.draw do |map|
  614. map.hello '/hello/world', :controller => 'a', :action => 'b'
  615. map.hello '/hello', :controller => 'a', :action => 'b'
  616. end
  617. assert_equal set.routes.last, set.named_routes[:hello]
  618. end
  619. def setup_named_route_test
  620. set.draw do |map|
  621. map.show '/people/:id', :controller => 'people', :action => 'show'
  622. map.index '/people', :controller => 'people', :action => 'index'
  623. map.multi '/people/go/:foo/:bar/joe/:id', :controller => 'people', :action => 'multi'
  624. map.users '/admin/users', :controller => 'admin/users', :action => 'index'
  625. end
  626. MockController.build(set.url_helpers).new
  627. end
  628. def test_named_route_hash_access_method
  629. controller = setup_named_route_test
  630. assert_equal(
  631. { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => false },
  632. controller.send(:hash_for_show_url, :id => 5))
  633. assert_equal(
  634. { :controller => 'people', :action => 'index', :use_route => :index, :only_path => false },
  635. controller.send(:hash_for_index_url))
  636. assert_equal(
  637. { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => true },
  638. controller.send(:hash_for_show_path, :id => 5)
  639. )
  640. end
  641. def test_named_route_url_method
  642. controller = setup_named_route_test
  643. assert_equal "http://test.host/people/5", controller.send(:show_url, :id => 5)
  644. assert_equal "/people/5", controller.send(:show_path, :id => 5)
  645. assert_equal "http://test.host/people", controller.send(:index_url)
  646. assert_equal "/people", controller.send(:index_path)
  647. assert_equal "http://test.host/admin/users", controller.send(:users_url)
  648. assert_equal '/admin/users', controller.send(:users_path)
  649. assert_equal '/admin/users', set.generate(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'})
  650. end
  651. def test_named_route_url_method_with_anchor
  652. controller = setup_named_route_test
  653. assert_equal "http://test.host/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location')
  654. assert_equal "/people/5#location", controller.send(:show_path, :id => 5, :anchor => 'location')
  655. assert_equal "http://test.host/people#location", controller.send(:index_url, :anchor => 'location')
  656. assert_equal "/people#location", controller.send(:index_path, :anchor => 'location')
  657. assert_equal "http://test.host/admin/users#location", controller.send(:users_url, :anchor => 'location')
  658. assert_equal '/admin/users#location', controller.send(:users_path, :anchor => 'location')
  659. assert_equal "http://test.host/people/go/7/hello/joe/5#location",
  660. controller.send(:multi_url, 7, "hello", 5, :anchor => 'location')
  661. assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar#location",
  662. controller.send(:multi_url, 7, "hello", 5, :baz => "bar", :anchor => 'location')
  663. assert_equal "http://test.host/people?baz=bar#location",
  664. controller.send(:index_url, :baz => "bar", :anchor => 'location')
  665. end
  666. def test_named_route_url_method_with_port
  667. controller = setup_named_route_test
  668. assert_equal "http://test.host:8080/people/5", controller.send(:show_url, 5, :port=>8080)
  669. end
  670. def test_named_route_url_method_with_host
  671. controller = setup_named_route_test
  672. assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, :host=>"some.example.com")
  673. end
  674. def test_named_route_url_method_with_protocol
  675. controller = setup_named_route_test
  676. assert_equal "https://test.host/people/5", controller.send(:show_url, 5, :protocol => "https")
  677. end
  678. def test_named_route_url_method_with_ordered_parameters
  679. controller = setup_named_route_test
  680. assert_equal "http://test.host/people/go/7/hello/joe/5",
  681. controller.send(:multi_url, 7, "hello", 5)
  682. end
  683. def test_named_route_url_method_with_ordered_parameters_and_hash
  684. controller = setup_named_route_test
  685. assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar",
  686. controller.send(:multi_url, 7, "hello", 5, :baz => "bar")
  687. end
  688. def test_named_route_url_method_with_ordered_parameters_and_empty_hash
  689. controller = setup_named_route_test
  690. assert_equal "http://test.host/people/go/7/hello/joe/5",
  691. controller.send(:multi_url, 7, "hello", 5, {})
  692. end
  693. def test_named_route_url_method_with_no_positional_arguments
  694. controller = setup_named_route_test
  695. assert_equal "http://test.host/people?baz=bar",
  696. controller.send(:index_url, :baz => "bar")
  697. end
  698. def test_draw_default_route
  699. set.draw do |map|
  700. map.connect '/:controller/:action/:id'
  701. end
  702. assert_equal 1, set.routes.size
  703. assert_equal '/users/show/10', set.generate(:controller => 'users', :action => 'show', :id => 10)
  704. assert_equal '/users/index/10', set.generate(:controller => 'users', :id => 10)
  705. assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10'))
  706. assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10/'))
  707. end
  708. def test_draw_default_route_with_default_controller
  709. set.draw do |map|
  710. map.connect '/:controller/:action/:id', :controller => 'users'
  711. end
  712. assert_equal({:controller => 'users', :action => 'index'}, set.recognize_path('/'))
  713. end
  714. def test_route_with_parameter_shell
  715. set.draw do |map|
  716. map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/
  717. map.connect '/:controller/:action/:id'
  718. end
  719. assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages'))
  720. assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages/index'))
  721. assert_equal({:controller => 'pages', :action => 'list'}, set.recognize_path('/pages/list'))
  722. assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/pages/show/10'))
  723. assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
  724. end
  725. def test_route_requirements_with_anchor_chars_are_invalid
  726. assert_raise ArgumentError do
  727. set.draw do |map|
  728. map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /^\d+/
  729. end
  730. end
  731. assert_raise ArgumentError do
  732. set.draw do |map|
  733. map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\A\d+/
  734. end
  735. end
  736. assert_raise ArgumentError do
  737. set.draw do |map|
  738. map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+$/
  739. end
  740. end
  741. assert_raise ArgumentError do
  742. set.draw do |map|
  743. map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\Z/
  744. end
  745. end
  746. assert_raise ArgumentError do
  747. set.draw do |map|
  748. map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\z/
  749. end
  750. end
  751. end
  752. def test_route_requirements_with_invalid_http_method_is_invalid
  753. assert_raise ArgumentError do
  754. set.draw do |map|
  755. map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :invalid}
  756. end
  757. end
  758. end
  759. def test_route_requirements_with_options_method_condition_is_valid
  760. assert_nothing_raised do
  761. set.draw do |map|
  762. map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :options}
  763. end
  764. end
  765. end
  766. def test_route_requirements_with_head_method_condition_is_invalid
  767. assert_raise ArgumentError do
  768. set.draw do |map|
  769. map.connect 'valid/route', :controller => 'pages', :action => 'show', :conditions => {:method => :head}
  770. end
  771. end
  772. end
  773. def test_recognize_with_encoded_id_and_regex
  774. set.draw do |map|
  775. map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /[a-zA-Z0-9\+]+/
  776. end
  777. assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10'))
  778. assert_equal({:controller => 'pages', :action => 'show', :id => 'hello+world'}, set.recognize_path('/page/hello+world'))
  779. end
  780. def test_recognize_with_conditions
  781. set.draw do |map|
  782. map.with_options(:controller => "people") do |people|
  783. people.people "/people", :action => "index", :conditions => { :method => :get }
  784. people.connect "/people", :action => "create", :conditions => { :method => :post }
  785. people.person "/people/:id", :action => "show", :conditions => { :method => :get }
  786. people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
  787. people.connect "/people/:id", :action => "destroy", :conditions => { :method => :delete }
  788. end
  789. end
  790. params = set.recognize_path("/people", :method => :get)
  791. assert_equal("index", params[:action])
  792. params = set.recognize_path("/people", :method => :post)
  793. assert_equal("create", params[:action])
  794. params = set.recognize_path("/people", :method => :put)
  795. assert_equal("update", params[:action])
  796. assert_raise(ActionController::UnknownHttpMethod) {
  797. set.recognize_path("/people", :method => :bacon)
  798. }
  799. params = set.recognize_path("/people/5", :method => :get)
  800. assert_equal("show", params[:action])
  801. assert_equal("5", params[:id])
  802. params = set.recognize_path("/people/5", :method => :put)
  803. assert_equal("update", params[:action])
  804. assert_equal("5", params[:id])
  805. params = set.recognize_path("/people/5", :method => :delete)
  806. assert_equal("destroy", params[:action])
  807. assert_equal("5", params[:id])
  808. assert_raise(ActionController::RoutingError) {
  809. set.recognize_path("/people/5", :method => :post)
  810. }
  811. end
  812. def test_recognize_with_alias_in_conditions
  813. set.draw do |map|
  814. map.people "/people", :controller => 'people', :action => "index",
  815. :conditions => { :method => :get }
  816. map.root :people
  817. end
  818. params = set.recognize_path("/people", :method => :get)
  819. assert_equal("people", params[:controller])
  820. assert_equal("index", params[:action])
  821. params = set.recognize_path("/", :method => :get)
  822. assert_equal("people", params[:controller])
  823. assert_equal("index", params[:action])
  824. end
  825. def test_typo_recognition
  826. set.draw do |map|
  827. map.connect 'articles/:year/:month/:day/:title',
  828. :controller => 'articles', :action => 'permalink',
  829. :year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/
  830. end
  831. params = set.recognize_path("/articles/2005/11/05/a-very-interesting-article", :method => :get)
  832. assert_equal("permalink", params[:action])
  833. assert_equal("2005", params[:year])
  834. assert_equal("11", params[:month])
  835. assert_equal("05", params[:day])
  836. assert_equal("a-very-interesting-article", params[:title])
  837. end
  838. def test_routing_traversal_does_not_load_extra_classes
  839. assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
  840. set.draw do |map|
  841. map.connect '/profile', :controller => 'profile'
  842. end
  843. params = set.recognize_path("/profile") rescue nil
  844. assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded"
  845. end
  846. def test_recognize_with_conditions_and_format
  847. set.draw do |map|
  848. map.with_options(:controller => "people") do |people|
  849. people.person "/people/:id", :action => "show", :conditions => { :method => :get }
  850. people.connect "/people/:id", :action => "update", :conditions => { :method => :put }
  851. people.connect "/people/:id.:_format", :action => "show", :conditions => { :method => :get }
  852. end
  853. end
  854. params = set.recognize_path("/people/5", :method => :get)
  855. assert_equal("show", params[:action])
  856. assert_equal("5", params[:id])
  857. params = set.recognize_path("/people/5", :method => :put)
  858. assert_equal("update", params[:action])
  859. params = set.recognize_path("/people/5.png", :method => :get)
  860. assert_equal("show", params[:action])
  861. assert_equal("5", params[:id])
  862. assert_equal("png", params[:_format])
  863. end
  864. def test_generate_with_default_action
  865. set.draw do |map|
  866. map.connect "/people", :controller => "people"
  867. map.connect "/people/list", :controller => "people", :action => "list"
  868. end
  869. url = set.generate(:controller => "people", :action => "list")
  870. assert_equal "/people/list", url
  871. end
  872. def test_root_map
  873. set.draw { |map| map.root :controller => "people" }
  874. params = set.recognize_path("", :method => :get)
  875. assert_equal("people", params[:controller])
  876. assert_equal("index", params[:action])
  877. end
  878. def test_namespace
  879. set.draw do |map|
  880. map.namespace 'api' do |api|
  881. api.route 'inventory', :controller => "products", :action => 'inventory'
  882. end
  883. end
  884. params = set.recognize_path("/api/inventory", :method => :get)
  885. assert_equal("api/products", params[:controller])
  886. assert_equal("inventory", params[:action])
  887. end
  888. def test_namespaced_root_map
  889. set.draw do |map|
  890. map.namespace 'api' do |api|
  891. api.root :controller => "products"
  892. end
  893. end
  894. params = set.recognize_path("/api", :method => :get)
  895. assert_equal("api/products", params[:controller])
  896. assert_equal("index", params[:action])
  897. end
  898. def test_namespace_with_path_prefix
  899. set.draw do |map|
  900. map.namespace 'api', :path_prefix => 'prefix' do |api|
  901. api.route 'inventory', :controller => "products", :action => 'inventory'
  902. end
  903. end
  904. params = set.recognize_path("/prefix/inventory", :method => :get)
  905. assert_equal("api/products", params[:controller])
  906. assert_equal("inventory", params[:action])
  907. end
  908. def test_namespace_with_blank_path_prefix
  909. set.draw do |map|
  910. map.namespace 'api', :path_prefix => '' do |api|
  911. api.route 'inventory', :controller => "products", :action => 'inventory'
  912. end
  913. end
  914. params = set.recognize_path("/inventory", :method => :get)
  915. assert_equal("api/products", params[:controller])
  916. assert_equal("inventory", params[:action])
  917. end
  918. def test_generate_changes_controller_module
  919. set.draw { |map| map.connect ':controller/:action/:id' }
  920. current = { :controller => "bling/bloop", :action => "bap", :id => 9 }
  921. url = set.generate({:controller => "foo/bar", :action => "baz", :id => 7}, current)
  922. assert_equal "/foo/bar/baz/7", url
  923. end
  924. # def test_id_is_not_impossibly_sticky
  925. # set.draw do |map|
  926. # map.connect 'foo/:number', :controller => "people", :action => "index"
  927. # map.connect ':controller/:action/:id'
  928. # end
  929. #
  930. # url = set.generate({:controller => "people", :action => "index", :number => 3},
  931. # {:controller => "people", :action => "index", :id => "21"})
  932. # assert_equal "/foo/3", url
  933. # end
  934. def test_id_is_sticky_when_it_ought_to_be
  935. set.draw do |map|
  936. map.connect ':controller/:id/:action'
  937. end
  938. url = set.generate({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"})
  939. assert_equal "/people/7/destroy", url
  940. end
  941. def test_use_static_path_when_possible
  942. set.draw do |map|
  943. map.connect 'about', :controller => "welcome", :action => "about"
  944. map.connect ':controller/:action/:id'
  945. end
  946. url = set.generate({:controller => "welcome", :action => "about"},
  947. {:controller => "welcome", :action => "get", :id => "7"})
  948. assert_equal "/about", url
  949. end
  950. def test_generate
  951. set.draw { |map| map.connect ':controller/:action/:id' }
  952. args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
  953. assert_equal "/foo/bar/7?x=y", set.generate(args)
  954. assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args)
  955. assert_equal [:x], set.extra_keys(args)
  956. end
  957. def test_generate_with_path_prefix
  958. set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => 'my' }
  959. args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
  960. assert_equal "/my/foo/bar/7?x=y", set.generate(args)
  961. end
  962. def test_generate_with_blank_path_prefix
  963. set.draw { |map| map.connect ':controller/:action/:id', :path_prefix => '' }
  964. args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" }
  965. assert_equal "/foo/bar/7?x=y", set.generate(args)
  966. end
  967. def test_named_routes_are_never_relative_to_modules
  968. set.draw do |map|
  969. map.connect "/connection/manage/:action", :controller => 'connection/manage'
  970. map.connect "/connection/connection", :controller => "connection/connection"
  971. map.family_connection "/connection", :controller => "connection"
  972. end
  973. url = set.generate({:controller => "connection"}, {:controller => 'connection/manage'})
  974. assert_equal "/connection/connection", url
  975. url = set.generate({:use_route => :family_connection, :controller => "connection"}, {:controller => 'connection/manage'})
  976. assert_equal "/connection", url
  977. end
  978. def test_action_left_off_when_id_is_recalled
  979. set.draw do |map|
  980. map.connect ':controller/:action/:id'
  981. end
  982. assert_equal '/books', set.generate(
  983. {:controller => 'books', :action => 'index'},
  984. {:controller => 'books', :action => 'show', :id => '10'}
  985. )
  986. end
  987. def test_query_params_will_be_shown_when_recalled
  988. set.draw do |map|
  989. map.connect 'show_weblog/:parameter', :controller => 'weblog', :action => 'show'
  990. map.connect ':controller/:action/:id'
  991. end
  992. assert_equal '/weblog/edit?parameter=1', set.generate(
  993. {:action => 'edit', :parameter => 1},
  994. {:controller => 'weblog', :action => 'show', :parameter => 1}
  995. )
  996. end
  997. def test_format_is_not_inherit
  998. set.draw do |map|
  999. map.connect '/posts.:format', :controller => 'posts'
  1000. end
  1001. assert_equal '/posts', set.generate(
  1002. {:controller => 'posts'},
  1003. {:controller => 'posts', :action => 'index', :format => 'xml'}
  1004. )
  1005. assert_equal '/posts.xml', set.generate(
  1006. {:controller => 'posts', :format => 'xml'},
  1007. {:controller => 'posts', :action => 'index', :format => 'xml'}
  1008. )
  1009. end
  1010. def test_expiry_determination_should_consider_values_with_to_param
  1011. set.draw { |map| map.connect 'projects/:project_id/:controller/:action' }
  1012. assert_equal '/projects/1/weblog/show', set.generate(
  1013. {:action => 'show', :project_id => 1},
  1014. {:controller => 'weblog', :action => 'show', :project_id => '1'})
  1015. end
  1016. def test_named_route_in_nested_resource
  1017. set.draw do |map|
  1018. map.resources :projects do |project|
  1019. project.milestones 'milestones', :controller => 'milestones', :action => 'index'
  1020. end
  1021. end
  1022. params = set.recognize_path("/projects/1/milestones", :method => :get)
  1023. assert_equal("milestones", params[:controller])
  1024. assert_equal("index", params[:action])
  1025. end
  1026. def test_setting_root_in_namespace_using_symbol
  1027. assert_nothing_raised do
  1028. set.draw do |map|
  1029. map.namespace :admin do |admin|
  1030. admin.root :controller => 'home'
  1031. end
  1032. end
  1033. end
  1034. end
  1035. def test_setting_root_in_namespace_using_string
  1036. assert_nothing_raised do
  1037. set.draw do |map|
  1038. map.namespace 'admin' do |admin|
  1039. admin.root :controller => 'home'
  1040. end
  1041. end
  1042. end
  1043. end
  1044. def test_route_requirements_with_unsupported_regexp_options_must_error
  1045. assert_raise ArgumentError do
  1046. set.draw do |map|
  1047. map.connect 'page/:name', :controller => 'pages',
  1048. :action => 'show',
  1049. :requirements => {:name => /(david|jamis)/m}
  1050. end
  1051. end
  1052. end
  1053. def test_route_requirements_with_supported_options_must_not_error
  1054. assert_nothing_raised do
  1055. set.draw do |map|
  1056. map.connect 'page/:name', :controller => 'pages',
  1057. :action => 'show',
  1058. :requirements => {:name => /(david|jamis)/i}
  1059. end
  1060. end
  1061. assert_nothing_raised do
  1062. set.draw do |map|
  1063. map.connect 'page/:name', :controller => 'pages',
  1064. :action => 'show',
  1065. :requirements => {:name => / # Desperately overcommented regexp
  1066. ( #Either
  1067. david #The Creator
  1068. | #Or
  1069. jamis #The Deployer
  1070. )/x}
  1071. end
  1072. end
  1073. end
  1074. def test_route_requirement_recognize_with_ignore_case
  1075. set.draw do |map|
  1076. map.connect 'page/:name', :controller => 'pages',
  1077. :action => 'show',
  1078. :requirements => {:name => /(david|jamis)/i}
  1079. end
  1080. assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
  1081. assert_raise ActionController::RoutingError do
  1082. set.recognize_path('/page/davidjamis')
  1083. end
  1084. assert_equal({:controller => 'pages', :action => 'show', :name => 'DAVID'}, set.recognize_path('/page/DAVID'))
  1085. end
  1086. def test_route_requirement_generate_with_ignore_case
  1087. set.draw do |map|
  1088. map.connect 'page/:name', :controller => 'pages',
  1089. :action => 'show',
  1090. :requirements => {:name => /(david|jamis)/i}
  1091. end
  1092. url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'})
  1093. assert_equal "/page/david", url
  1094. assert_raise ActionController::RoutingError do
  1095. url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'})
  1096. end
  1097. url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'})
  1098. assert_equal "/page/JAMIS", url
  1099. end
  1100. def test_route_requirement_recognize_with_extended_syntax
  1101. set.draw do |map|
  1102. map.connect 'page/:name', :controller => 'pages',
  1103. :action => 'show',
  1104. :requirements => {:name => / # Desperately overcommented regexp
  1105. ( #Either
  1106. david #The Creator
  1107. | #Or
  1108. jamis #The Deployer
  1109. )/x}
  1110. end
  1111. assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis'))
  1112. assert_equal({:controller => 'pages', :action => 'show', :name => 'david'}, set.recognize_path('/page/david'))
  1113. assert_raise ActionController::RoutingError do
  1114. set.recognize_path('/page/david #The Creator')
  1115. end
  1116. assert_raise ActionController::RoutingError do
  1117. set.recognize_path('/page/David')
  1118. end
  1119. end
  1120. def test_route_requirement_generate_with_extended_syntax
  1121. set.draw do |map|
  1122. map.connect 'page/:name', :controller => 'pages',
  1123. :action => 'show',
  1124. :requirements => {:name => / # Desperately overcommented regexp
  1125. ( #Either
  1126. david #The

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