9523189f412d86de15d26c0987d3ccf4fb28b779
[feedcatcher.git] / vendor / rails / actionpack / test / controller / base_test.rb
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
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
13 hide_action :hidden_action
14 def hidden_action
15 raise "Noooo!"
16 end
17
18 def another_hidden_action
19 end
20 hide_action :another_hidden_action
21 end
22 class SubclassedController < ContainedNonEmptyController
23 hide_action :public_action # Hiding it here should not affect the superclass.
24 end
25 end
26 class EmptyController < ActionController::Base
27 end
28 class NonEmptyController < ActionController::Base
29 def public_action
30 end
31
32 hide_action :hidden_action
33 def hidden_action
34 end
35 end
36
37 class MethodMissingController < ActionController::Base
38
39 hide_action :shouldnt_be_called
40 def shouldnt_be_called
41 raise "NO WAY!"
42 end
43
44 protected
45
46 def method_missing(selector)
47 render :text => selector.to_s
48 end
49
50 end
51
52 class DefaultUrlOptionsController < ActionController::Base
53 def default_url_options_action
54 end
55
56 def default_url_options(options = nil)
57 { :host => 'www.override.com', :action => 'new', :bacon => 'chunky' }
58 end
59 end
60
61 class ControllerClassTests < Test::Unit::TestCase
62 def test_controller_path
63 assert_equal 'empty', EmptyController.controller_path
64 assert_equal EmptyController.controller_path, EmptyController.new.controller_path
65 assert_equal 'submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
66 assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
67 end
68 def test_controller_name
69 assert_equal 'empty', EmptyController.controller_name
70 assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
71 end
72 end
73
74 class ControllerInstanceTests < Test::Unit::TestCase
75 def setup
76 @empty = EmptyController.new
77 @contained = Submodule::ContainedEmptyController.new
78 @empty_controllers = [@empty, @contained, Submodule::SubclassedController.new]
79
80 @non_empty_controllers = [NonEmptyController.new,
81 Submodule::ContainedNonEmptyController.new]
82 end
83
84 def test_action_methods
85 @empty_controllers.each do |c|
86 hide_mocha_methods_from_controller(c)
87 assert_equal Set.new, c.__send__(:action_methods), "#{c.controller_path} should be empty!"
88 end
89 @non_empty_controllers.each do |c|
90 hide_mocha_methods_from_controller(c)
91 assert_equal Set.new(%w(public_action)), c.__send__(:action_methods), "#{c.controller_path} should not be empty!"
92 end
93 end
94
95 protected
96 # Mocha adds some public instance methods to Object that would be
97 # considered actions, so explicitly hide_action them.
98 def hide_mocha_methods_from_controller(controller)
99 mocha_methods = [
100 :expects, :mocha, :mocha_inspect, :reset_mocha, :stubba_object,
101 :stubba_method, :stubs, :verify, :__metaclass__, :__is_a__, :to_matcher,
102 ]
103 controller.class.__send__(:hide_action, *mocha_methods)
104 end
105 end
106
107
108 class PerformActionTest < ActionController::TestCase
109 class MockLogger
110 attr_reader :logged
111
112 def initialize
113 @logged = []
114 end
115
116 def method_missing(method, *args)
117 @logged << args.first
118 end
119 end
120
121 def use_controller(controller_class)
122 @controller = controller_class.new
123
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
128 @request = ActionController::TestRequest.new
129 @response = ActionController::TestResponse.new
130
131 @request.host = "www.nextangle.com"
132
133 rescue_action_in_public!
134 end
135
136 def test_get_on_priv_should_show_selector
137 use_controller MethodMissingController
138 get :shouldnt_be_called
139 assert_response :success
140 assert_equal 'shouldnt_be_called', @response.body
141 end
142
143 def test_method_missing_is_not_an_action_name
144 use_controller MethodMissingController
145 assert ! @controller.__send__(:action_methods).include?('method_missing')
146
147 get :method_missing
148 assert_response :success
149 assert_equal 'method_missing', @response.body
150 end
151
152 def test_get_on_hidden_should_fail
153 use_controller NonEmptyController
154 get :hidden_action
155 assert_response 404
156
157 get :another_hidden_action
158 assert_response 404
159 end
160
161 def test_namespaced_action_should_log_module_name
162 use_controller Submodule::ContainedNonEmptyController
163 @controller.logger = MockLogger.new
164 get :public_action
165 assert_match /Processing\sSubmodule::ContainedNonEmptyController#public_action/, @controller.logger.logged[1]
166 end
167 end
168
169 class DefaultUrlOptionsTest < ActionController::TestCase
170 tests DefaultUrlOptionsController
171
172 def setup
173 @request.host = 'www.example.com'
174 rescue_action_in_public!
175 end
176
177 def test_default_url_options_are_used_if_set
178 ActionController::Routing::Routes.draw do |map|
179 map.default_url_options 'default_url_options', :controller => 'default_url_options'
180 map.connect ':controller/:action/:id'
181 end
182
183 get :default_url_options_action # Make a dummy request so that the controller is initialized properly.
184
185 assert_equal 'http://www.override.com/default_url_options/new?bacon=chunky', @controller.url_for(:controller => 'default_url_options')
186 assert_equal 'http://www.override.com/default_url_options?bacon=chunky', @controller.send(:default_url_options_url)
187 ensure
188 ActionController::Routing::Routes.load!
189 end
190 end
191
192 class EmptyUrlOptionsTest < ActionController::TestCase
193 tests NonEmptyController
194
195 def setup
196 @request.host = 'www.example.com'
197 rescue_action_in_public!
198 end
199
200 def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set
201 get :public_action
202 assert_equal "http://www.example.com/non_empty/public_action", @controller.url_for
203 end
204 end
205
206 class EnsureNamedRoutesWorksTicket22BugTest < Test::Unit::TestCase
207 def test_named_routes_still_work
208 ActionController::Routing::Routes.draw do |map|
209 map.resources :things
210 end
211 EmptyController.send :include, ActionController::UrlWriter
212
213 assert_equal '/things', EmptyController.new.send(:things_path)
214 ensure
215 ActionController::Routing::Routes.load!
216 end
217 end