PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Languages/Ruby/Tests/Libraries/Rails-3.0.0/actionpack/test/controller/base_test.rb

http://github.com/IronLanguages/main
Ruby | 318 lines | 252 code | 63 blank | 3 comment | 1 complexity | 5a3d1d1c3d9758d7f266f5cff018258b MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. require 'abstract_unit'
  2. require 'logger'
  3. require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
  4. # Provide some controller to run the tests on.
  5. module Submodule
  6. class ContainedEmptyController < ActionController::Base
  7. end
  8. class ContainedNonEmptyController < ActionController::Base
  9. def public_action
  10. render :nothing => true
  11. end
  12. hide_action :hidden_action
  13. def hidden_action
  14. raise "Noooo!"
  15. end
  16. def another_hidden_action
  17. end
  18. hide_action :another_hidden_action
  19. end
  20. class SubclassedController < ContainedNonEmptyController
  21. hide_action :public_action # Hiding it here should not affect the superclass.
  22. end
  23. end
  24. class EmptyController < ActionController::Base
  25. end
  26. class NonEmptyController < ActionController::Base
  27. def public_action
  28. render :nothing => true
  29. end
  30. hide_action :hidden_action
  31. def hidden_action
  32. end
  33. end
  34. class MethodMissingController < ActionController::Base
  35. hide_action :shouldnt_be_called
  36. def shouldnt_be_called
  37. raise "NO WAY!"
  38. end
  39. protected
  40. def method_missing(selector)
  41. render :text => selector.to_s
  42. end
  43. end
  44. class AnotherMethodMissingController < ActionController::Base
  45. cattr_accessor :_exception
  46. rescue_from Exception, :with => :_exception=
  47. protected
  48. def method_missing(*attrs, &block)
  49. super
  50. end
  51. end
  52. class DefaultUrlOptionsController < ActionController::Base
  53. def from_view
  54. render :inline => "<%= #{params[:route]} %>"
  55. end
  56. def default_url_options(options = nil)
  57. { :host => 'www.override.com', :action => 'new', :locale => 'en' }
  58. end
  59. end
  60. class UrlOptionsController < ActionController::Base
  61. def from_view
  62. render :inline => "<%= #{params[:route]} %>"
  63. end
  64. def url_options
  65. super.merge(:host => 'www.override.com', :action => 'new', :locale => 'en')
  66. end
  67. end
  68. class RecordIdentifierController < ActionController::Base
  69. end
  70. class ControllerClassTests < ActiveSupport::TestCase
  71. def test_controller_path
  72. assert_equal 'empty', EmptyController.controller_path
  73. assert_equal EmptyController.controller_path, EmptyController.new.controller_path
  74. assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
  75. assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
  76. end
  77. def test_controller_name
  78. assert_equal 'empty', EmptyController.controller_name
  79. assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
  80. end
  81. def test_filter_parameter_logging
  82. parameters = []
  83. config = mock(:config => mock(:filter_parameters => parameters))
  84. Rails.expects(:application).returns(config)
  85. assert_deprecated do
  86. Class.new(ActionController::Base) do
  87. filter_parameter_logging :password
  88. end
  89. end
  90. assert_equal [:password], parameters
  91. end
  92. def test_record_identifier
  93. assert_respond_to RecordIdentifierController.new, :dom_id
  94. assert_respond_to RecordIdentifierController.new, :dom_class
  95. end
  96. end
  97. class ControllerInstanceTests < Test::Unit::TestCase
  98. def setup
  99. @empty = EmptyController.new
  100. @contained = Submodule::ContainedEmptyController.new
  101. @empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
  102. @non_empty_controllers = [NonEmptyController.new,
  103. Submodule::ContainedNonEmptyController.new]
  104. end
  105. def test_action_methods
  106. @empty_controllers.each do |c|
  107. assert_equal Set.new, c.class.action_methods, "#{c.controller_path} should be empty!"
  108. end
  109. @non_empty_controllers.each do |c|
  110. assert_equal Set.new(%w(public_action)), c.class.action_methods, "#{c.controller_path} should not be empty!"
  111. end
  112. end
  113. def test_temporary_anonymous_controllers
  114. name = 'ExamplesController'
  115. klass = Class.new(ActionController::Base)
  116. Object.const_set(name, klass)
  117. controller = klass.new
  118. assert_equal "examples", controller.controller_path
  119. end
  120. end
  121. class PerformActionTest < ActionController::TestCase
  122. def use_controller(controller_class)
  123. @controller = controller_class.new
  124. # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
  125. # a more accurate simulation of what happens in "real life".
  126. @controller.logger = Logger.new(nil)
  127. @request = ActionController::TestRequest.new
  128. @response = ActionController::TestResponse.new
  129. @request.host = "www.nextangle.com"
  130. rescue_action_in_public!
  131. end
  132. def test_process_should_be_precise
  133. use_controller EmptyController
  134. exception = assert_raise AbstractController::ActionNotFound do
  135. get :non_existent
  136. end
  137. assert_equal exception.message, "The action 'non_existent' could not be found for EmptyController"
  138. end
  139. def test_get_on_priv_should_show_selector
  140. use_controller MethodMissingController
  141. get :shouldnt_be_called
  142. assert_response :success
  143. assert_equal 'shouldnt_be_called', @response.body
  144. end
  145. def test_method_missing_is_not_an_action_name
  146. use_controller MethodMissingController
  147. assert !@controller.__send__(:action_method?, 'method_missing')
  148. get :method_missing
  149. assert_response :success
  150. assert_equal 'method_missing', @response.body
  151. end
  152. def test_method_missing_should_recieve_symbol
  153. use_controller AnotherMethodMissingController
  154. get :some_action
  155. assert_kind_of NameError, @controller._exception
  156. end
  157. def test_get_on_hidden_should_fail
  158. use_controller NonEmptyController
  159. assert_raise(ActionController::UnknownAction) { get :hidden_action }
  160. assert_raise(ActionController::UnknownAction) { get :another_hidden_action }
  161. end
  162. end
  163. class UrlOptionsTest < ActionController::TestCase
  164. tests UrlOptionsController
  165. def setup
  166. super
  167. @request.host = 'www.example.com'
  168. rescue_action_in_public!
  169. end
  170. def test_url_options_override
  171. with_routing do |set|
  172. set.draw do
  173. match 'from_view', :to => 'url_options#from_view', :as => :from_view
  174. match ':controller/:action'
  175. end
  176. get :from_view, :route => "from_view_url"
  177. assert_equal 'http://www.override.com/from_view?locale=en', @response.body
  178. assert_equal 'http://www.override.com/from_view?locale=en', @controller.send(:from_view_url)
  179. assert_equal 'http://www.override.com/default_url_options/new?locale=en', @controller.url_for(:controller => 'default_url_options')
  180. end
  181. end
  182. def test_url_helpers_does_not_become_actions
  183. with_routing do |set|
  184. set.draw do
  185. match "account/overview"
  186. end
  187. @controller.class.send(:include, set.url_helpers)
  188. assert !@controller.class.action_methods.include?("account_overview_path")
  189. end
  190. end
  191. end
  192. class DefaultUrlOptionsTest < ActionController::TestCase
  193. tests DefaultUrlOptionsController
  194. def setup
  195. super
  196. @request.host = 'www.example.com'
  197. rescue_action_in_public!
  198. end
  199. def test_default_url_options_override
  200. with_routing do |set|
  201. set.draw do
  202. match 'from_view', :to => 'default_url_options#from_view', :as => :from_view
  203. match ':controller/:action'
  204. end
  205. get :from_view, :route => "from_view_url"
  206. assert_equal 'http://www.override.com/from_view?locale=en', @response.body
  207. assert_equal 'http://www.override.com/from_view?locale=en', @controller.send(:from_view_url)
  208. assert_equal 'http://www.override.com/default_url_options/new?locale=en', @controller.url_for(:controller => 'default_url_options')
  209. end
  210. end
  211. def test_default_url_options_are_used_in_non_positional_parameters
  212. with_routing do |set|
  213. set.draw do
  214. scope("/:locale") do
  215. resources :descriptions
  216. end
  217. match ':controller/:action'
  218. end
  219. get :from_view, :route => "description_path(1)"
  220. assert_equal '/en/descriptions/1', @response.body
  221. assert_equal '/en/descriptions', @controller.send(:descriptions_path)
  222. assert_equal '/pl/descriptions', @controller.send(:descriptions_path, "pl")
  223. assert_equal '/pl/descriptions', @controller.send(:descriptions_path, :locale => "pl")
  224. assert_equal '/pl/descriptions.xml', @controller.send(:descriptions_path, "pl", "xml")
  225. assert_equal '/en/descriptions.xml', @controller.send(:descriptions_path, :format => "xml")
  226. assert_equal '/en/descriptions/1', @controller.send(:description_path, 1)
  227. assert_equal '/pl/descriptions/1', @controller.send(:description_path, "pl", 1)
  228. assert_equal '/pl/descriptions/1', @controller.send(:description_path, 1, :locale => "pl")
  229. assert_equal '/pl/descriptions/1.xml', @controller.send(:description_path, "pl", 1, "xml")
  230. assert_equal '/en/descriptions/1.xml', @controller.send(:description_path, 1, :format => "xml")
  231. end
  232. end
  233. end
  234. class EmptyUrlOptionsTest < ActionController::TestCase
  235. tests NonEmptyController
  236. def setup
  237. super
  238. @request.host = 'www.example.com'
  239. rescue_action_in_public!
  240. end
  241. def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set
  242. get :public_action
  243. assert_equal "http://www.example.com/non_empty/public_action", @controller.url_for
  244. end
  245. def test_named_routes_with_path_without_doing_a_request_first
  246. @controller = EmptyController.new
  247. @controller.request = @request
  248. with_routing do |set|
  249. set.draw do |map|
  250. resources :things
  251. end
  252. assert_equal '/things', @controller.send(:things_path)
  253. end
  254. end
  255. end