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
15 # a redirect to an internal location
16 def redirect_internal() redirect_to
"/nothing"; end
18 def redirect_to_action() redirect_to
:action => "flash_me", :id => 1, :params => { "panda" => "fun" }; end
20 def redirect_to_controller() redirect_to
:controller => "elsewhere", :action => "flash_me"; end
22 def redirect_to_controller_with_symbol() redirect_to
:controller => :elsewhere, :action => :flash_me; end
24 def redirect_to_path() redirect_to
'/some/path' end
26 def redirect_to_named_route() redirect_to route_one_url
end
28 # a redirect to an external location
29 def redirect_external() redirect_to
"http://www.rubyonrails.org"; end
32 def response404() head
'404 AWOL' end
35 def response500() head
'500 Sorry' end
38 def response599() head
'599 Whoah!' end
40 # putting stuff in the flash
42 flash
['hello'] = 'my name is inigo montoya...'
43 render
:text => "Inconceivable!"
46 # we have a flash, but nothing is in it
49 render
:text => "wow!"
52 # assign some template instance variables
55 render
:inline => "Mr. Henke"
58 def render_based_on_parameters
59 render
:text => "Mr. #{params[:name]}"
63 render
:text => "<div>#{url_for(:action => 'flash_me', :only_path => true)}</div>"
66 def render_text_with_custom_content_type
67 render
:text => "Hello!", :content_type => Mime
::RSS
70 # puts something in the session
72 session
['xmas'] = 'turkey'
73 render
:text => "ho ho ho"
76 # raises exception on get requests
78 raise "get" if request
.get
?
79 render
:text => "request method: #{request.env['REQUEST_METHOD']}"
82 # raises exception on post requests
84 raise "post" if request
.post
?
85 render
:text => "request method: #{request.env['REQUEST_METHOD']}"
89 @record = Class
.new
do
96 def full_messages
; []; end
102 render
:nothing => true
106 def get_invalid_record
107 @record = Class
.new
do
115 def full_messages
; ['...stuff...']; end
120 render
:nothing => true
124 def rescue_action(e
) raise; end
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
136 render
:text => "Boom", :status => 500
140 class UserController
< ActionController
::Base
144 class InnerModuleController
< ActionController
::Base
146 render
:nothing => true
149 def redirect_to_index
150 redirect_to admin_inner_module_path
153 def redirect_to_absolute_controller
154 redirect_to
:controller => '/content'
157 def redirect_to_fellow_controller
158 redirect_to
:controller => 'user'
161 def redirect_to_top_level_named_route
162 redirect_to
top_level_url(:id => "foo")
167 # a test case to exercise the new capabilities TestRequest & TestResponse
168 class ActionPackAssertionsControllerTest
< ActionController
::TestCase
169 # let's get this party started
171 ActionController
::Routing::Routes.reload
172 ActionController
::Routing.use_controllers
!(%w(action_pack_assertions admin
/inner_module user content admin/user
))
176 ActionController
::Routing::Routes.reload
179 # -- assertion-based testing ------------------------------------------------
181 def test_assert_tag_and_url_for
183 assert_tag
:content => "/action_pack_assertions/flash_me"
186 # test the get method, make sure the request really was a get
188 assert_raise(RuntimeError
) { get
:raise_on_get }
190 assert_equal
@response.body
, 'request method: GET'
193 # test the get method, make sure the request really was a get
195 assert_raise(RuntimeError
) { post
:raise_on_post }
197 assert_equal
@response.body
, 'request method: POST'
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
204 # assert_equal @response.body, 'request method: POST'
206 # assert_equal @response.body, 'request method: GET'
208 # assert_equal @response.body, 'request method: POST'
210 # assert_equal @response.body, 'request method: GET'
213 # test the redirection to a named route
214 def test_assert_redirect_to_named_route
215 with_routing
do |set
|
217 map
.route_one
'route_one', :controller => 'action_pack_assertions', :action => 'nothing'
218 map
.connect
':controller/:action/:id'
222 process
:redirect_to_named_route
223 assert_redirected_to
'http://test.host/route_one'
224 assert_redirected_to route_one_url
228 def test_assert_redirect_to_named_route_failure
229 with_routing
do |set
|
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'
235 process
:redirect_to_named_route
236 assert_raise(ActiveSupport
::TestCase::Assertion) do
237 assert_redirected_to
'http://test.host/route_two'
239 assert_raise(ActiveSupport
::TestCase::Assertion) do
240 assert_redirected_to
:controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
242 assert_raise(ActiveSupport
::TestCase::Assertion) do
243 assert_redirected_to route_two_url
248 def test_assert_redirect_to_nested_named_route
249 with_routing
do |set
|
251 map
.admin_inner_module
'admin/inner_module', :controller => 'admin/inner_module', :action => 'index'
252 map
.connect
':controller/:action/:id'
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
261 def test_assert_redirected_to_top_level_named_route_from_nested_controller
262 with_routing
do |set
|
264 map
.top_level
'/action_pack_assertions/:id', :controller => 'action_pack_assertions', :action => 'index'
265 map
.connect
':controller/:action/:id'
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"
274 def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in_both_namespaces
275 with_routing
do |set
|
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'
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')
288 # -- standard request/response object testing --------------------------------
290 # make sure that the template objects exist
291 def test_template_objects_alive
293 assert
!@response.has_template_object
?('hi')
294 assert
@response.has_template_object
?('howdy')
297 # make sure we don't have template objects when we shouldn't
298 def test_template_object_missing
300 assert_nil
@response.template_objects
['howdy']
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
?
310 # check if we have flash objects
313 assert
@response.has_flash
?
314 assert
@response.has_flash_with_contents
?
315 assert
@response.has_flash_object
?('hello')
318 # ensure we don't have flash objects
319 def test_flash_have_nots
321 assert
!@response.has_flash
?
322 assert
!@response.has_flash_with_contents
?
323 assert_nil
@response.flash
['hello']
326 # check if we were rendered by a file-based template?
327 def test_rendered_action
329 assert_nil
@response.rendered
[:template]
332 assert
@response.rendered
[:template]
333 assert
'hello_world', @response.rendered
[:template].to_s
336 # check the redirection location
337 def test_redirection_location
338 process
:redirect_internal
339 assert_equal
'http://test.host/nothing', @response.redirect_url
341 process
:redirect_external
342 assert_equal
'http://www.rubyonrails.org', @response.redirect_url
345 def test_no_redirect_url
347 assert_nil
@response.redirect_url
351 # check server errors
352 def test_server_error_response_code
354 assert
@response.server_error
?
357 assert
@response.server_error
?
360 assert
!@response.server_error
?
363 # check a 404 response code
364 def test_missing_response_code
366 assert
@response.missing
?
369 # check client errors
370 def test_client_error_response_code
372 assert
@response.client_error
?
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/)
385 # check for a redirection
387 process
:redirect_internal
388 assert
@response.redirect
?
390 process
:redirect_external
391 assert
@response.redirect
?
394 assert
!@response.redirect
?
397 # check a successful response code
398 def test_successful_response_code
400 assert
@response.success
?
403 # a basic check to make sure we have a TestResponse object
404 def test_has_response
406 assert_kind_of ActionController
::TestResponse, @response
409 def test_render_based_on_parameters
410 process
:render_based_on_parameters, "name" => "David"
411 assert_equal
"Mr. David", @response.body
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"
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' }
427 def test_redirected_to_url_leading_slash
428 process
:redirect_to_path
429 assert_redirected_to
'/some/path'
432 def test_redirected_to_url_no_leadling_slash
433 process
:redirect_to_path
434 assert_redirected_to
'some/path'
437 def test_redirected_to_url_full_url
438 process
:redirect_to_path
439 assert_redirected_to
'http://test.host/some/path'
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"
447 process
:redirect_to_controller_with_symbol
448 assert_nothing_raised
{
449 assert_redirected_to
:controller => :elsewhere, :action => :flash_me
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'
458 get
:redirect_to_fellow_controller
459 assert_redirected_to
:controller => 'admin/user'
462 def test_assert_valid
463 get
:get_valid_record
464 assert_deprecated
{ assert_valid
assigns('record') }
467 def test_assert_valid_failing
468 get
:get_invalid_record
471 assert_deprecated
{ assert_valid
assigns('record') }
473 rescue ActiveSupport
::TestCase::Assertion => e
477 def test_assert_response_uses_exception_message
478 @controller = AssertResponseWithUnexpectedErrorController
.new
480 assert_response
:success
481 flunk
'Expected non-success response'
482 rescue ActiveSupport
::TestCase::Assertion => e
483 assert e
.message
.include?('FAIL')
486 def test_assert_response_failure_response_with_no_exception
487 @controller = AssertResponseWithUnexpectedErrorController
.new
489 assert_response
:success
490 flunk
'Expected non-success response'
491 rescue ActiveSupport
::TestCase::Assertion
494 flunk
"assert_response failed to handle failure response with missing, but optional, exception."
498 class ActionPackHeaderTest
< ActionController
::TestCase
499 tests ActionPackAssertionsController
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'])
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'])
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']