PageRenderTime 61ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/Faisal-Hassan/insoshi
Ruby | 219 lines | 172 code | 42 blank | 5 comment | 0 complexity | 6c4113436c8ca4df9da9303f02dc6530 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. require 'abstract_unit'
  2. require 'pp' # require 'pp' early to prevent hidden_methods from not picking up the pretty-print methods until too late
  3. # Provide some controller to run the tests on.
  4. module Submodule
  5. class ContainedEmptyController < ActionController::Base
  6. end
  7. class ContainedNonEmptyController < ActionController::Base
  8. def public_action
  9. render :nothing => true
  10. end
  11. hide_action :hidden_action
  12. def hidden_action
  13. raise "Noooo!"
  14. end
  15. def another_hidden_action
  16. end
  17. hide_action :another_hidden_action
  18. end
  19. class SubclassedController < ContainedNonEmptyController
  20. hide_action :public_action # Hiding it here should not affect the superclass.
  21. end
  22. end
  23. class EmptyController < ActionController::Base
  24. end
  25. class NonEmptyController < ActionController::Base
  26. def public_action
  27. end
  28. hide_action :hidden_action
  29. def hidden_action
  30. end
  31. end
  32. class MethodMissingController < ActionController::Base
  33. hide_action :shouldnt_be_called
  34. def shouldnt_be_called
  35. raise "NO WAY!"
  36. end
  37. protected
  38. def method_missing(selector)
  39. render :text => selector.to_s
  40. end
  41. end
  42. class DefaultUrlOptionsController < ActionController::Base
  43. def default_url_options_action
  44. end
  45. def default_url_options(options = nil)
  46. { :host => 'www.override.com', :action => 'new', :bacon => 'chunky' }
  47. end
  48. end
  49. class ControllerClassTests < Test::Unit::TestCase
  50. def test_controller_path
  51. assert_equal 'empty', EmptyController.controller_path
  52. assert_equal EmptyController.controller_path, EmptyController.new.controller_path
  53. assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
  54. assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
  55. end
  56. def test_controller_name
  57. assert_equal 'empty', EmptyController.controller_name
  58. assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
  59. end
  60. end
  61. class ControllerInstanceTests < Test::Unit::TestCase
  62. def setup
  63. @empty = EmptyController.new
  64. @contained = Submodule::ContainedEmptyController.new
  65. @empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
  66. @non_empty_controllers = [NonEmptyController.new,
  67. Submodule::ContainedNonEmptyController.new]
  68. end
  69. def test_action_methods
  70. @empty_controllers.each do |c|
  71. hide_mocha_methods_from_controller(c)
  72. assert_equal Set.new, c.__send__(:action_methods), "#{c.controller_path} should be empty!"
  73. end
  74. @non_empty_controllers.each do |c|
  75. hide_mocha_methods_from_controller(c)
  76. assert_equal Set.new(%w(public_action)), c.__send__(:action_methods), "#{c.controller_path} should not be empty!"
  77. end
  78. end
  79. protected
  80. # Mocha adds some public instance methods to Object that would be
  81. # considered actions, so explicitly hide_action them.
  82. def hide_mocha_methods_from_controller(controller)
  83. mocha_methods = [
  84. :expects, :mocha, :mocha_inspect, :reset_mocha, :stubba_object,
  85. :stubba_method, :stubs, :verify, :__metaclass__, :__is_a__, :to_matcher,
  86. ]
  87. controller.class.__send__(:hide_action, *mocha_methods)
  88. end
  89. end
  90. class PerformActionTest < Test::Unit::TestCase
  91. class MockLogger
  92. attr_reader :logged
  93. def initialize
  94. @logged = []
  95. end
  96. def method_missing(method, *args)
  97. @logged << args.first
  98. end
  99. end
  100. def use_controller(controller_class)
  101. @controller = controller_class.new
  102. # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
  103. # a more accurate simulation of what happens in "real life".
  104. @controller.logger = Logger.new(nil)
  105. @request = ActionController::TestRequest.new
  106. @response = ActionController::TestResponse.new
  107. @request.host = "www.nextangle.com"
  108. end
  109. def test_get_on_priv_should_show_selector
  110. use_controller MethodMissingController
  111. get :shouldnt_be_called
  112. assert_response :success
  113. assert_equal 'shouldnt_be_called', @response.body
  114. end
  115. def test_method_missing_is_not_an_action_name
  116. use_controller MethodMissingController
  117. assert ! @controller.__send__(:action_methods).include?('method_missing')
  118. get :method_missing
  119. assert_response :success
  120. assert_equal 'method_missing', @response.body
  121. end
  122. def test_get_on_hidden_should_fail
  123. use_controller NonEmptyController
  124. get :hidden_action
  125. assert_response 404
  126. get :another_hidden_action
  127. assert_response 404
  128. end
  129. def test_namespaced_action_should_log_module_name
  130. use_controller Submodule::ContainedNonEmptyController
  131. @controller.logger = MockLogger.new
  132. get :public_action
  133. assert_match /Processing\sSubmodule::ContainedNonEmptyController#public_action/, @controller.logger.logged[1]
  134. end
  135. end
  136. class DefaultUrlOptionsTest < Test::Unit::TestCase
  137. def setup
  138. @controller = DefaultUrlOptionsController.new
  139. @request = ActionController::TestRequest.new
  140. @response = ActionController::TestResponse.new
  141. @request.host = 'www.example.com'
  142. end
  143. def test_default_url_options_are_used_if_set
  144. ActionController::Routing::Routes.draw do |map|
  145. map.default_url_options 'default_url_options', :controller => 'default_url_options'
  146. map.connect ':controller/:action/:id'
  147. end
  148. get :default_url_options_action # Make a dummy request so that the controller is initialized properly.
  149. assert_equal 'http://www.override.com/default_url_options/new?bacon=chunky', @controller.url_for(:controller => 'default_url_options')
  150. assert_equal 'http://www.override.com/default_url_options?bacon=chunky', @controller.send(:default_url_options_url)
  151. ensure
  152. ActionController::Routing::Routes.load!
  153. end
  154. end
  155. class EmptyUrlOptionsTest < Test::Unit::TestCase
  156. def setup
  157. @controller = NonEmptyController.new
  158. @request = ActionController::TestRequest.new
  159. @response = ActionController::TestResponse.new
  160. @request.host = 'www.example.com'
  161. end
  162. def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set
  163. get :public_action
  164. assert_equal "http://www.example.com/non_empty/public_action", @controller.url_for
  165. end
  166. end
  167. class EnsureNamedRoutesWorksTicket22BugTest < Test::Unit::TestCase
  168. def test_named_routes_still_work
  169. ActionController::Routing::Routes.draw do |map|
  170. map.resources :things
  171. end
  172. EmptyController.send :include, ActionController::UrlWriter
  173. assert_equal '/things', EmptyController.new.send(:things_path)
  174. ensure
  175. ActionController::Routing::Routes.load!
  176. end
  177. end