1 require File.dirname(__FILE__) + '/../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
128 class InnerModuleController < ActionController::Base
130 render :nothing => true
133 def redirect_to_index
134 redirect_to admin_inner_module_path
137 def redirect_to_absolute_controller
138 redirect_to :controller => '/content'
141 def redirect_to_fellow_controller
142 redirect_to :controller => 'user'
145 def redirect_to_top_level_named_route
146 redirect_to top_level_url(:id => "foo")
151 # ---------------------------------------------------------------------------
154 # tell the controller where to find its templates but start from parent
155 # directory of test_request_response to simulate the behaviour of a
156 # production environment
157 ActionPackAssertionsController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ]
159 # a test case to exercise the new capabilities TestRequest & TestResponse
160 class ActionPackAssertionsControllerTest < Test::Unit::TestCase
161 # let's get this party started
163 ActionController::Routing::Routes.reload
164 ActionController::Routing.use_controllers!(%w(action_pack_assertions admin/inner_module content admin/user))
165 @controller = ActionPackAssertionsController.new
166 @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
170 ActionController::Routing::Routes.reload
173 # -- assertion-based testing ------------------------------------------------
175 def test_assert_tag_and_url_for
177 assert_tag :content => "/action_pack_assertions/flash_me"
180 # test the get method, make sure the request really was a get
182 assert_raise(RuntimeError) { get :raise_on_get }
184 assert_equal @response.body, 'request method: GET'
187 # test the get method, make sure the request really was a get
189 assert_raise(RuntimeError) { post :raise_on_post }
191 assert_equal @response.body, 'request method: POST'
194 # the following test fails because the request_method is now cached on the request instance
195 # test the get/post switch within one test action
196 # def test_get_post_switch
198 # assert_equal @response.body, 'request method: POST'
200 # assert_equal @response.body, 'request method: GET'
202 # assert_equal @response.body, 'request method: POST'
204 # assert_equal @response.body, 'request method: GET'
207 # test the redirection to a named route
208 def test_assert_redirect_to_named_route
209 with_routing do |set|
211 map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing'
212 map.connect ':controller/:action/:id'
216 process :redirect_to_named_route
217 assert_redirected_to 'http://test.host/route_one'
218 assert_redirected_to route_one_url
219 assert_redirected_to :route_one_url
223 def test_assert_redirect_to_named_route_failure
224 with_routing do |set|
226 map.route_one 'route_one', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'one'
227 map.route_two 'route_two', :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
228 map.connect ':controller/:action/:id'
230 process :redirect_to_named_route
231 assert_raise(Test::Unit::AssertionFailedError) do
232 assert_redirected_to 'http://test.host/route_two'
234 assert_raise(Test::Unit::AssertionFailedError) do
235 assert_redirected_to :controller => 'action_pack_assertions', :action => 'nothing', :id => 'two'
237 assert_raise(Test::Unit::AssertionFailedError) do
238 assert_redirected_to route_two_url
240 assert_raise(Test::Unit::AssertionFailedError) do
241 assert_redirected_to :route_two_url
246 def test_assert_redirect_to_nested_named_route
247 with_routing do |set|
249 map.admin_inner_module 'admin/inner_module', :controller => 'admin/inner_module', :action => 'index'
250 map.connect ':controller/:action/:id'
252 @controller = Admin::InnerModuleController.new
253 process :redirect_to_index
254 # redirection is <{"action"=>"index", "controller"=>"admin/admin/inner_module"}>
255 assert_redirected_to admin_inner_module_path
259 def test_assert_redirected_to_top_level_named_route_from_nested_controller
260 with_routing do |set|
262 map.top_level '/action_pack_assertions/:id', :controller => 'action_pack_assertions', :action => 'index'
263 map.connect ':controller/:action/:id'
265 @controller = Admin::InnerModuleController.new
266 process :redirect_to_top_level_named_route
267 # passes -> assert_redirected_to "http://test.host/action_pack_assertions/foo"
268 assert_redirected_to "/action_pack_assertions/foo"
272 # -- standard request/response object testing --------------------------------
274 # make sure that the template objects exist
275 def test_template_objects_alive
277 assert !@response.has_template_object?('hi')
278 assert @response.has_template_object?('howdy')
281 # make sure we don't have template objects when we shouldn't
282 def test_template_object_missing
284 assert_nil @response.template_objects['howdy']
287 # check the empty flashing
288 def test_flash_me_naked
289 process :flash_me_naked
290 assert !@response.has_flash?
291 assert !@response.has_flash_with_contents?
294 # check if we have flash objects
297 assert @response.has_flash?
298 assert @response.has_flash_with_contents?
299 assert @response.has_flash_object?('hello')
302 # ensure we don't have flash objects
303 def test_flash_have_nots
305 assert !@response.has_flash?
306 assert !@response.has_flash_with_contents?
307 assert_nil @response.flash['hello']
310 # check if we were rendered by a file-based template?
311 def test_rendered_action
313 assert !@response.rendered_with_file?
316 assert @response.rendered_with_file?
317 assert 'hello_world', @response.rendered_file
320 # check the redirection location
321 def test_redirection_location
322 process :redirect_internal
323 assert_equal 'http://test.host/nothing', @response.redirect_url
325 process :redirect_external
326 assert_equal 'http://www.rubyonrails.org', @response.redirect_url
329 def test_no_redirect_url
331 assert_nil @response.redirect_url
335 # check server errors
336 def test_server_error_response_code
338 assert @response.server_error?
341 assert @response.server_error?
344 assert !@response.server_error?
347 # check a 404 response code
348 def test_missing_response_code
350 assert @response.missing?
353 # check to see if our redirection matches a pattern
354 def test_redirect_url_match
355 process :redirect_external
356 assert @response.redirect?
357 assert @response.redirect_url_match?("rubyonrails")
358 assert @response.redirect_url_match?(/rubyonrails/)
359 assert !@response.redirect_url_match?("phpoffrails")
360 assert !@response.redirect_url_match?(/perloffrails/)
363 # check for a redirection
365 process :redirect_internal
366 assert @response.redirect?
368 process :redirect_external
369 assert @response.redirect?
372 assert !@response.redirect?
375 # check a successful response code
376 def test_successful_response_code
378 assert @response.success?
381 # a basic check to make sure we have a TestResponse object
382 def test_has_response
384 assert_kind_of ActionController::TestResponse, @response
387 def test_render_based_on_parameters
388 process :render_based_on_parameters, "name" => "David"
389 assert_equal "Mr. David", @response.body
392 def test_follow_redirect
393 process :redirect_to_action
394 assert_redirected_to :action => "flash_me"
397 assert_equal 1, @request.parameters["id"].to_i
399 assert "Inconceivable!", @response.body
402 def test_follow_redirect_outside_current_action
403 process :redirect_to_controller
404 assert_redirected_to :controller => "elsewhere", :action => "flash_me"
406 assert_raises(RuntimeError, "Can't follow redirects outside of current controller (elsewhere)") { follow_redirect }
409 def test_assert_redirection_fails_with_incorrect_controller
410 process :redirect_to_controller
411 assert_raise(Test::Unit::AssertionFailedError) do
412 assert_redirected_to :controller => "action_pack_assertions", :action => "flash_me"
416 def test_assert_redirection_with_extra_controller_option
417 get :redirect_to_action
418 assert_redirected_to :controller => 'action_pack_assertions', :action => "flash_me", :id => 1, :params => { :panda => 'fun' }
421 def test_redirected_to_url_leadling_slash
422 process :redirect_to_path
423 assert_redirected_to '/some/path'
425 def test_redirected_to_url_no_leadling_slash
426 process :redirect_to_path
427 assert_redirected_to 'some/path'
429 def test_redirected_to_url_full_url
430 process :redirect_to_path
431 assert_redirected_to 'http://test.host/some/path'
434 def test_assert_redirection_with_symbol
435 process :redirect_to_controller_with_symbol
436 assert_nothing_raised {
437 assert_redirected_to :controller => "elsewhere", :action => "flash_me"
439 process :redirect_to_controller_with_symbol
440 assert_nothing_raised {
441 assert_redirected_to :controller => :elsewhere, :action => :flash_me
445 def test_redirected_to_with_nested_controller
446 @controller = Admin::InnerModuleController.new
447 get :redirect_to_absolute_controller
448 assert_redirected_to :controller => 'content'
450 get :redirect_to_fellow_controller
451 assert_redirected_to :controller => 'admin/user'
454 def test_assert_valid
455 get :get_valid_record
456 assert_valid assigns('record')
459 def test_assert_valid_failing
460 get :get_invalid_record
463 assert_valid assigns('record')
465 rescue Test::Unit::AssertionFailedError => e
470 class ActionPackHeaderTest < Test::Unit::TestCase
472 @controller = ActionPackAssertionsController.new
473 @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
476 def test_rendering_xml_sets_content_type
477 process :hello_xml_world
478 assert_equal('application/xml; charset=utf-8', @response.headers['type'])
481 def test_rendering_xml_respects_content_type
482 @response.headers['type'] = 'application/pdf'
483 process :hello_xml_world
484 assert_equal('application/pdf; charset=utf-8', @response.headers['type'])
488 def test_render_text_with_custom_content_type
489 get :render_text_with_custom_content_type
490 assert_equal 'application/rss+xml; charset=utf-8', @response.headers['type']