cb7922efd2b5562217423eaa026142fef776bcc0
[feedcatcher.git] / vendor / rails / actionpack / test / controller / action_pack_assertions_test.rb
1 require 'abstract_unit'
2
3 # a controller class to facilitate the tests
4 class ActionPackAssertionsController < ActionController::Base
5
6 # this does absolutely nothing
7 def nothing() head :ok end
8
9 # a standard template
10 def hello_world() render :template => "test/hello_world"; end
11
12 # a standard template
13 def hello_xml_world() render :template => "test/hello_xml_world"; end
14
15 # a redirect to an internal location
16 def redirect_internal() redirect_to "/nothing"; end
17
18 def redirect_to_action() redirect_to :action => "flash_me", :id => 1, :params => { "panda" => "fun" }; end
19
20 def redirect_to_controller() redirect_to :controller => "elsewhere", :action => "flash_me"; end
21
22 def redirect_to_controller_with_symbol() redirect_to :controller => :elsewhere, :action => :flash_me; end
23
24 def redirect_to_path() redirect_to '/some/path' end
25
26 def redirect_to_named_route() redirect_to route_one_url end
27
28 # a redirect to an external location
29 def redirect_external() redirect_to "http://www.rubyonrails.org"; end
30
31 # a 404
32 def response404() head '404 AWOL' end
33
34 # a 500
35 def response500() head '500 Sorry' end
36
37 # a fictional 599
38 def response599() head '599 Whoah!' end
39
40 # putting stuff in the flash
41 def flash_me
42 flash['hello'] = 'my name is inigo montoya...'
43 render :text => "Inconceivable!"
44 end
45
46 # we have a flash, but nothing is in it
47 def flash_me_naked
48 flash.clear
49 render :text => "wow!"
50 end
51
52 # assign some template instance variables
53 def assign_this
54 @howdy = "ho"
55 render :inline => "Mr. Henke"
56 end
57
58 def render_based_on_parameters
59 render :text => "Mr. #{params[:name]}"
60 end
61
62 def render_url
63 render :text => "<div>#{url_for(:action => 'flash_me', :only_path => true)}</div>"
64 end
65
66 def render_text_with_custom_content_type
67 render :text => "Hello!", :content_type => Mime::RSS
68 end
69
70 # puts something in the session
71 def session_stuffing
72 session['xmas'] = 'turkey'
73 render :text => "ho ho ho"
74 end
75
76 # raises exception on get requests
77 def raise_on_get
78 raise "get" if request.get?
79 render :text => "request method: #{request.env['REQUEST_METHOD']}"
80 end
81
82 # raises exception on post requests
83 def raise_on_post
84 raise "post" if request.post?
85 render :text => "request method: #{request.env['REQUEST_METHOD']}"
86 end
87
88 def get_valid_record
89 @record = Class.new do
90 def valid?
91 true
92 end
93
94 def errors
95 Class.new do
96 def full_messages; []; end
97 end.new
98 end
99
100 end.new
101
102 render :nothing => true
103 end
104
105
106 def get_invalid_record
107 @record = Class.new do
108
109 def valid?
110 false
111 end
112
113 def errors
114 Class.new do
115 def full_messages; ['...stuff...']; end
116 end.new
117 end
118 end.new
119
120 render :nothing => true
121 end
122
123 # 911
124 def rescue_action(e) raise; end
125 end
126
127 # Used to test that assert_response includes the exception message
128 # in the failure message when an action raises and assert_response
129 # is expecting something other than an error.
130 class AssertResponseWithUnexpectedErrorController < ActionController::Base
131 def index
132 raise 'FAIL'
133 end
134
135 def show
136 render :text => "Boom", :status => 500
137 end
138 end
139
140 class UserController < ActionController::Base
141 end
142
143 module Admin
144 class InnerModuleController < ActionController::Base
145 def index
146 render :nothing => true
147 end
148
149 def redirect_to_index
150 redirect_to admin_inner_module_path
151 end
152
153 def redirect_to_absolute_controller
154 redirect_to :controller => '/content'
155 end
156
157 def redirect_to_fellow_controller
158 redirect_to :controller => 'user'
159 end
160
161 def redirect_to_top_level_named_route
162 redirect_to top_level_url(:id => "foo")
163 end
164 end
165 end
166
167 # a test case to exercise the new capabilities TestRequest & TestResponse
168 class ActionPackAssertionsControllerTest < ActionController::TestCase
169 # let's get this party started
170 def setup
171 ActionController::Routing::Routes.reload
172 ActionController::Routing.use_controllers!(%w(action_pack_assertions admin/inner_module user content admin/user))
173 end
174
175 def teardown
176 ActionController::Routing::Routes.reload
177 end
178
179 # -- assertion-based testing ------------------------------------------------
180
181 def test_assert_tag_and_url_for
182 get :render_url
183 assert_tag :content => "/action_pack_assertions/flash_me"
184 end
185
186 # test the get method, make sure the request really was a get
187 def test_get
188 assert_raise(RuntimeError) { get :raise_on_get }
189 get :raise_on_post
190 assert_equal @response.body, 'request method: GET'
191 end
192
193 # test the get method, make sure the request really was a get
194 def test_post
195 assert_raise(RuntimeError) { post :raise_on_post }
196 post :raise_on_get
197 assert_equal @response.body, 'request method: POST'
198 end
199
200 # the following test fails because the request_method is now cached on the request instance
201 # test the get/post switch within one test action
202 # def test_get_post_switch
203 # post :raise_on_get
204 # assert_equal @response.body, 'request method: POST'
205 # get :raise_on_post
206 # assert_equal @response.body, 'request method: GET'
207 # post :raise_on_get
208 # assert_equal @response.body, 'request method: POST'
209 # get :raise_on_post
210 # assert_equal @response.body, 'request method: GET'
211 # end
212
213 # test the redirection to a named route
214 def test_assert_redirect_to_named_route
215 with_routing do |set|
216 set.draw do |map|
217 map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing'
218 map.connect ':controller/:action/:id'
219 end
220 set.install_helpers
221
222 process :redirect_to_named_route
223 assert_redirected_to 'http://test.host/route_one'
224 assert_redirected_to route_one_url
225 end
226 end
227
228 def test_assert_redirect_to_named_route_failure
229 with_routing do |set|
230 set.draw do |map|
231 map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'one'
232 map.route_two 'route_two', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
233 map.connect ':controller/:action/:id'
234 end
235 process :redirect_to_named_route
236 assert_raise(ActiveSupport::TestCase::Assertion) do
237 assert_redirected_to 'http://test.host/route_two'
238 end
239 assert_raise(ActiveSupport::TestCase::Assertion) do
240 assert_redirected_to :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
241 end
242 assert_raise(ActiveSupport::TestCase::Assertion) do
243 assert_redirected_to route_two_url
244 end
245 end
246 end
247
248 def test_assert_redirect_to_nested_named_route
249 with_routing do |set|
250 set.draw do |map|
251 map.admin_inner_module 'admin/inner_module', :controller => 'admin/inner_module', :action => 'index'
252 map.connect ':controller/:action/:id'
253 end
254 @controller = Admin::InnerModuleController.new
255 process :redirect_to_index
256 # redirection is <{"action"=>"index", "controller"=>"admin/admin/inner_module"}>
257 assert_redirected_to admin_inner_module_path
258 end
259 end
260
261 def test_assert_redirected_to_top_level_named_route_from_nested_controller
262 with_routing do |set|
263 set.draw do |map|
264 map.top_level '/action_pack_assertions/:id', :controller => 'action_pack_assertions', :action => 'index'
265 map.connect ':controller/:action/:id'
266 end
267 @controller = Admin::InnerModuleController.new
268 process :redirect_to_top_level_named_route
269 # assert_redirected_to "http://test.host/action_pack_assertions/foo" would pass because of exact match early return
270 assert_redirected_to "/action_pack_assertions/foo"
271 end
272 end
273
274 def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in_both_namespaces
275 with_routing do |set|
276 set.draw do |map|
277 # this controller exists in the admin namespace as well which is the only difference from previous test
278 map.top_level '/user/:id', :controller => 'user', :action => 'index'
279 map.connect ':controller/:action/:id'
280 end
281 @controller = Admin::InnerModuleController.new
282 process :redirect_to_top_level_named_route
283 # assert_redirected_to top_level_url('foo') would pass because of exact match early return
284 assert_redirected_to top_level_path('foo')
285 end
286 end
287
288 # -- standard request/response object testing --------------------------------
289
290 # make sure that the template objects exist
291 def test_template_objects_alive
292 process :assign_this
293 assert !@response.has_template_object?('hi')
294 assert @response.has_template_object?('howdy')
295 end
296
297 # make sure we don't have template objects when we shouldn't
298 def test_template_object_missing
299 process :nothing
300 assert_nil @response.template_objects['howdy']
301 end
302
303 # check the empty flashing
304 def test_flash_me_naked
305 process :flash_me_naked
306 assert !@response.has_flash?
307 assert !@response.has_flash_with_contents?
308 end
309
310 # check if we have flash objects
311 def test_flash_haves
312 process :flash_me
313 assert @response.has_flash?
314 assert @response.has_flash_with_contents?
315 assert @response.has_flash_object?('hello')
316 end
317
318 # ensure we don't have flash objects
319 def test_flash_have_nots
320 process :nothing
321 assert !@response.has_flash?
322 assert !@response.has_flash_with_contents?
323 assert_nil @response.flash['hello']
324 end
325
326 # check if we were rendered by a file-based template?
327 def test_rendered_action
328 process :nothing
329 assert_nil @response.rendered[:template]
330
331 process :hello_world
332 assert @response.rendered[:template]
333 assert 'hello_world', @response.rendered[:template].to_s
334 end
335
336 # check the redirection location
337 def test_redirection_location
338 process :redirect_internal
339 assert_equal 'http://test.host/nothing', @response.redirect_url
340
341 process :redirect_external
342 assert_equal 'http://www.rubyonrails.org', @response.redirect_url
343 end
344
345 def test_no_redirect_url
346 process :nothing
347 assert_nil @response.redirect_url
348 end
349
350
351 # check server errors
352 def test_server_error_response_code
353 process :response500
354 assert @response.server_error?
355
356 process :response599
357 assert @response.server_error?
358
359 process :response404
360 assert !@response.server_error?
361 end
362
363 # check a 404 response code
364 def test_missing_response_code
365 process :response404
366 assert @response.missing?
367 end
368
369 # check client errors
370 def test_client_error_response_code
371 process :response404
372 assert @response.client_error?
373 end
374
375 # check to see if our redirection matches a pattern
376 def test_redirect_url_match
377 process :redirect_external
378 assert @response.redirect?
379 assert @response.redirect_url_match?("rubyonrails")
380 assert @response.redirect_url_match?(/rubyonrails/)
381 assert !@response.redirect_url_match?("phpoffrails")
382 assert !@response.redirect_url_match?(/perloffrails/)
383 end
384
385 # check for a redirection
386 def test_redirection
387 process :redirect_internal
388 assert @response.redirect?
389
390 process :redirect_external
391 assert @response.redirect?
392
393 process :nothing
394 assert !@response.redirect?
395 end
396
397 # check a successful response code
398 def test_successful_response_code
399 process :nothing
400 assert @response.success?
401 end
402
403 # a basic check to make sure we have a TestResponse object
404 def test_has_response
405 process :nothing
406 assert_kind_of ActionController::TestResponse, @response
407 end
408
409 def test_render_based_on_parameters
410 process :render_based_on_parameters, "name" => "David"
411 assert_equal "Mr. David", @response.body
412 end
413
414
415 def test_assert_redirection_fails_with_incorrect_controller
416 process :redirect_to_controller
417 assert_raise(ActiveSupport::TestCase::Assertion) do
418 assert_redirected_to :controller => "action_pack_assertions", :action => "flash_me"
419 end
420 end
421
422 def test_assert_redirection_with_extra_controller_option
423 get :redirect_to_action
424 assert_redirected_to :controller => 'action_pack_assertions', :action => "flash_me", :id => 1, :params => { :panda => 'fun' }
425 end
426
427 def test_redirected_to_url_leading_slash
428 process :redirect_to_path
429 assert_redirected_to '/some/path'
430 end
431
432 def test_redirected_to_url_no_leadling_slash
433 process :redirect_to_path
434 assert_redirected_to 'some/path'
435 end
436
437 def test_redirected_to_url_full_url
438 process :redirect_to_path
439 assert_redirected_to 'http://test.host/some/path'
440 end
441
442 def test_assert_redirection_with_symbol
443 process :redirect_to_controller_with_symbol
444 assert_nothing_raised {
445 assert_redirected_to :controller => "elsewhere", :action => "flash_me"
446 }
447 process :redirect_to_controller_with_symbol
448 assert_nothing_raised {
449 assert_redirected_to :controller => :elsewhere, :action => :flash_me
450 }
451 end
452
453 def test_redirected_to_with_nested_controller
454 @controller = Admin::InnerModuleController.new
455 get :redirect_to_absolute_controller
456 assert_redirected_to :controller => '/content'
457
458 get :redirect_to_fellow_controller
459 assert_redirected_to :controller => 'admin/user'
460 end
461
462 def test_assert_valid
463 get :get_valid_record
464 assert_deprecated { assert_valid assigns('record') }
465 end
466
467 def test_assert_valid_failing
468 get :get_invalid_record
469
470 begin
471 assert_deprecated { assert_valid assigns('record') }
472 assert false
473 rescue ActiveSupport::TestCase::Assertion => e
474 end
475 end
476
477 def test_assert_response_uses_exception_message
478 @controller = AssertResponseWithUnexpectedErrorController.new
479 get :index
480 assert_response :success
481 flunk 'Expected non-success response'
482 rescue ActiveSupport::TestCase::Assertion => e
483 assert e.message.include?('FAIL')
484 end
485
486 def test_assert_response_failure_response_with_no_exception
487 @controller = AssertResponseWithUnexpectedErrorController.new
488 get :show
489 assert_response :success
490 flunk 'Expected non-success response'
491 rescue ActiveSupport::TestCase::Assertion
492 # success
493 rescue
494 flunk "assert_response failed to handle failure response with missing, but optional, exception."
495 end
496 end
497
498 class ActionPackHeaderTest < ActionController::TestCase
499 tests ActionPackAssertionsController
500
501 def test_rendering_xml_sets_content_type
502 process :hello_xml_world
503 assert_equal('application/xml; charset=utf-8', @response.headers['Content-Type'])
504 end
505
506 def test_rendering_xml_respects_content_type
507 @response.headers['type'] = 'application/pdf'
508 process :hello_xml_world
509 assert_equal('application/pdf; charset=utf-8', @response.headers['Content-Type'])
510 end
511
512 def test_render_text_with_custom_content_type
513 get :render_text_with_custom_content_type
514 assert_equal 'application/rss+xml; charset=utf-8', @response.headers['Content-Type']
515 end
516 end