PageRenderTime 117ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/bricooke/my-biz-expenses
Ruby | 1790 lines | 1438 code | 341 blank | 11 comment | 8 complexity | 998779a50d8986e2da3656b826e3c166 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause

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

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

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