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
< Test
::Unit::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
))
173 @controller = ActionPackAssertionsController
.new
174 @request, @response = ActionController
::TestRequest.new
, ActionController
::TestResponse.new
178 ActionController
::Routing::Routes.reload
181 # -- assertion-based testing ------------------------------------------------
183 def test_assert_tag_and_url_for
185 assert_tag
:content => "/action_pack_assertions/flash_me"
188 # test the get method, make sure the request really was a get
190 assert_raise(RuntimeError
) { get
:raise_on_get }
192 assert_equal
@response.body
, 'request method: GET'
195 # test the get method, make sure the request really was a get
197 assert_raise(RuntimeError
) { post
:raise_on_post }
199 assert_equal
@response.body
, 'request method: POST'
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
206 # assert_equal @response.body, 'request method: POST'
208 # assert_equal @response.body, 'request method: GET'
210 # assert_equal @response.body, 'request method: POST'
212 # assert_equal @response.body, 'request method: GET'
215 # test the redirection to a named route
216 def test_assert_redirect_to_named_route
217 with_routing
do |set
|
219 map
.route_one
'route_one', :controller => 'action_pack_assertions', :action => 'nothing'
220 map
.connect
':controller/:action/:id'
224 process
:redirect_to_named_route
225 assert_redirected_to
'http://test.host/route_one'
226 assert_redirected_to route_one_url
230 def test_assert_redirect_to_named_route_failure
231 with_routing
do |set
|
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'
237 process
:redirect_to_named_route
238 assert_raise(Test
::Unit::AssertionFailedError) do
239 assert_redirected_to
'http://test.host/route_two'
241 assert_raise(Test
::Unit::AssertionFailedError) do
242 assert_redirected_to
:controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
244 assert_raise(Test
::Unit::AssertionFailedError) do
245 assert_redirected_to route_two_url
250 def test_assert_redirect_to_nested_named_route
251 with_routing
do |set
|
253 map
.admin_inner_module
'admin/inner_module', :controller => 'admin/inner_module', :action => 'index'
254 map
.connect
':controller/:action/:id'
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
263 def test_assert_redirected_to_top_level_named_route_from_nested_controller
264 with_routing
do |set
|
266 map
.top_level
'/action_pack_assertions/:id', :controller => 'action_pack_assertions', :action => 'index'
267 map
.connect
':controller/:action/:id'
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"
276 def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in_both_namespaces
277 with_routing
do |set
|
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'
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')
290 # -- standard request/response object testing --------------------------------
292 # make sure that the template objects exist
293 def test_template_objects_alive
295 assert
!@response.has_template_object
?('hi')
296 assert
@response.has_template_object
?('howdy')
299 # make sure we don't have template objects when we shouldn't
300 def test_template_object_missing
302 assert_nil
@response.template_objects
['howdy']
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
?
312 # check if we have flash objects
315 assert
@response.has_flash
?
316 assert
@response.has_flash_with_contents
?
317 assert
@response.has_flash_object
?('hello')
320 # ensure we don't have flash objects
321 def test_flash_have_nots
323 assert
!@response.has_flash
?
324 assert
!@response.has_flash_with_contents
?
325 assert_nil
@response.flash
['hello']
328 # check if we were rendered by a file-based template?
329 def test_rendered_action
331 assert_nil
@response.rendered_template
334 assert
@response.rendered_template
335 assert
'hello_world', @response.rendered_template
.to_s
338 # check the redirection location
339 def test_redirection_location
340 process
:redirect_internal
341 assert_equal
'http://test.host/nothing', @response.redirect_url
343 process
:redirect_external
344 assert_equal
'http://www.rubyonrails.org', @response.redirect_url
347 def test_no_redirect_url
349 assert_nil
@response.redirect_url
353 # check server errors
354 def test_server_error_response_code
356 assert
@response.server_error
?
359 assert
@response.server_error
?
362 assert
!@response.server_error
?
365 # check a 404 response code
366 def test_missing_response_code
368 assert
@response.missing
?
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/)
381 # check for a redirection
383 process
:redirect_internal
384 assert
@response.redirect
?
386 process
:redirect_external
387 assert
@response.redirect
?
390 assert
!@response.redirect
?
393 # check a successful response code
394 def test_successful_response_code
396 assert
@response.success
?
399 # a basic check to make sure we have a TestResponse object
400 def test_has_response
402 assert_kind_of ActionController
::TestResponse, @response
405 def test_render_based_on_parameters
406 process
:render_based_on_parameters, "name" => "David"
407 assert_equal
"Mr. David", @response.body
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"
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' }
423 def test_redirected_to_url_leading_slash
424 process
:redirect_to_path
425 assert_redirected_to
'/some/path'
428 def test_redirected_to_url_no_leadling_slash
429 process
:redirect_to_path
430 assert_redirected_to
'some/path'
433 def test_redirected_to_url_full_url
434 process
:redirect_to_path
435 assert_redirected_to
'http://test.host/some/path'
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"
443 process
:redirect_to_controller_with_symbol
444 assert_nothing_raised
{
445 assert_redirected_to
:controller => :elsewhere, :action => :flash_me
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'
454 get
:redirect_to_fellow_controller
455 assert_redirected_to
:controller => 'admin/user'
458 def test_assert_valid
459 get
:get_valid_record
460 assert_valid
assigns('record')
463 def test_assert_valid_failing
464 get
:get_invalid_record
467 assert_valid
assigns('record')
469 rescue Test
::Unit::AssertionFailedError => e
473 def test_assert_response_uses_exception_message
474 @controller = AssertResponseWithUnexpectedErrorController
.new
476 assert_response
:success
477 flunk
'Expected non-success response'
478 rescue Test
::Unit::AssertionFailedError => e
479 assert e
.message
.include?('FAIL')
482 def test_assert_response_failure_response_with_no_exception
483 @controller = AssertResponseWithUnexpectedErrorController
.new
485 assert_response
:success
486 flunk
'Expected non-success response'
487 rescue Test
::Unit::AssertionFailedError
489 flunk
"assert_response failed to handle failure response with missing, but optional, exception."
493 class ActionPackHeaderTest
< Test
::Unit::TestCase
495 @controller = ActionPackAssertionsController
.new
496 @request, @response = ActionController
::TestRequest.new
, ActionController
::TestResponse.new
499 def test_rendering_xml_sets_content_type
500 process
:hello_xml_world
501 assert_equal('application/xml; charset=utf-8', @response.headers
['type'])
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'])
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']