Froze rails gems
[depot.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 < Test::Unit::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 @controller = ActionPackAssertionsController.new
174 @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
175 end
176
177 def teardown
178 ActionController::Routing::Routes.reload
179 end
180
181 # -- assertion-based testing ------------------------------------------------
182
183 def test_assert_tag_and_url_for
184 get :render_url
185 assert_tag :content => "/action_pack_assertions/flash_me"
186 end
187
188 # test the get method, make sure the request really was a get
189 def test_get
190 assert_raise(RuntimeError) { get :raise_on_get }
191 get :raise_on_post
192 assert_equal @response.body, 'request method: GET'
193 end
194
195 # test the get method, make sure the request really was a get
196 def test_post
197 assert_raise(RuntimeError) { post :raise_on_post }
198 post :raise_on_get
199 assert_equal @response.body, 'request method: POST'
200 end
201
202 # the following test fails because the request_method is now cached on the request instance
203 # test the get/post switch within one test action
204 # def test_get_post_switch
205 # post :raise_on_get
206 # assert_equal @response.body, 'request method: POST'
207 # get :raise_on_post
208 # assert_equal @response.body, 'request method: GET'
209 # post :raise_on_get
210 # assert_equal @response.body, 'request method: POST'
211 # get :raise_on_post
212 # assert_equal @response.body, 'request method: GET'
213 # end
214
215 # test the redirection to a named route
216 def test_assert_redirect_to_named_route
217 with_routing do |set|
218 set.draw do |map|
219 map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing'
220 map.connect ':controller/:action/:id'
221 end
222 set.install_helpers
223
224 process :redirect_to_named_route
225 assert_redirected_to 'http://test.host/route_one'
226 assert_redirected_to route_one_url
227 end
228 end
229
230 def test_assert_redirect_to_named_route_failure
231 with_routing do |set|
232 set.draw do |map|
233 map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'one'
234 map.route_two 'route_two', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
235 map.connect ':controller/:action/:id'
236 end
237 process :redirect_to_named_route
238 assert_raise(Test::Unit::AssertionFailedError) do
239 assert_redirected_to 'http://test.host/route_two'
240 end
241 assert_raise(Test::Unit::AssertionFailedError) do
242 assert_redirected_to :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
243 end
244 assert_raise(Test::Unit::AssertionFailedError) do
245 assert_redirected_to route_two_url
246 end
247 end
248 end
249
250 def test_assert_redirect_to_nested_named_route
251 with_routing do |set|
252 set.draw do |map|
253 map.admin_inner_module 'admin/inner_module', :controller => 'admin/inner_module', :action => 'index'
254 map.connect ':controller/:action/:id'
255 end
256 @controller = Admin::InnerModuleController.new
257 process :redirect_to_index
258 # redirection is <{"action"=>"index", "controller"=>"admin/admin/inner_module"}>
259 assert_redirected_to admin_inner_module_path
260 end
261 end
262
263 def test_assert_redirected_to_top_level_named_route_from_nested_controller
264 with_routing do |set|
265 set.draw do |map|
266 map.top_level '/action_pack_assertions/:id', :controller => 'action_pack_assertions', :action => 'index'
267 map.connect ':controller/:action/:id'
268 end
269 @controller = Admin::InnerModuleController.new
270 process :redirect_to_top_level_named_route
271 # assert_redirected_to "http://test.host/action_pack_assertions/foo" would pass because of exact match early return
272 assert_redirected_to "/action_pack_assertions/foo"
273 end
274 end
275
276 def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in_both_namespaces
277 with_routing do |set|
278 set.draw do |map|
279 # this controller exists in the admin namespace as well which is the only difference from previous test
280 map.top_level '/user/:id', :controller => 'user', :action => 'index'
281 map.connect ':controller/:action/:id'
282 end
283 @controller = Admin::InnerModuleController.new
284 process :redirect_to_top_level_named_route
285 # assert_redirected_to top_level_url('foo') would pass because of exact match early return
286 assert_redirected_to top_level_path('foo')
287 end
288 end
289
290 # -- standard request/response object testing --------------------------------
291
292 # make sure that the template objects exist
293 def test_template_objects_alive
294 process :assign_this
295 assert !@response.has_template_object?('hi')
296 assert @response.has_template_object?('howdy')
297 end
298
299 # make sure we don't have template objects when we shouldn't
300 def test_template_object_missing
301 process :nothing
302 assert_nil @response.template_objects['howdy']
303 end
304
305 # check the empty flashing
306 def test_flash_me_naked
307 process :flash_me_naked
308 assert !@response.has_flash?
309 assert !@response.has_flash_with_contents?
310 end
311
312 # check if we have flash objects
313 def test_flash_haves
314 process :flash_me
315 assert @response.has_flash?
316 assert @response.has_flash_with_contents?
317 assert @response.has_flash_object?('hello')
318 end
319
320 # ensure we don't have flash objects
321 def test_flash_have_nots
322 process :nothing
323 assert !@response.has_flash?
324 assert !@response.has_flash_with_contents?
325 assert_nil @response.flash['hello']
326 end
327
328 # check if we were rendered by a file-based template?
329 def test_rendered_action
330 process :nothing
331 assert_nil @response.rendered_template
332
333 process :hello_world
334 assert @response.rendered_template
335 assert 'hello_world', @response.rendered_template.to_s
336 end
337
338 # check the redirection location
339 def test_redirection_location
340 process :redirect_internal
341 assert_equal 'http://test.host/nothing', @response.redirect_url
342
343 process :redirect_external
344 assert_equal 'http://www.rubyonrails.org', @response.redirect_url
345 end
346
347 def test_no_redirect_url
348 process :nothing
349 assert_nil @response.redirect_url
350 end
351
352
353 # check server errors
354 def test_server_error_response_code
355 process :response500
356 assert @response.server_error?
357
358 process :response599
359 assert @response.server_error?
360
361 process :response404
362 assert !@response.server_error?
363 end
364
365 # check a 404 response code
366 def test_missing_response_code
367 process :response404
368 assert @response.missing?
369 end
370
371 # check to see if our redirection matches a pattern
372 def test_redirect_url_match
373 process :redirect_external
374 assert @response.redirect?
375 assert @response.redirect_url_match?("rubyonrails")
376 assert @response.redirect_url_match?(/rubyonrails/)
377 assert !@response.redirect_url_match?("phpoffrails")
378 assert !@response.redirect_url_match?(/perloffrails/)
379 end
380
381 # check for a redirection
382 def test_redirection
383 process :redirect_internal
384 assert @response.redirect?
385
386 process :redirect_external
387 assert @response.redirect?
388
389 process :nothing
390 assert !@response.redirect?
391 end
392
393 # check a successful response code
394 def test_successful_response_code
395 process :nothing
396 assert @response.success?
397 end
398
399 # a basic check to make sure we have a TestResponse object
400 def test_has_response
401 process :nothing
402 assert_kind_of ActionController::TestResponse, @response
403 end
404
405 def test_render_based_on_parameters
406 process :render_based_on_parameters, "name" => "David"
407 assert_equal "Mr. David", @response.body
408 end
409
410
411 def test_assert_redirection_fails_with_incorrect_controller
412 process :redirect_to_controller
413 assert_raise(Test::Unit::AssertionFailedError) do
414 assert_redirected_to :controller => "action_pack_assertions", :action => "flash_me"
415 end
416 end
417
418 def test_assert_redirection_with_extra_controller_option
419 get :redirect_to_action
420 assert_redirected_to :controller => 'action_pack_assertions', :action => "flash_me", :id => 1, :params => { :panda => 'fun' }
421 end
422
423 def test_redirected_to_url_leading_slash
424 process :redirect_to_path
425 assert_redirected_to '/some/path'
426 end
427
428 def test_redirected_to_url_no_leadling_slash
429 process :redirect_to_path
430 assert_redirected_to 'some/path'
431 end
432
433 def test_redirected_to_url_full_url
434 process :redirect_to_path
435 assert_redirected_to 'http://test.host/some/path'
436 end
437
438 def test_assert_redirection_with_symbol
439 process :redirect_to_controller_with_symbol
440 assert_nothing_raised {
441 assert_redirected_to :controller => "elsewhere", :action => "flash_me"
442 }
443 process :redirect_to_controller_with_symbol
444 assert_nothing_raised {
445 assert_redirected_to :controller => :elsewhere, :action => :flash_me
446 }
447 end
448
449 def test_redirected_to_with_nested_controller
450 @controller = Admin::InnerModuleController.new
451 get :redirect_to_absolute_controller
452 assert_redirected_to :controller => '/content'
453
454 get :redirect_to_fellow_controller
455 assert_redirected_to :controller => 'admin/user'
456 end
457
458 def test_assert_valid
459 get :get_valid_record
460 assert_valid assigns('record')
461 end
462
463 def test_assert_valid_failing
464 get :get_invalid_record
465
466 begin
467 assert_valid assigns('record')
468 assert false
469 rescue Test::Unit::AssertionFailedError => e
470 end
471 end
472
473 def test_assert_response_uses_exception_message
474 @controller = AssertResponseWithUnexpectedErrorController.new
475 get :index
476 assert_response :success
477 flunk 'Expected non-success response'
478 rescue Test::Unit::AssertionFailedError => e
479 assert e.message.include?('FAIL')
480 end
481
482 def test_assert_response_failure_response_with_no_exception
483 @controller = AssertResponseWithUnexpectedErrorController.new
484 get :show
485 assert_response :success
486 flunk 'Expected non-success response'
487 rescue Test::Unit::AssertionFailedError
488 rescue
489 flunk "assert_response failed to handle failure response with missing, but optional, exception."
490 end
491 end
492
493 class ActionPackHeaderTest < Test::Unit::TestCase
494 def setup
495 @controller = ActionPackAssertionsController.new
496 @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
497 end
498
499 def test_rendering_xml_sets_content_type
500 process :hello_xml_world
501 assert_equal('application/xml; charset=utf-8', @response.headers['type'])
502 end
503
504 def test_rendering_xml_respects_content_type
505 @response.headers['type'] = 'application/pdf'
506 process :hello_xml_world
507 assert_equal('application/pdf; charset=utf-8', @response.headers['type'])
508 end
509
510 def test_render_text_with_custom_content_type
511 get :render_text_with_custom_content_type
512 assert_equal 'application/rss+xml; charset=utf-8', @response.headers['type']
513 end
514 end