PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://monkeycharger.googlecode.com/
Ruby | 731 lines | 597 code | 111 blank | 23 comment | 13 complexity | e6164217792efb67a94b8a35817b7cc7 MD5 | raw file
  1. require File.dirname(__FILE__) + '/../abstract_unit'
  2. class ResourcesController < ActionController::Base
  3. def index() render :nothing => true end
  4. alias_method :show, :index
  5. def rescue_action(e) raise e end
  6. end
  7. class ThreadsController < ResourcesController; end
  8. class MessagesController < ResourcesController; end
  9. class CommentsController < ResourcesController; end
  10. class AuthorsController < ResourcesController; end
  11. class LogosController < ResourcesController; end
  12. class AccountsController < ResourcesController; end
  13. class AdminController < ResourcesController; end
  14. module Backoffice
  15. class ProductsController < ResourcesController; end
  16. class TagsController < ResourcesController; end
  17. class ManufacturersController < ResourcesController; end
  18. module Admin
  19. class ProductsController < ResourcesController; end
  20. end
  21. end
  22. class ResourcesTest < Test::Unit::TestCase
  23. def test_should_arrange_actions
  24. resource = ActionController::Resources::Resource.new(:messages,
  25. :collection => { :rss => :get, :reorder => :post, :csv => :post },
  26. :member => { :rss => :get, :atom => :get, :upload => :post, :fix => :post },
  27. :new => { :preview => :get, :draft => :get })
  28. assert_resource_methods [:rss], resource, :collection, :get
  29. assert_resource_methods [:csv, :reorder], resource, :collection, :post
  30. assert_resource_methods [:edit, :rss, :atom], resource, :member, :get
  31. assert_resource_methods [:upload, :fix], resource, :member, :post
  32. assert_resource_methods [:new, :preview, :draft], resource, :new, :get
  33. end
  34. def test_should_resource_controller_name_equal_resource_name_by_default
  35. resource = ActionController::Resources::Resource.new(:messages, {})
  36. assert_equal 'messages', resource.controller
  37. end
  38. def test_should_resource_controller_name_equal_controller_option
  39. resource = ActionController::Resources::Resource.new(:messages, :controller => 'posts')
  40. assert_equal 'posts', resource.controller
  41. end
  42. def test_should_all_singleton_paths_be_the_same
  43. [ :path, :nesting_path_prefix, :member_path ].each do |method|
  44. resource = ActionController::Resources::SingletonResource.new(:messages, :path_prefix => 'admin')
  45. assert_equal 'admin/messages', resource.send(method)
  46. end
  47. end
  48. def test_default_restful_routes
  49. with_restful_routing :messages do
  50. assert_simply_restful_for :messages
  51. end
  52. end
  53. def test_multiple_default_restful_routes
  54. with_restful_routing :messages, :comments do
  55. assert_simply_restful_for :messages
  56. assert_simply_restful_for :comments
  57. end
  58. end
  59. def test_irregular_id_with_no_requirements_should_raise_error
  60. expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
  61. with_restful_routing :messages do
  62. assert_raises(ActionController::RoutingError) do
  63. assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
  64. end
  65. end
  66. end
  67. def test_irregular_id_with_requirements_should_pass
  68. expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
  69. with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do
  70. assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
  71. end
  72. end
  73. def test_with_path_prefix_requirements
  74. expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
  75. with_restful_routing :messages, :path_prefix => '/thread/:thread_id', :requirements => {:thread_id => /[0-9]\.[0-9]\.[0-9]/} do
  76. assert_recognizes(expected_options, :path => 'thread/1.1.1/messages/1', :method => :get)
  77. end
  78. end
  79. def test_with_path_prefix
  80. with_restful_routing :messages, :path_prefix => '/thread/:thread_id' do
  81. assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
  82. end
  83. end
  84. def test_multiple_with_path_prefix
  85. with_restful_routing :messages, :comments, :path_prefix => '/thread/:thread_id' do
  86. assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
  87. assert_simply_restful_for :comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
  88. end
  89. end
  90. def test_with_name_prefix
  91. with_restful_routing :messages, :name_prefix => 'post_' do
  92. assert_simply_restful_for :messages, :name_prefix => 'post_'
  93. end
  94. end
  95. def test_with_collection_actions
  96. actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
  97. with_restful_routing :messages, :collection => actions do
  98. assert_restful_routes_for :messages do |options|
  99. actions.each do |action, method|
  100. assert_recognizes(options.merge(:action => action), :path => "/messages/#{action}", :method => method)
  101. end
  102. end
  103. assert_restful_named_routes_for :messages do |options|
  104. actions.keys.each do |action|
  105. assert_named_route "/messages/#{action}", "#{action}_messages_path", :action => action
  106. end
  107. end
  108. end
  109. end
  110. def test_with_collection_actions_and_name_prefix
  111. actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
  112. with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do
  113. assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
  114. actions.each do |action, method|
  115. assert_recognizes(options.merge(:action => action), :path => "/threads/1/messages/#{action}", :method => method)
  116. end
  117. end
  118. assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
  119. actions.keys.each do |action|
  120. assert_deprecated_named_route /thread_#{action}_messages/, "/threads/1/messages/#{action}", "thread_#{action}_messages_path", :action => action
  121. assert_named_route "/threads/1/messages/#{action}", "#{action}_thread_messages_path", :action => action
  122. end
  123. end
  124. end
  125. end
  126. def test_with_collection_action_and_name_prefix_and_formatted
  127. actions = { 'a' => :get, 'b' => :put, 'c' => :post, 'd' => :delete }
  128. with_restful_routing :messages, :path_prefix => '/threads/:thread_id', :name_prefix => "thread_", :collection => actions do
  129. assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
  130. actions.each do |action, method|
  131. assert_recognizes(options.merge(:action => action, :format => 'xml'), :path => "/threads/1/messages/#{action}.xml", :method => method)
  132. end
  133. end
  134. assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
  135. actions.keys.each do |action|
  136. assert_deprecated_named_route /formatted_thread_#{action}_messages/, "/threads/1/messages/#{action}.xml", "formatted_thread_#{action}_messages_path", :action => action, :format => 'xml'
  137. assert_named_route "/threads/1/messages/#{action}.xml", "formatted_#{action}_thread_messages_path", :action => action, :format => 'xml'
  138. end
  139. end
  140. end
  141. end
  142. def test_with_member_action
  143. [:put, :post].each do |method|
  144. with_restful_routing :messages, :member => { :mark => method } do
  145. mark_options = {:action => 'mark', :id => '1'}
  146. mark_path = "/messages/1/mark"
  147. assert_restful_routes_for :messages do |options|
  148. assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method)
  149. end
  150. assert_restful_named_routes_for :messages do |options|
  151. assert_named_route mark_path, :mark_message_path, mark_options
  152. end
  153. end
  154. end
  155. end
  156. def test_with_two_member_actions_with_same_method
  157. [:put, :post].each do |method|
  158. with_restful_routing :messages, :member => { :mark => method, :unmark => method } do
  159. %w(mark unmark).each do |action|
  160. action_options = {:action => action, :id => '1'}
  161. action_path = "/messages/1/#{action}"
  162. assert_restful_routes_for :messages do |options|
  163. assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
  164. end
  165. assert_restful_named_routes_for :messages do |options|
  166. assert_named_route action_path, "#{action}_message_path".to_sym, action_options
  167. end
  168. end
  169. end
  170. end
  171. end
  172. def test_with_new_action
  173. with_restful_routing :messages, :new => { :preview => :post } do
  174. preview_options = {:action => 'preview'}
  175. preview_path = "/messages/new/preview"
  176. assert_restful_routes_for :messages do |options|
  177. assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
  178. end
  179. assert_restful_named_routes_for :messages do |options|
  180. assert_named_route preview_path, :preview_new_message_path, preview_options
  181. end
  182. end
  183. end
  184. def test_with_new_action_with_name_prefix
  185. with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
  186. preview_options = {:action => 'preview', :thread_id => '1'}
  187. preview_path = "/threads/1/messages/new/preview"
  188. assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
  189. assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
  190. end
  191. assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
  192. assert_deprecated_named_route /thread_preview_new_message/, preview_path, :thread_preview_new_message_path, preview_options
  193. assert_named_route preview_path, :preview_new_thread_message_path, preview_options
  194. end
  195. end
  196. end
  197. def test_with_formatted_new_action_with_name_prefix
  198. with_restful_routing :messages, :new => { :preview => :post }, :path_prefix => '/threads/:thread_id', :name_prefix => 'thread_' do
  199. preview_options = {:action => 'preview', :thread_id => '1', :format => 'xml'}
  200. preview_path = "/threads/1/messages/new/preview.xml"
  201. assert_restful_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
  202. assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
  203. end
  204. assert_restful_named_routes_for :messages, :path_prefix => 'threads/1/', :name_prefix => 'thread_', :options => { :thread_id => '1' } do |options|
  205. assert_deprecated_named_route /formatted_thread_preview_new_message/, preview_path, :formatted_thread_preview_new_message_path, preview_options
  206. assert_named_route preview_path, :formatted_preview_new_thread_message_path, preview_options
  207. end
  208. end
  209. end
  210. def test_override_new_method
  211. with_restful_routing :messages do
  212. assert_restful_routes_for :messages do |options|
  213. assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
  214. assert_raises(ActionController::MethodNotAllowed) do
  215. ActionController::Routing::Routes.recognize_path("/messages/new", :method => :post)
  216. end
  217. end
  218. end
  219. with_restful_routing :messages, :new => { :new => :any } do
  220. assert_restful_routes_for :messages do |options|
  221. assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :post)
  222. assert_recognizes(options.merge(:action => "new"), :path => "/messages/new", :method => :get)
  223. end
  224. end
  225. end
  226. def test_nested_restful_routes
  227. with_routing do |set|
  228. set.draw do |map|
  229. map.resources :threads do |map|
  230. map.resources :messages do |map|
  231. map.resources :comments
  232. end
  233. end
  234. end
  235. assert_simply_restful_for :threads
  236. assert_simply_restful_for :messages,
  237. :name_prefix => 'thread_',
  238. :path_prefix => 'threads/1/',
  239. :options => { :thread_id => '1' }
  240. assert_simply_restful_for :comments,
  241. :name_prefix => 'thread_message_',
  242. :path_prefix => 'threads/1/messages/2/',
  243. :options => { :thread_id => '1', :message_id => '2' }
  244. end
  245. end
  246. def test_nested_restful_routes_with_overwritten_defaults
  247. with_routing do |set|
  248. set.draw do |map|
  249. map.resources :threads do |map|
  250. map.resources :messages, :name_prefix => nil do |map|
  251. map.resources :comments, :name_prefix => nil
  252. end
  253. end
  254. end
  255. assert_simply_restful_for :threads
  256. assert_simply_restful_for :messages,
  257. :path_prefix => 'threads/1/',
  258. :options => { :thread_id => '1' }
  259. assert_simply_restful_for :comments,
  260. :path_prefix => 'threads/1/messages/2/',
  261. :options => { :thread_id => '1', :message_id => '2' }
  262. end
  263. end
  264. # NOTE from dchelimsky: http://dev.rubyonrails.org/ticket/8251 changes some of the
  265. # named routes. In order to play nice, I didn't delete the old ones, which means
  266. # that this test fails because until the old ones are deprecated and/or removed, there
  267. # must be room for two names for the same route.
  268. #
  269. # From what I can tell this shouldn't matter - all the other
  270. # tests in actionpack pass without this one passing. But I could be wrong ;)
  271. #
  272. # def test_restful_routes_dont_generate_duplicates
  273. # with_restful_routing :messages do
  274. # routes = ActionController::Routing::Routes.routes
  275. # routes.each do |route|
  276. # routes.each do |r|
  277. # next if route === r # skip the comparison instance
  278. # assert distinct_routes?(route, r), "Duplicate Route: #{route}"
  279. # end
  280. # end
  281. # end
  282. # end
  283. def test_should_create_singleton_resource_routes
  284. with_singleton_resources :account do
  285. assert_singleton_restful_for :account
  286. end
  287. end
  288. def test_should_create_multiple_singleton_resource_routes
  289. with_singleton_resources :account, :logo do
  290. assert_singleton_restful_for :account
  291. assert_singleton_restful_for :logo
  292. end
  293. end
  294. def test_should_create_nested_singleton_resource_routes
  295. with_routing do |set|
  296. set.draw do |map|
  297. map.resource :admin, :controller => 'admin' do |admin|
  298. admin.resource :account
  299. end
  300. end
  301. assert_singleton_restful_for :admin, :controller => 'admin'
  302. assert_singleton_restful_for :account, :name_prefix => "admin_", :path_prefix => 'admin/'
  303. end
  304. end
  305. def test_resource_has_many_should_become_nested_resources
  306. with_routing do |set|
  307. set.draw do |map|
  308. map.resources :messages, :has_many => [ :comments, :authors ]
  309. end
  310. assert_simply_restful_for :messages
  311. assert_simply_restful_for :comments, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
  312. assert_simply_restful_for :authors, :name_prefix => "message_", :path_prefix => 'messages/1/', :options => { :message_id => '1' }
  313. end
  314. end
  315. def test_resource_has_one_should_become_nested_resources
  316. with_routing do |set|
  317. set.draw do |map|
  318. map.resources :messages, :has_one => :logo
  319. end
  320. assert_simply_restful_for :messages
  321. assert_singleton_restful_for :logo, :name_prefix => 'message_', :path_prefix => 'messages/1/', :options => { :message_id => '1' }
  322. end
  323. end
  324. def test_singleton_resource_with_member_action
  325. [:put, :post].each do |method|
  326. with_singleton_resources :account, :member => { :reset => method } do
  327. reset_options = {:action => 'reset'}
  328. reset_path = "/account/reset"
  329. assert_singleton_routes_for :account do |options|
  330. assert_recognizes(options.merge(reset_options), :path => reset_path, :method => method)
  331. end
  332. assert_singleton_named_routes_for :account do |options|
  333. assert_named_route reset_path, :reset_account_path, reset_options
  334. end
  335. end
  336. end
  337. end
  338. def test_singleton_resource_with_two_member_actions_with_same_method
  339. [:put, :post].each do |method|
  340. with_singleton_resources :account, :member => { :reset => method, :disable => method } do
  341. %w(reset disable).each do |action|
  342. action_options = {:action => action}
  343. action_path = "/account/#{action}"
  344. assert_singleton_routes_for :account do |options|
  345. assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
  346. end
  347. assert_singleton_named_routes_for :account do |options|
  348. assert_named_route action_path, "#{action}_account_path".to_sym, action_options
  349. end
  350. end
  351. end
  352. end
  353. end
  354. def test_should_nest_resources_in_singleton_resource
  355. with_routing do |set|
  356. set.draw do |map|
  357. map.resource :account do |account|
  358. account.resources :messages
  359. end
  360. end
  361. assert_singleton_restful_for :account
  362. assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => 'account/'
  363. end
  364. end
  365. def test_should_nest_resources_in_singleton_resource_with_path_prefix
  366. with_routing do |set|
  367. set.draw do |map|
  368. map.resource(:account, :path_prefix => ':site_id') do |account|
  369. account.resources :messages
  370. end
  371. end
  372. assert_singleton_restful_for :account, :path_prefix => '7/', :options => { :site_id => '7' }
  373. assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => '7/account/', :options => { :site_id => '7' }
  374. end
  375. end
  376. def test_should_nest_singleton_resource_in_resources
  377. with_routing do |set|
  378. set.draw do |map|
  379. map.resources :threads do |thread|
  380. thread.resource :admin, :controller => 'admin'
  381. end
  382. end
  383. assert_simply_restful_for :threads
  384. assert_singleton_restful_for :admin, :controller => 'admin', :name_prefix => 'thread_', :path_prefix => 'threads/5/', :options => { :thread_id => '5' }
  385. end
  386. end
  387. def test_should_not_allow_delete_or_put_on_collection_path
  388. controller_name = :messages
  389. with_restful_routing controller_name do
  390. options = { :controller => controller_name.to_s }
  391. collection_path = "/#{controller_name}"
  392. assert_raises(ActionController::MethodNotAllowed) do
  393. assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put)
  394. end
  395. assert_raises(ActionController::MethodNotAllowed) do
  396. assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete)
  397. end
  398. end
  399. end
  400. def test_resources_in_namespace
  401. with_routing do |set|
  402. set.draw do |map|
  403. map.namespace :backoffice do |backoffice|
  404. backoffice.resources :products
  405. end
  406. end
  407. assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
  408. end
  409. end
  410. def test_resource_has_many_in_namespace
  411. with_routing do |set|
  412. set.draw do |map|
  413. map.namespace :backoffice do |backoffice|
  414. backoffice.resources :products, :has_many => :tags
  415. end
  416. end
  417. assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
  418. assert_simply_restful_for :tags, :controller => "backoffice/tags", :name_prefix => "backoffice_product_", :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
  419. end
  420. end
  421. def test_resource_has_one_in_namespace
  422. with_routing do |set|
  423. set.draw do |map|
  424. map.namespace :backoffice do |backoffice|
  425. backoffice.resources :products, :has_one => :manufacturer
  426. end
  427. end
  428. assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
  429. assert_singleton_restful_for :manufacturer, :controller => "backoffice/manufacturers", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
  430. end
  431. end
  432. def test_resources_in_nested_namespace
  433. with_routing do |set|
  434. set.draw do |map|
  435. map.namespace :backoffice do |backoffice|
  436. backoffice.namespace :admin do |admin|
  437. admin.resources :products
  438. end
  439. end
  440. end
  441. assert_simply_restful_for :products, :controller => "backoffice/admin/products", :name_prefix => 'backoffice_admin_', :path_prefix => 'backoffice/admin/'
  442. end
  443. end
  444. def test_resources_using_namespace
  445. with_routing do |set|
  446. set.draw do |map|
  447. map.resources :products, :namespace => "backoffice/"
  448. end
  449. assert_simply_restful_for :products, :controller => "backoffice/products"
  450. end
  451. end
  452. protected
  453. def with_restful_routing(*args)
  454. with_routing do |set|
  455. set.draw { |map| map.resources(*args) }
  456. yield
  457. end
  458. end
  459. def with_singleton_resources(*args)
  460. with_routing do |set|
  461. set.draw { |map| map.resource(*args) }
  462. yield
  463. end
  464. end
  465. # runs assert_restful_routes_for and assert_restful_named_routes for on the controller_name and options, without passing a block.
  466. def assert_simply_restful_for(controller_name, options = {})
  467. assert_restful_routes_for controller_name, options
  468. assert_restful_named_routes_for controller_name, nil, options
  469. end
  470. def assert_singleton_restful_for(singleton_name, options = {})
  471. assert_singleton_routes_for singleton_name, options
  472. assert_singleton_named_routes_for singleton_name, options
  473. end
  474. def assert_restful_routes_for(controller_name, options = {})
  475. options[:options] ||= {}
  476. options[:options][:controller] = options[:controller] || controller_name.to_s
  477. collection_path = "/#{options[:path_prefix]}#{controller_name}"
  478. member_path = "#{collection_path}/1"
  479. new_path = "#{collection_path}/new"
  480. edit_member_path = "#{member_path}/edit"
  481. formatted_edit_member_path = "#{member_path}/edit.xml"
  482. with_options(options[:options]) do |controller|
  483. controller.assert_routing collection_path, :action => 'index'
  484. controller.assert_routing new_path, :action => 'new'
  485. controller.assert_routing member_path, :action => 'show', :id => '1'
  486. controller.assert_routing edit_member_path, :action => 'edit', :id => '1'
  487. controller.assert_routing "#{collection_path}.xml", :action => 'index', :format => 'xml'
  488. controller.assert_routing "#{new_path}.xml", :action => 'new', :format => 'xml'
  489. controller.assert_routing "#{member_path}.xml", :action => 'show', :id => '1', :format => 'xml'
  490. controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml'
  491. end
  492. assert_recognizes(options[:options].merge(:action => 'index'), :path => collection_path, :method => :get)
  493. assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
  494. assert_recognizes(options[:options].merge(:action => 'create'), :path => collection_path, :method => :post)
  495. assert_recognizes(options[:options].merge(:action => 'show', :id => '1'), :path => member_path, :method => :get)
  496. assert_recognizes(options[:options].merge(:action => 'edit', :id => '1'), :path => edit_member_path, :method => :get)
  497. assert_recognizes(options[:options].merge(:action => 'update', :id => '1'), :path => member_path, :method => :put)
  498. assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1'), :path => member_path, :method => :delete)
  499. assert_recognizes(options[:options].merge(:action => 'index', :format => 'xml'), :path => "#{collection_path}.xml", :method => :get)
  500. assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
  501. assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{collection_path}.xml", :method => :post)
  502. assert_recognizes(options[:options].merge(:action => 'show', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :get)
  503. assert_recognizes(options[:options].merge(:action => 'edit', :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get)
  504. assert_recognizes(options[:options].merge(:action => 'update', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :put)
  505. assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml", :method => :delete)
  506. yield options[:options] if block_given?
  507. end
  508. # test named routes like foo_path and foos_path map to the correct options.
  509. def assert_restful_named_routes_for(controller_name, singular_name = nil, options = {})
  510. if singular_name.is_a?(Hash)
  511. options = singular_name
  512. singular_name = nil
  513. end
  514. singular_name ||= controller_name.to_s.singularize
  515. options[:options] ||= {}
  516. options[:options][:controller] = options[:controller] || controller_name.to_s
  517. @controller = "#{options[:options][:controller].camelize}Controller".constantize.new
  518. @request = ActionController::TestRequest.new
  519. @response = ActionController::TestResponse.new
  520. get :index, options[:options]
  521. options[:options].delete :action
  522. full_prefix = "/#{options[:path_prefix]}#{controller_name}"
  523. name_prefix = options[:name_prefix]
  524. assert_named_route "#{full_prefix}", "#{name_prefix}#{controller_name}_path", options[:options]
  525. assert_named_route "#{full_prefix}.xml", "formatted_#{name_prefix}#{controller_name}_path", options[:options].merge( :format => 'xml')
  526. assert_named_route "#{full_prefix}/1", "#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1')
  527. assert_named_route "#{full_prefix}/1.xml", "formatted_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
  528. assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}new_#{singular_name}/, "#{full_prefix}/new", "#{name_prefix}new_#{singular_name}_path", options[:options]
  529. assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}new_#{singular_name}/, "#{full_prefix}/new.xml", "formatted_#{name_prefix}new_#{singular_name}_path", options[:options].merge( :format => 'xml')
  530. assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}edit_#{singular_name}/, "#{full_prefix}/1/edit", "#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1')
  531. assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}edit_#{singular_name}/, "#{full_prefix}/1/edit.xml", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
  532. assert_named_route "#{full_prefix}/new", "new_#{name_prefix}#{singular_name}_path", options[:options]
  533. assert_named_route "#{full_prefix}/new.xml", "formatted_new_#{name_prefix}#{singular_name}_path", options[:options].merge( :format => 'xml')
  534. assert_named_route "#{full_prefix}/1/edit", "edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1')
  535. assert_named_route "#{full_prefix}/1/edit.xml", "formatted_edit_#{name_prefix}#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml')
  536. yield options[:options] if block_given?
  537. end
  538. def assert_singleton_routes_for(singleton_name, options = {})
  539. options[:options] ||= {}
  540. options[:options][:controller] = options[:controller] || singleton_name.to_s.pluralize
  541. full_path = "/#{options[:path_prefix]}#{singleton_name}"
  542. new_path = "#{full_path}/new"
  543. edit_path = "#{full_path}/edit"
  544. formatted_edit_path = "#{full_path}/edit.xml"
  545. with_options options[:options] do |controller|
  546. controller.assert_routing full_path, :action => 'show'
  547. controller.assert_routing new_path, :action => 'new'
  548. controller.assert_routing edit_path, :action => 'edit'
  549. controller.assert_routing "#{full_path}.xml", :action => 'show', :format => 'xml'
  550. controller.assert_routing "#{new_path}.xml", :action => 'new', :format => 'xml'
  551. controller.assert_routing formatted_edit_path, :action => 'edit', :format => 'xml'
  552. end
  553. assert_recognizes(options[:options].merge(:action => 'show'), :path => full_path, :method => :get)
  554. assert_recognizes(options[:options].merge(:action => 'new'), :path => new_path, :method => :get)
  555. assert_recognizes(options[:options].merge(:action => 'edit'), :path => edit_path, :method => :get)
  556. assert_recognizes(options[:options].merge(:action => 'create'), :path => full_path, :method => :post)
  557. assert_recognizes(options[:options].merge(:action => 'update'), :path => full_path, :method => :put)
  558. assert_recognizes(options[:options].merge(:action => 'destroy'), :path => full_path, :method => :delete)
  559. assert_recognizes(options[:options].merge(:action => 'show', :format => 'xml'), :path => "#{full_path}.xml", :method => :get)
  560. assert_recognizes(options[:options].merge(:action => 'new', :format => 'xml'), :path => "#{new_path}.xml", :method => :get)
  561. assert_recognizes(options[:options].merge(:action => 'edit', :format => 'xml'), :path => formatted_edit_path, :method => :get)
  562. assert_recognizes(options[:options].merge(:action => 'create', :format => 'xml'), :path => "#{full_path}.xml", :method => :post)
  563. assert_recognizes(options[:options].merge(:action => 'update', :format => 'xml'), :path => "#{full_path}.xml", :method => :put)
  564. assert_recognizes(options[:options].merge(:action => 'destroy', :format => 'xml'), :path => "#{full_path}.xml", :method => :delete)
  565. yield options[:options] if block_given?
  566. end
  567. def assert_singleton_named_routes_for(singleton_name, options = {})
  568. (options[:options] ||= {})[:controller] ||= singleton_name.to_s.pluralize
  569. @controller = "#{options[:options][:controller].camelize}Controller".constantize.new
  570. @request = ActionController::TestRequest.new
  571. @response = ActionController::TestResponse.new
  572. get :show, options[:options]
  573. options[:options].delete :action
  574. full_path = "/#{options[:path_prefix]}#{singleton_name}"
  575. name_prefix = options[:name_prefix]
  576. assert_named_route "#{full_path}", "#{name_prefix}#{singleton_name}_path", options[:options]
  577. assert_named_route "#{full_path}.xml", "formatted_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
  578. assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}new_#{singleton_name}/, "#{full_path}/new", "#{name_prefix}new_#{singleton_name}_path", options[:options]
  579. assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}new_#{singleton_name}/, "#{full_path}/new.xml", "formatted_#{name_prefix}new_#{singleton_name}_path", options[:options].merge(:format => 'xml')
  580. assert_potentially_deprecated_named_route name_prefix, /#{name_prefix}edit_#{singleton_name}/, "#{full_path}/edit", "#{name_prefix}edit_#{singleton_name}_path", options[:options]
  581. assert_potentially_deprecated_named_route name_prefix, /formatted_#{name_prefix}edit_#{singleton_name}/, "#{full_path}/edit.xml", "formatted_#{name_prefix}edit_#{singleton_name}_path", options[:options].merge(:format => 'xml')
  582. assert_named_route "#{full_path}/new", "new_#{name_prefix}#{singleton_name}_path", options[:options]
  583. assert_named_route "#{full_path}/new.xml", "formatted_new_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
  584. assert_named_route "#{full_path}/edit", "edit_#{name_prefix}#{singleton_name}_path", options[:options]
  585. assert_named_route "#{full_path}/edit.xml", "formatted_edit_#{name_prefix}#{singleton_name}_path", options[:options].merge(:format => 'xml')
  586. end
  587. def assert_named_route(expected, route, options)
  588. actual = @controller.send(route, options) rescue $!.class.name
  589. assert_equal expected, actual, "Error on route: #{route}(#{options.inspect})"
  590. end
  591. def assert_deprecated_named_route(message, expected, route, options)
  592. assert_deprecated message do
  593. assert_named_route expected, route, options
  594. end
  595. end
  596. # These are only deprecated if they have a name_prefix.
  597. # See http://dev.rubyonrails.org/ticket/8251
  598. def assert_potentially_deprecated_named_route(name_prefix, message, expected, route, options)
  599. if name_prefix
  600. assert_deprecated_named_route(message, expected, route, options)
  601. else
  602. assert_named_route expected, route, options
  603. end
  604. end
  605. def assert_resource_methods(expected, resource, action_method, method)
  606. assert_equal expected.length, resource.send("#{action_method}_methods")[method].size, "#{resource.send("#{action_method}_methods")[method].inspect}"
  607. expected.each do |action|
  608. assert resource.send("#{action_method}_methods")[method].include?(action),
  609. "#{method} not in #{action_method} methods: #{resource.send("#{action_method}_methods")[method].inspect}"
  610. end
  611. end
  612. def distinct_routes? (r1, r2)
  613. if r1.conditions == r2.conditions and r1.requirements == r2.requirements then
  614. if r1.segments.collect(&:to_s) == r2.segments.collect(&:to_s) then
  615. return false
  616. end
  617. end
  618. true
  619. end
  620. end