1
require 'abstract_unit'
3
# a controller class to facilitate the tests
4
class ActionPackAssertionsController < ActionController::Base
6
# this does absolutely nothing
7
def nothing() head :ok end
10
def hello_world() render :template => "test/hello_world"; end
13
def hello_xml_world() render :template => "test/hello_xml_world"; end
16
def partial() render :partial => 'test/partial'; end
18
# a redirect to an internal location
19
def redirect_internal() redirect_to "/nothing"; end
21
def redirect_to_action() redirect_to :action => "flash_me", :id => 1, :params => { "panda" => "fun" }; end
23
def redirect_to_controller() redirect_to :controller => "elsewhere", :action => "flash_me"; end
25
def redirect_to_controller_with_symbol() redirect_to :controller => :elsewhere, :action => :flash_me; end
27
def redirect_to_path() redirect_to '/some/path' end
29
def redirect_to_named_route() redirect_to route_one_url end
31
# a redirect to an external location
32
def redirect_external() redirect_to "http://www.rubyonrails.org"; end
35
def response404() head '404 AWOL' end
38
def response500() head '500 Sorry' end
41
def response599() head '599 Whoah!' end
43
# putting stuff in the flash
45
flash['hello'] = 'my name is inigo montoya...'
46
render :text => "Inconceivable!"
49
# we have a flash, but nothing is in it
52
render :text => "wow!"
55
# assign some template instance variables
58
render :inline => "Mr. Henke"
61
def render_based_on_parameters
62
render :text => "Mr. #{params[:name]}"
66
render :text => "<div>#{url_for(:action => 'flash_me', :only_path => true)}</div>"
69
def render_text_with_custom_content_type
70
render :text => "Hello!", :content_type => Mime::RSS
73
# puts something in the session
75
session['xmas'] = 'turkey'
76
render :text => "ho ho ho"
79
# raises exception on get requests
81
raise "get" if request.get?
82
render :text => "request method: #{request.env['REQUEST_METHOD']}"
85
# raises exception on post requests
87
raise "post" if request.post?
88
render :text => "request method: #{request.env['REQUEST_METHOD']}"
92
@record = Class.new do
99
def full_messages; []; end
105
render :nothing => true
109
def get_invalid_record
110
@record = Class.new do
118
def full_messages; ['...stuff...']; end
123
render :nothing => true
127
def rescue_action(e) raise; end
130
# Used to test that assert_response includes the exception message
131
# in the failure message when an action raises and assert_response
132
# is expecting something other than an error.
133
class AssertResponseWithUnexpectedErrorController < ActionController::Base
139
render :text => "Boom", :status => 500
143
class UserController < ActionController::Base
147
class InnerModuleController < ActionController::Base
149
render :nothing => true
152
def redirect_to_index
153
redirect_to admin_inner_module_path
156
def redirect_to_absolute_controller
157
redirect_to :controller => '/content'
160
def redirect_to_fellow_controller
161
redirect_to :controller => 'user'
164
def redirect_to_top_level_named_route
165
redirect_to top_level_url(:id => "foo")
170
# a test case to exercise the new capabilities TestRequest & TestResponse
171
class ActionPackAssertionsControllerTest < ActionController::TestCase
172
# let's get this party started
174
ActionController::Routing::Routes.reload
175
ActionController::Routing.use_controllers!(%w(action_pack_assertions admin/inner_module user content admin/user))
179
ActionController::Routing::Routes.reload
182
# -- assertion-based testing ------------------------------------------------
184
def test_assert_tag_and_url_for
186
assert_tag :content => "/action_pack_assertions/flash_me"
189
# test the get method, make sure the request really was a get
191
assert_raise(RuntimeError) { get :raise_on_get }
193
assert_equal @response.body, 'request method: GET'
196
# test the get method, make sure the request really was a get
198
assert_raise(RuntimeError) { post :raise_on_post }
200
assert_equal @response.body, 'request method: POST'
203
# the following test fails because the request_method is now cached on the request instance
204
# test the get/post switch within one test action
205
# def test_get_post_switch
207
# assert_equal @response.body, 'request method: POST'
209
# assert_equal @response.body, 'request method: GET'
211
# assert_equal @response.body, 'request method: POST'
213
# assert_equal @response.body, 'request method: GET'
216
# test the redirection to a named route
217
def test_assert_redirect_to_named_route
218
with_routing do |set|
220
map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing'
221
map.connect ':controller/:action/:id'
225
process :redirect_to_named_route
226
assert_redirected_to 'http://test.host/route_one'
227
assert_redirected_to route_one_url
231
def test_assert_redirect_to_named_route_failure
232
with_routing do |set|
234
map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'one'
235
map.route_two 'route_two', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
236
map.connect ':controller/:action/:id'
238
process :redirect_to_named_route
239
assert_raise(ActiveSupport::TestCase::Assertion) do
240
assert_redirected_to 'http://test.host/route_two'
242
assert_raise(ActiveSupport::TestCase::Assertion) do
243
assert_redirected_to :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
245
assert_raise(ActiveSupport::TestCase::Assertion) do
246
assert_redirected_to route_two_url
251
def test_assert_redirect_to_nested_named_route
252
with_routing do |set|
254
map.admin_inner_module 'admin/inner_module', :controller => 'admin/inner_module', :action => 'index'
255
map.connect ':controller/:action/:id'
257
@controller = Admin::InnerModuleController.new
258
process :redirect_to_index
259
# redirection is <{"action"=>"index", "controller"=>"admin/admin/inner_module"}>
260
assert_redirected_to admin_inner_module_path
264
def test_assert_redirected_to_top_level_named_route_from_nested_controller
265
with_routing do |set|
267
map.top_level '/action_pack_assertions/:id', :controller => 'action_pack_assertions', :action => 'index'
268
map.connect ':controller/:action/:id'
270
@controller = Admin::InnerModuleController.new
271
process :redirect_to_top_level_named_route
272
# assert_redirected_to "http://test.host/action_pack_assertions/foo" would pass because of exact match early return
273
assert_redirected_to "/action_pack_assertions/foo"
277
def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in_both_namespaces
278
with_routing do |set|
280
# this controller exists in the admin namespace as well which is the only difference from previous test
281
map.top_level '/user/:id', :controller => 'user', :action => 'index'
282
map.connect ':controller/:action/:id'
284
@controller = Admin::InnerModuleController.new
285
process :redirect_to_top_level_named_route
286
# assert_redirected_to top_level_url('foo') would pass because of exact match early return
287
assert_redirected_to top_level_path('foo')
291
# -- standard request/response object testing --------------------------------
293
# make sure that the template objects exist
294
def test_template_objects_alive
296
assert !@response.has_template_object?('hi')
297
assert @response.has_template_object?('howdy')
300
# make sure we don't have template objects when we shouldn't
301
def test_template_object_missing
303
assert_nil @response.template_objects['howdy']
306
# check the empty flashing
307
def test_flash_me_naked
308
process :flash_me_naked
309
assert !@response.has_flash?
310
assert !@response.has_flash_with_contents?
313
# check if we have flash objects
316
assert @response.has_flash?
317
assert @response.has_flash_with_contents?
318
assert @response.has_flash_object?('hello')
321
# ensure we don't have flash objects
322
def test_flash_have_nots
324
assert !@response.has_flash?
325
assert !@response.has_flash_with_contents?
326
assert_nil @response.flash['hello']
329
# check if we were rendered by a file-based template?
330
def test_rendered_action
332
assert_nil @response.rendered[:template]
335
assert @response.rendered[:template]
336
assert 'hello_world', @response.rendered[:template].to_s
339
def test_assert_template_with_partial
341
assert_template :partial => '_partial'
344
def test_assert_template_with_nil
349
def test_assert_template_with_string
351
assert_template 'hello_world'
354
def test_assert_template_with_symbol
356
assert_template :hello_world
359
def test_assert_template_with_bad_argument
360
assert_raise(ArgumentError) { assert_template 1 }
363
# check the redirection location
364
def test_redirection_location
365
process :redirect_internal
366
assert_equal 'http://test.host/nothing', @response.redirect_url
368
process :redirect_external
369
assert_equal 'http://www.rubyonrails.org', @response.redirect_url
372
def test_no_redirect_url
374
assert_nil @response.redirect_url
378
# check server errors
379
def test_server_error_response_code
381
assert @response.server_error?
384
assert @response.server_error?
387
assert !@response.server_error?
390
# check a 404 response code
391
def test_missing_response_code
393
assert @response.missing?
396
# check client errors
397
def test_client_error_response_code
399
assert @response.client_error?
402
# check to see if our redirection matches a pattern
403
def test_redirect_url_match
404
process :redirect_external
405
assert @response.redirect?
406
assert @response.redirect_url_match?("rubyonrails")
407
assert @response.redirect_url_match?(/rubyonrails/)
408
assert !@response.redirect_url_match?("phpoffrails")
409
assert !@response.redirect_url_match?(/perloffrails/)
412
# check for a redirection
414
process :redirect_internal
415
assert @response.redirect?
417
process :redirect_external
418
assert @response.redirect?
421
assert !@response.redirect?
424
# check a successful response code
425
def test_successful_response_code
427
assert @response.success?
430
# a basic check to make sure we have a TestResponse object
431
def test_has_response
433
assert_kind_of ActionController::TestResponse, @response
436
def test_render_based_on_parameters
437
process :render_based_on_parameters, "name" => "David"
438
assert_equal "Mr. David", @response.body
442
def test_assert_redirection_fails_with_incorrect_controller
443
process :redirect_to_controller
444
assert_raise(ActiveSupport::TestCase::Assertion) do
445
assert_redirected_to :controller => "action_pack_assertions", :action => "flash_me"
449
def test_assert_redirection_with_extra_controller_option
450
get :redirect_to_action
451
assert_redirected_to :controller => 'action_pack_assertions', :action => "flash_me", :id => 1, :params => { :panda => 'fun' }
454
def test_redirected_to_url_leading_slash
455
process :redirect_to_path
456
assert_redirected_to '/some/path'
459
def test_redirected_to_url_no_leadling_slash
460
process :redirect_to_path
461
assert_redirected_to 'some/path'
464
def test_redirected_to_url_full_url
465
process :redirect_to_path
466
assert_redirected_to 'http://test.host/some/path'
469
def test_assert_redirection_with_symbol
470
process :redirect_to_controller_with_symbol
471
assert_nothing_raised {
472
assert_redirected_to :controller => "elsewhere", :action => "flash_me"
474
process :redirect_to_controller_with_symbol
475
assert_nothing_raised {
476
assert_redirected_to :controller => :elsewhere, :action => :flash_me
480
def test_redirected_to_with_nested_controller
481
@controller = Admin::InnerModuleController.new
482
get :redirect_to_absolute_controller
483
assert_redirected_to :controller => '/content'
485
get :redirect_to_fellow_controller
486
assert_redirected_to :controller => 'admin/user'
489
def test_assert_valid
490
get :get_valid_record
491
assert_deprecated { assert_valid assigns('record') }
494
def test_assert_valid_failing
495
get :get_invalid_record
498
assert_deprecated { assert_valid assigns('record') }
500
rescue ActiveSupport::TestCase::Assertion => e
504
def test_assert_response_uses_exception_message
505
@controller = AssertResponseWithUnexpectedErrorController.new
507
assert_response :success
508
flunk 'Expected non-success response'
509
rescue ActiveSupport::TestCase::Assertion => e
510
assert e.message.include?('FAIL')
513
def test_assert_response_failure_response_with_no_exception
514
@controller = AssertResponseWithUnexpectedErrorController.new
516
assert_response :success
517
flunk 'Expected non-success response'
518
rescue ActiveSupport::TestCase::Assertion
521
flunk "assert_response failed to handle failure response with missing, but optional, exception."
525
class ActionPackHeaderTest < ActionController::TestCase
526
tests ActionPackAssertionsController
528
def test_rendering_xml_sets_content_type
529
process :hello_xml_world
530
assert_equal('application/xml; charset=utf-8', @response.headers['Content-Type'])
533
def test_rendering_xml_respects_content_type
534
@response.headers['type'] = 'application/pdf'
535
process :hello_xml_world
536
assert_equal('application/pdf; charset=utf-8', @response.headers['Content-Type'])
539
def test_render_text_with_custom_content_type
540
get :render_text_with_custom_content_type
541
assert_equal 'application/rss+xml; charset=utf-8', @response.headers['Content-Type']