PageRenderTime 53ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

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

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