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

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

https://github.com/millbanksystems/epetitions
Ruby | 2028 lines | 1614 code | 391 blank | 23 comment | 6 complexity | 0e5aca4d21c9e9cbd02e379051a2f071 MD5 | raw file
Possible License(s): Apache-2.0

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

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

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