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

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

http://monkeycharger.googlecode.com/
Ruby | 1831 lines | 1467 code | 349 blank | 15 comment | 5 complexity | 572775fda5d687788903dbccc21a5a12 MD5 | raw file

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

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