diff options
Diffstat (limited to 'actionpack/test/controller')
-rw-r--r-- | actionpack/test/controller/action_pack_assertions_test.rb | 323 | ||||
-rw-r--r-- | actionpack/test/controller/active_record_assertions_test.rb | 119 | ||||
-rwxr-xr-x | actionpack/test/controller/cgi_test.rb | 142 | ||||
-rw-r--r-- | actionpack/test/controller/cookie_test.rb | 38 | ||||
-rw-r--r-- | actionpack/test/controller/filters_test.rb | 159 | ||||
-rw-r--r-- | actionpack/test/controller/flash_test.rb | 69 | ||||
-rw-r--r-- | actionpack/test/controller/helper_test.rb | 110 | ||||
-rw-r--r-- | actionpack/test/controller/layout_test.rb | 49 | ||||
-rwxr-xr-x | actionpack/test/controller/redirect_test.rb | 44 | ||||
-rw-r--r-- | actionpack/test/controller/render_test.rb | 178 | ||||
-rw-r--r-- | actionpack/test/controller/send_file_test.rb | 68 | ||||
-rw-r--r-- | actionpack/test/controller/url_test.rb | 368 |
12 files changed, 1667 insertions, 0 deletions
diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb new file mode 100644 index 0000000000..6d727be5a2 --- /dev/null +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -0,0 +1,323 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +# a controller class to facilitate the tests +class ActionPackAssertionsController < ActionController::Base + + # this does absolutely nothing + def nothing() render_text ""; end + + # a standard template + def hello_world() render "test/hello_world"; end + + # a standard template + def hello_xml_world() render "test/hello_xml_world"; end + + # a redirect to an internal location + def redirect_internal() redirect_to "nothing"; end + + # a redirect to an external location + def redirect_external() redirect_to_url "http://www.rubyonrails.org"; end + + # a 404 + def response404() render_text "", "404 AWOL"; end + + # a 500 + def response500() render_text "", "500 Sorry"; end + + # a fictional 599 + def response599() render_text "", "599 Whoah!"; end + + # putting stuff in the flash + def flash_me + flash['hello'] = 'my name is inigo montoya...' + render_text "Inconceivable!" + end + + # we have a flash, but nothing is in it + def flash_me_naked + flash.clear + render_text "wow!" + end + + # assign some template instance variables + def assign_this + @howdy = "ho" + render_text "Mr. Henke" + end + + def render_based_on_parameters + render_text "Mr. #{@params["name"]}" + end + + # puts something in the session + def session_stuffing + session['xmas'] = 'turkey' + render_text "ho ho ho" + end + + # 911 + def rescue_action(e) raise; end + +end + +# --------------------------------------------------------------------------- + + +# tell the controller where to find its templates but start from parent +# directory of test_request_response to simulate the behaviour of a +# production environment +ActionPackAssertionsController.template_root = File.dirname(__FILE__) + "/../fixtures/" + + +# a test case to exercise the new capabilities TestRequest & TestResponse +class ActionPackAssertionsControllerTest < Test::Unit::TestCase + # let's get this party started + def setup + @controller = ActionPackAssertionsController.new + @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new + end + + # -- assertion-based testing ------------------------------------------------ + + # test the session assertion to make sure something is there. + def test_assert_session_has + process :session_stuffing + assert_session_has 'xmas' + assert_session_has_no 'halloween' + end + + # test the assertion of goodies in the template + def test_assert_template_has + process :assign_this + assert_template_has 'howdy' + end + + # test the assertion for goodies that shouldn't exist in the template + def test_assert_template_has_no + process :nothing + assert_template_has_no 'maple syrup' + assert_template_has_no 'howdy' + end + + # test the redirection assertions + def test_assert_redirect + process :redirect_internal + assert_redirect + end + + # test the redirect url string + def test_assert_redirect_url + process :redirect_external + assert_redirect_url 'http://www.rubyonrails.org' + end + + # test the redirection pattern matching on a string + def test_assert_redirect_url_match_string + process :redirect_external + assert_redirect_url_match 'rails.org' + end + + # test the redirection pattern matching on a pattern + def test_assert_redirect_url_match_pattern + process :redirect_external + assert_redirect_url_match /ruby/ + end + + # test the flash-based assertions with something is in the flash + def test_flash_assertions_full + process :flash_me + assert @response.has_flash_with_contents? + assert_flash_exists + assert ActionController::TestResponse.assertion_target.has_flash_with_contents? + assert_flash_not_empty + assert_flash_has 'hello' + assert_flash_has_no 'stds' + end + + # test the flash-based assertions with no flash at all + def test_flash_assertions_negative + process :nothing + assert_flash_not_exists + assert_flash_empty + assert_flash_has_no 'hello' + assert_flash_has_no 'qwerty' + end + + # test the assert_rendered_file + def test_assert_rendered_file + process :hello_world + assert_rendered_file 'test/hello_world' + assert_rendered_file 'hello_world' + assert_rendered_file + end + + # test the assert_success assertion + def test_assert_success + process :nothing + assert_success + end + + # -- standard request/reponse object testing -------------------------------- + + # ensure our session is working properly + def test_session_objects + process :session_stuffing + assert @response.has_session_object?('xmas') + assert_session_equal 'turkey', 'xmas' + assert !@response.has_session_object?('easter') + end + + # make sure that the template objects exist + def test_template_objects_alive + process :assign_this + assert !@response.has_template_object?('hi') + assert @response.has_template_object?('howdy') + end + + # make sure we don't have template objects when we shouldn't + def test_template_object_missing + process :nothing + assert_nil @response.template_objects['howdy'] + end + + def test_assigned_equal + process :assign_this + assert_assigned_equal "ho", :howdy + end + + # check the empty flashing + def test_flash_me_naked + process :flash_me_naked + assert @response.has_flash? + assert !@response.has_flash_with_contents? + end + + # check if we have flash objects + def test_flash_haves + process :flash_me + assert @response.has_flash? + assert @response.has_flash_with_contents? + assert @response.has_flash_object?('hello') + end + + # ensure we don't have flash objects + def test_flash_have_nots + process :nothing + assert !@response.has_flash? + assert !@response.has_flash_with_contents? + assert_nil @response.flash['hello'] + end + + # examine that the flash objects are what we expect + def test_flash_equals + process :flash_me + assert_flash_equal 'my name is inigo montoya...', 'hello' + end + + + # check if we were rendered by a file-based template? + def test_rendered_action + process :nothing + assert !@response.rendered_with_file? + + process :hello_world + assert @response.rendered_with_file? + assert 'hello_world', @response.rendered_file + end + + # check the redirection location + def test_redirection_location + process :redirect_internal + assert_equal 'nothing', @response.redirect_url + + process :redirect_external + assert_equal 'http://www.rubyonrails.org', @response.redirect_url + + process :nothing + assert_nil @response.redirect_url + end + + + # check server errors + def test_server_error_response_code + process :response500 + assert @response.server_error? + + process :response599 + assert @response.server_error? + + process :response404 + assert !@response.server_error? + end + + # check a 404 response code + def test_missing_response_code + process :response404 + assert @response.missing? + end + + # check to see if our redirection matches a pattern + def test_redirect_url_match + process :redirect_external + assert @response.redirect? + assert @response.redirect_url_match?("rubyonrails") + assert @response.redirect_url_match?(/rubyonrails/) + assert !@response.redirect_url_match?("phpoffrails") + assert !@response.redirect_url_match?(/perloffrails/) + end + + # check for a redirection + def test_redirection + process :redirect_internal + assert @response.redirect? + + process :redirect_external + assert @response.redirect? + + process :nothing + assert !@response.redirect? + end + + # check a successful response code + def test_successful_response_code + process :nothing + assert @response.success? + end + + # a basic check to make sure we have a TestResponse object + def test_has_response + process :nothing + assert_kind_of ActionController::TestResponse, @response + end + + def test_render_based_on_parameters + process :render_based_on_parameters, "name" => "David" + assert_equal "Mr. David", @response.body + end + + def test_simple_one_element_xpath_match + process :hello_xml_world + assert_template_xpath_match('//title', "Hello World") + end + + def test_array_of_elements_in_xpath_match + process :hello_xml_world + assert_template_xpath_match('//p', %w( abes monks wiseguys )) + end +end + +class ActionPackHeaderTest < Test::Unit::TestCase + def setup + @controller = ActionPackAssertionsController.new + @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new + end + def test_rendering_xml_sets_content_type + process :hello_xml_world + assert_equal('text/xml', @controller.headers['Content-Type']) + end + def test_rendering_xml_respects_content_type + @response.headers['Content-Type'] = 'application/pdf' + process :hello_xml_world + assert_equal('application/pdf', @controller.headers['Content-Type']) + end +end diff --git a/actionpack/test/controller/active_record_assertions_test.rb b/actionpack/test/controller/active_record_assertions_test.rb new file mode 100644 index 0000000000..53106aaee7 --- /dev/null +++ b/actionpack/test/controller/active_record_assertions_test.rb @@ -0,0 +1,119 @@ +path_to_ar = File.dirname(__FILE__) + '/../../../activerecord' + +if Object.const_defined?("ActiveRecord") || File.exist?(path_to_ar) +# This test is very different than the others. It requires ActiveRecord to +# run. There's a bunch of stuff we are assuming here: +# +# 1. activerecord exists as a sibling directory to actionpack +# (i.e., actionpack/../activerecord) +# 2. you've created the appropriate database to run the active_record unit tests +# 3. you set the appropriate database connection below + +driver_to_use = 'native_sqlite' + +$: << path_to_ar + '/lib/' +$: << path_to_ar + '/test/' +require 'active_record' unless Object.const_defined?("ActiveRecord") +require "connections/#{driver_to_use}/connection" +require 'fixtures/company' + +# ----------------------------------------------------------------------------- + +# add some validation rules to trip up the assertions +class Company + def validate + errors.add_on_empty('name') + errors.add('rating', 'rating should not be 2') if rating == 2 + errors.add_to_base('oh oh') if rating == 3 + end +end + +# ----------------------------------------------------------------------------- + +require File.dirname(__FILE__) + '/../abstract_unit' + +# a controller class to handle the AR assertions +class ActiveRecordAssertionsController < ActionController::Base + # fail with 1 bad column + def nasty_columns_1 + @company = Company.new + @company.name = "B" + @company.rating = 2 + render_text "snicker...." + end + + # fail with 2 bad column + def nasty_columns_2 + @company = Company.new + @company.name = "" + @company.rating = 2 + render_text "double snicker...." + end + + # this will pass validation + def good_company + @company = Company.new + @company.name = "A" + @company.rating = 69 + render_text "Goodness Gracious!" + end + + # this will fail validation + def bad_company + @company = Company.new + render_text "Who's Bad?" + end + + # the safety dance...... + def rescue_action(e) raise; end +end + +# ----------------------------------------------------------------------------- + +ActiveRecordAssertionsController.template_root = File.dirname(__FILE__) + "/../fixtures/" + +# The test case to try the AR assertions +class ActiveRecordAssertionsControllerTest < Test::Unit::TestCase + # set it up + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @controller = ActiveRecordAssertionsController.new + end + + # test for 1 bad apple column + def test_some_invalid_columns + process :nasty_columns_1 + assert_success + assert_invalid_record 'company' + assert_invalid_column_on_record 'company', 'rating' + assert_valid_column_on_record 'company', 'name' + assert_valid_column_on_record 'company', ['name','id'] + end + + # test for 2 bad apples columns + def test_all_invalid_columns + process :nasty_columns_2 + assert_success + assert_invalid_record 'company' + assert_invalid_column_on_record 'company', 'rating' + assert_invalid_column_on_record 'company', 'name' + assert_invalid_column_on_record 'company', ['name','rating'] + end + + # ensure we have no problems with an ActiveRecord + def test_valid_record + process :good_company + assert_success + assert_valid_record 'company' + end + + # ensure we have problems with an ActiveRecord + def test_invalid_record + process :bad_company + assert_success + assert_invalid_record 'company' + end +end + +end
\ No newline at end of file diff --git a/actionpack/test/controller/cgi_test.rb b/actionpack/test/controller/cgi_test.rb new file mode 100755 index 0000000000..46e24ab403 --- /dev/null +++ b/actionpack/test/controller/cgi_test.rb @@ -0,0 +1,142 @@ +$:.unshift(File.dirname(__FILE__) + '/../../lib') + +require 'test/unit' +require 'action_controller/cgi_ext/cgi_methods' +require 'stringio' + +class MockUploadedFile < StringIO + def content_type + "img/jpeg" + end + + def original_filename + "my_file.doc" + end +end + +class CGITest < Test::Unit::TestCase + def setup + @query_string = "action=create_customer&full_name=David%20Heinemeier%20Hansson&customerId=1" + @query_string_with_nil = "action=create_customer&full_name=" + @query_string_with_array = "action=create_customer&selected[]=1&selected[]=2&selected[]=3" + @query_string_with_amps = "action=create_customer&name=Don%27t+%26+Does" + @query_string_with_multiple_of_same_name = + "action=update_order&full_name=Lau%20Taarnskov&products=4&products=2&products=3" + end + + def test_query_string + assert_equal( + { "action" => "create_customer", "full_name" => "David Heinemeier Hansson", "customerId" => "1"}, + CGIMethods.parse_query_parameters(@query_string) + ) + end + + def test_query_string_with_nil + assert_equal( + { "action" => "create_customer", "full_name" => nil}, + CGIMethods.parse_query_parameters(@query_string_with_nil) + ) + end + + def test_query_string_with_array + assert_equal( + { "action" => "create_customer", "selected" => ["1", "2", "3"]}, + CGIMethods.parse_query_parameters(@query_string_with_array) + ) + end + + def test_query_string_with_amps + assert_equal( + { "action" => "create_customer", "name" => "Don't & Does"}, + CGIMethods.parse_query_parameters(@query_string_with_amps) + ) + end + + def test_parse_params + input = { + "customers[boston][first][name]" => [ "David" ], + "customers[boston][first][url]" => [ "http://David" ], + "customers[boston][second][name]" => [ "Allan" ], + "customers[boston][second][url]" => [ "http://Allan" ], + "something_else" => [ "blah" ], + "something_nil" => [ nil ], + "something_empty" => [ "" ], + "products[first]" => [ "Apple Computer" ], + "products[second]" => [ "Pc" ] + } + + expected_output = { + "customers" => { + "boston" => { + "first" => { + "name" => "David", + "url" => "http://David" + }, + "second" => { + "name" => "Allan", + "url" => "http://Allan" + } + } + }, + "something_else" => "blah", + "something_empty" => "", + "something_nil" => "", + "products" => { + "first" => "Apple Computer", + "second" => "Pc" + } + } + + assert_equal expected_output, CGIMethods.parse_request_parameters(input) + end + + def test_parse_params_from_multipart_upload + mock_file = MockUploadedFile.new + + input = { + "something" => [ StringIO.new("") ], + "products[string]" => [ StringIO.new("Apple Computer") ], + "products[file]" => [ mock_file ] + } + + expected_output = { + "something" => "", + "products" => { + "string" => "Apple Computer", + "file" => mock_file + } + } + + assert_equal expected_output, CGIMethods.parse_request_parameters(input) + end + + def test_parse_params_with_file + input = { + "customers[boston][first][name]" => [ "David" ], + "something_else" => [ "blah" ], + "logo" => [ File.new(File.dirname(__FILE__) + "/cgi_test.rb").path ] + } + + expected_output = { + "customers" => { + "boston" => { + "first" => { + "name" => "David" + } + } + }, + "something_else" => "blah", + "logo" => File.new(File.dirname(__FILE__) + "/cgi_test.rb").path, + } + + assert_equal expected_output, CGIMethods.parse_request_parameters(input) + end + + def test_parse_params_with_array + input = { "selected[]" => [ "1", "2", "3" ] } + + expected_output = { "selected" => [ "1", "2", "3" ] } + + assert_equal expected_output, CGIMethods.parse_request_parameters(input) + end +end diff --git a/actionpack/test/controller/cookie_test.rb b/actionpack/test/controller/cookie_test.rb new file mode 100644 index 0000000000..d3099bcd99 --- /dev/null +++ b/actionpack/test/controller/cookie_test.rb @@ -0,0 +1,38 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class CookieTest < Test::Unit::TestCase + class TestController < ActionController::Base + def authenticate + cookie "name" => "user_name", "value" => "david" + render_text "hello world" + end + + def access_frozen_cookies + @cookies["wont"] = "work" + end + + def rescue_action(e) raise end + end + + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @request.host = "www.nextangle.com" + end + + def test_setting_cookie + @request.action = "authenticate" + assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david") ], process_request.headers["cookie"] + end + + def test_setting_cookie + @request.action = "access_frozen_cookies" + assert_raises(TypeError) { process_request } + end + + private + def process_request + TestController.process(@request, @response) + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb new file mode 100644 index 0000000000..f4d7a689b5 --- /dev/null +++ b/actionpack/test/controller/filters_test.rb @@ -0,0 +1,159 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class FilterTest < Test::Unit::TestCase + class TestController < ActionController::Base + before_filter :ensure_login + + def show + render_text "ran action" + end + + private + def ensure_login + @ran_filter ||= [] + @ran_filter << "ensure_login" + end + end + + class PrependingController < TestController + prepend_before_filter :wonderful_life + + private + def wonderful_life + @ran_filter ||= [] + @ran_filter << "wonderful_life" + end + end + + class ProcController < PrependingController + before_filter(proc { |c| c.assigns["ran_proc_filter"] = true }) + end + + class ImplicitProcController < PrependingController + before_filter { |c| c.assigns["ran_proc_filter"] = true } + end + + class AuditFilter + def self.filter(controller) + controller.assigns["was_audited"] = true + end + end + + class AroundFilter + def before(controller) + @execution_log = "before" + controller.class.execution_log << " before aroundfilter " if controller.respond_to? :execution_log + controller.assigns["before_ran"] = true + end + + def after(controller) + controller.assigns["execution_log"] = @execution_log + " and after" + controller.assigns["after_ran"] = true + controller.class.execution_log << " after aroundfilter " if controller.respond_to? :execution_log + end + end + + class AppendedAroundFilter + def before(controller) + controller.class.execution_log << " before appended aroundfilter " + end + + def after(controller) + controller.class.execution_log << " after appended aroundfilter " + end + end + + class AuditController < ActionController::Base + before_filter(AuditFilter) + + def show + render_text "hello" + end + end + + class BadFilterController < ActionController::Base + before_filter 2 + + def show() "show" end + + protected + def rescue_action(e) raise(e) end + end + + class AroundFilterController < PrependingController + around_filter AroundFilter.new + end + + class MixedFilterController < PrependingController + cattr_accessor :execution_log + def initialize + @@execution_log = "" + end + + before_filter { |c| c.class.execution_log << " before procfilter " } + prepend_around_filter AroundFilter.new + + after_filter { |c| c.class.execution_log << " after procfilter " } + append_around_filter AppendedAroundFilter.new + end + + + def test_added_filter_to_inheritance_graph + assert_equal [ :fire_flash, :ensure_login ], TestController.before_filters + end + + def test_base_class_in_isolation + assert_equal [ :fire_flash ], ActionController::Base.before_filters + end + + def test_prepending_filter + assert_equal [ :wonderful_life, :fire_flash, :ensure_login ], PrependingController.before_filters + end + + def test_running_filters + assert_equal %w( wonderful_life ensure_login ), test_process(PrependingController).template.assigns["ran_filter"] + end + + def test_running_filters_with_proc + assert test_process(ProcController).template.assigns["ran_proc_filter"] + end + + def test_running_filters_with_implicit_proc + assert test_process(ImplicitProcController).template.assigns["ran_proc_filter"] + end + + def test_running_filters_with_class + assert test_process(AuditController).template.assigns["was_audited"] + end + + def test_bad_filter + assert_raises(ActionController::ActionControllerError) { + test_process(BadFilterController) + } + end + + def test_around_filter + controller = test_process(AroundFilterController) + assert controller.template.assigns["before_ran"] + assert controller.template.assigns["after_ran"] + end + + def test_having_properties_in_around_filter + controller = test_process(AroundFilterController) + assert_equal "before and after", controller.template.assigns["execution_log"] + end + + def test_prepending_and_appending_around_filter + controller = test_process(MixedFilterController) + assert_equal " before aroundfilter before procfilter before appended aroundfilter " + + " after appended aroundfilter after aroundfilter after procfilter ", + MixedFilterController.execution_log + end + + private + def test_process(controller) + request = ActionController::TestRequest.new + request.action = "show" + controller.process(request, ActionController::TestResponse.new) + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb new file mode 100644 index 0000000000..033477fe39 --- /dev/null +++ b/actionpack/test/controller/flash_test.rb @@ -0,0 +1,69 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class FlashTest < Test::Unit::TestCase + class TestController < ActionController::Base + def set_flash + flash["that"] = "hello" + render_text "hello" + end + + def use_flash + @flashy = flash["that"] + render_text "hello" + end + + def use_flash_and_keep_it + @flashy = flash["that"] + keep_flash + render_text "hello" + end + + def rescue_action(e) + raise unless ActionController::MissingTemplate === e + end + end + + def setup + initialize_request_and_response + end + + def test_flash + @request.action = "set_flash" + response = process_request + + @request.action = "use_flash" + first_response = process_request + assert_equal "hello", first_response.template.assigns["flash"]["that"] + assert_equal "hello", first_response.template.assigns["flashy"] + + second_response = process_request + assert_nil second_response.template.assigns["flash"]["that"], "On second flash" + end + + def test_keep_flash + @request.action = "set_flash" + response = process_request + + @request.action = "use_flash_and_keep_it" + first_response = process_request + assert_equal "hello", first_response.template.assigns["flash"]["that"] + assert_equal "hello", first_response.template.assigns["flashy"] + + @request.action = "use_flash" + second_response = process_request + assert_equal "hello", second_response.template.assigns["flash"]["that"], "On second flash" + + third_response = process_request + assert_nil third_response.template.assigns["flash"]["that"], "On third flash" + end + + private + def initialize_request_and_response + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + def process_request + TestController.process(@request, @response) + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/helper_test.rb b/actionpack/test/controller/helper_test.rb new file mode 100644 index 0000000000..9d1da53241 --- /dev/null +++ b/actionpack/test/controller/helper_test.rb @@ -0,0 +1,110 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class HelperTest < Test::Unit::TestCase + HELPER_PATHS = %w(/../fixtures/helpers) + + class TestController < ActionController::Base + attr_accessor :delegate_attr + def delegate_method() end + def rescue_action(e) raise end + end + + module LocalAbcHelper + def a() end + def b() end + def c() end + end + + + def setup + # Increment symbol counter. + @symbol = (@@counter ||= 'A0').succ!.dup + + # Generate new controller class. + controller_class_name = "Helper#{@symbol}Controller" + eval("class #{controller_class_name} < TestController; end") + @controller_class = self.class.const_get(controller_class_name) + + # Generate new template class and assign to controller. + template_class_name = "Test#{@symbol}View" + eval("class #{template_class_name} < ActionView::Base; end") + @template_class = self.class.const_get(template_class_name) + @controller_class.template_class = @template_class + + # Add helper paths to LOAD_PATH. + HELPER_PATHS.each { |path| + $LOAD_PATH.unshift(File.dirname(__FILE__) + path) + } + + # Set default test helper. + self.test_helper = LocalAbcHelper + end + + def teardown + # Reset template class. + #ActionController::Base.template_class = ActionView::Base + + # Remove helper paths from LOAD_PATH. + HELPER_PATHS.each { |path| + $LOAD_PATH.delete(File.dirname(__FILE__) + path) + } + end + + + def test_deprecated_helper + assert_equal helper_methods, missing_methods + assert_nothing_raised { @controller_class.helper TestHelper } + assert_equal [], missing_methods + end + + def test_declare_helper + require 'abc_helper' + self.test_helper = AbcHelper + assert_equal helper_methods, missing_methods + assert_nothing_raised { @controller_class.helper :abc } + assert_equal [], missing_methods + end + + def test_declare_missing_helper + assert_equal helper_methods, missing_methods + assert_raise(LoadError) { @controller_class.helper :missing } + end + + def test_helper_block + assert_nothing_raised { + @controller_class.helper { def block_helper_method; end } + } + assert template_methods.include?('block_helper_method') + end + + def test_helper_block_include + assert_equal helper_methods, missing_methods + assert_nothing_raised { + @controller_class.helper { include TestHelper } + } + assert [], missing_methods + end + + def test_helper_method + assert_nothing_raised { @controller_class.helper_method :delegate_method } + assert template_methods.include?('delegate_method') + end + + def test_helper_attr + assert_nothing_raised { @controller_class.helper_attr :delegate_attr } + assert template_methods.include?('delegate_attr') + assert template_methods.include?('delegate_attr=') + end + + + private + def helper_methods; TestHelper.instance_methods end + def template_methods; @template_class.instance_methods end + def missing_methods; helper_methods - template_methods end + + def test_helper=(helper_module) + old_verbose, $VERBOSE = $VERBOSE, nil + self.class.const_set('TestHelper', helper_module) + $VERBOSE = old_verbose + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb new file mode 100644 index 0000000000..f652453ebd --- /dev/null +++ b/actionpack/test/controller/layout_test.rb @@ -0,0 +1,49 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class TestLayoutController < ActionController::Base + layout "layouts/standard" + + def hello_world + end + + def hello_world_outside_layout + end + + def rescue_action(e) raise end +end + +class ChildWithoutTestLayoutController < TestLayoutController + layout nil + + def hello_world + end +end + +class ChildWithOtherTestLayoutController < TestLayoutController + layout nil + + def hello_world + end +end + +class RenderTest < Test::Unit::TestCase + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @request.host = "www.nextangle.com" + end + + def test_layout_rendering + @request.action = "hello_world" + response = process_request + assert_equal "200 OK", response.headers["Status"] + assert_equal "layouts/standard", response.template.template_name + end + + + private + def process_request + TestLayoutController.process(@request, @response) + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/redirect_test.rb b/actionpack/test/controller/redirect_test.rb new file mode 100755 index 0000000000..6302016a53 --- /dev/null +++ b/actionpack/test/controller/redirect_test.rb @@ -0,0 +1,44 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class RedirectTest < Test::Unit::TestCase + class RedirectController < ActionController::Base + def simple_redirect + redirect_to :action => "hello_world" + end + + def method_redirect + redirect_to :dashbord_url, 1, "hello" + end + + def rescue_errors(e) raise e end + + protected + def dashbord_url(id, message) + url_for :action => "dashboard", :params => { "id" => id, "message" => message } + end + end + + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + def test_simple_redirect + @request.path = "/redirect/simple_redirect" + @request.action = "simple_redirect" + response = process_request + assert_equal "http://test.host/redirect/hello_world", response.headers["location"] + end + + def test_redirect_with_method_reference_and_parameters + @request.path = "/redirect/method_redirect" + @request.action = "method_redirect" + response = process_request + assert_equal "http://test.host/redirect/dashboard?message=hello&id=1", response.headers["location"] + end + + private + def process_request + RedirectController.process(@request, @response) + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb new file mode 100644 index 0000000000..ce778e1d7d --- /dev/null +++ b/actionpack/test/controller/render_test.rb @@ -0,0 +1,178 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +Customer = Struct.new("Customer", :name) + +class RenderTest < Test::Unit::TestCase + class TestController < ActionController::Base + layout :determine_layout + + def hello_world + end + + def render_hello_world + render "test/hello_world" + end + + def render_hello_world_from_variable + @person = "david" + render_text "hello #{@person}" + end + + def render_action_hello_world + render_action "hello_world" + end + + def render_text_hello_world + render_text "hello world" + end + + def render_custom_code + render_text "hello world", "404 Moved" + end + + def render_xml_hello + @name = "David" + render "test/hello" + end + + def greeting + # let's just rely on the template + end + + def layout_test + render_action "hello_world" + end + + def builder_layout_test + render_action "hello" + end + + def partials_list + @customers = [ Customer.new("david"), Customer.new("mary") ] + render_action "list" + end + + def modgreet + end + + def rescue_action(e) raise end + + private + def determine_layout + case action_name + when "layout_test": "layouts/standard" + when "builder_layout_test": "layouts/builder" + end + end + end + + TestController.template_root = File.dirname(__FILE__) + "/../fixtures/" + + class TestLayoutController < ActionController::Base + layout "layouts/standard" + + def hello_world + end + + def hello_world_outside_layout + end + + def rescue_action(e) + raise unless ActionController::MissingTemplate === e + end + end + + def setup + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + + @request.host = "www.nextangle.com" + end + + def test_simple_show + @request.action = "hello_world" + response = process_request + assert_equal "200 OK", response.headers["Status"] + assert_equal "test/hello_world", response.template.first_render + end + + def test_do_with_render + @request.action = "render_hello_world" + assert_equal "test/hello_world", process_request.template.first_render + end + + def test_do_with_render_from_variable + @request.action = "render_hello_world_from_variable" + assert_equal "hello david", process_request.body + end + + def test_do_with_render_action + @request.action = "render_action_hello_world" + assert_equal "test/hello_world", process_request.template.first_render + end + + def test_do_with_render_text + @request.action = "render_text_hello_world" + assert_equal "hello world", process_request.body + end + + def test_do_with_render_custom_code + @request.action = "render_custom_code" + assert_equal "404 Moved", process_request.headers["Status"] + end + + def test_attempt_to_access_object_method + @request.action = "clone" + assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { process_request } + end + + def test_access_to_request_in_view + ActionController::Base.view_controller_internals = false + + @request.action = "hello_world" + response = process_request + assert_nil response.template.assigns["request"] + + ActionController::Base.view_controller_internals = true + + @request.action = "hello_world" + response = process_request + assert_kind_of ActionController::AbstractRequest, response.template.assigns["request"] + end + + def test_render_xml + @request.action = "render_xml_hello" + assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", process_request.body + end + + def test_render_xml_with_default + @request.action = "greeting" + assert_equal "<p>This is grand!</p>\n", process_request.body + end + + def test_layout_rendering + @request.action = "layout_test" + assert_equal "<html>Hello world!</html>", process_request.body + end + + def test_render_xml_with_layouts + @request.action = "builder_layout_test" + assert_equal "<wrapper>\n<html>\n <p>Hello </p>\n<p>This is grand!</p>\n</html>\n</wrapper>\n", process_request.body + end + + def test_partials_list + @request.action = "partials_list" + assert_equal "Hello: davidHello: mary", process_request.body + end + + def test_module_rendering + @request.action = "modgreet" + @request.parameters["module"] = "scope" + assert_equal "<p>Beautiful modules!</p>", process_request.body + end + + private + def process_request + TestController.process(@request, @response) + end +end
\ No newline at end of file diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb new file mode 100644 index 0000000000..57205a01c5 --- /dev/null +++ b/actionpack/test/controller/send_file_test.rb @@ -0,0 +1,68 @@ +require File.join(File.dirname(__FILE__), '..', 'abstract_unit') + + +module TestFileUtils + def file_name() File.basename(__FILE__) end + def file_path() File.expand_path(__FILE__) end + def file_data() File.open(file_path, 'rb') { |f| f.read } end +end + + +class SendFileController < ActionController::Base + include TestFileUtils + + attr_writer :options + def options() @options ||= {} end + + def file() send_file(file_path, options) end + def data() send_data(file_data, options) end + + def rescue_action(e) raise end +end + + +class SendFileTest < Test::Unit::TestCase + include TestFileUtils + + def setup + @controller = SendFileController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + def test_file_nostream + @controller.options = { :stream => false } + response = nil + assert_nothing_raised { response = process('file') } + assert_not_nil response + assert_kind_of String, response.body + assert_equal file_data, response.body + end + + def test_file_stream + response = nil + assert_nothing_raised { response = process('file') } + assert_not_nil response + assert_kind_of Proc, response.body + + old_stdout = $stdout + begin + require 'stringio' + $stdout = StringIO.new + $stdout.binmode + assert_nothing_raised { response.body.call } + assert_equal file_data, $stdout.string + ensure + $stdout = old_stdout + end + end + + def test_data + response = nil + assert_nothing_raised { response = process('data') } + assert_not_nil response + + assert_kind_of String, response.body + assert_equal file_data, response.body + end +end diff --git a/actionpack/test/controller/url_test.rb b/actionpack/test/controller/url_test.rb new file mode 100644 index 0000000000..bf6a7aab75 --- /dev/null +++ b/actionpack/test/controller/url_test.rb @@ -0,0 +1,368 @@ +require File.dirname(__FILE__) + '/../abstract_unit' +require 'action_controller/url_rewriter' + +MockRequest = Struct.new("MockRequest", :protocol, :host, :port, :path, :parameters) +class MockRequest + def host_with_port + if (protocol == "http://" && port == 80) || (protocol == "https://" && port == 443) + host + else + host + ":#{port}" + end + end +end + +class UrlTest < Test::Unit::TestCase + def setup + @library_url = ActionController::UrlRewriter.new(MockRequest.new( + "http://", + "www.singlefile.com", + 80, + "/library/books/ISBN/0743536703/show", + { "type" => "ISBN", "code" => "0743536703" } + ), "books", "show") + + @library_url_on_index = ActionController::UrlRewriter.new(MockRequest.new( + "http://", + "www.singlefile.com", + 80, + "/library/books/ISBN/0743536703/", + { "type" => "ISBN", "code" => "0743536703" } + ), "books", "index") + + @clean_urls = [ + ActionController::UrlRewriter.new(MockRequest.new( + "http://", "www.singlefile.com", 80, "/identity/", {} + ), "identity", "index"), + ActionController::UrlRewriter.new(MockRequest.new( + "http://", "www.singlefile.com", 80, "/identity", {} + ), "identity", "index") + ] + + @clean_url_with_id = ActionController::UrlRewriter.new(MockRequest.new( + "http://", "www.singlefile.com", 80, "/identity/show/5", { "id" => "5" } + ), "identity", "show") + + @clean_url_with_id_as_char = ActionController::UrlRewriter.new(MockRequest.new( + "http://", "www.singlefile.com", 80, "/teachers/show/t", { "id" => "t" } + ), "teachers", "show") + end + + def test_clean_action + assert_equal "http://www.singlefile.com/library/books/ISBN/0743536703/edit", @library_url.rewrite(:action => "edit") + end + + def test_clean_action_with_only_path + assert_equal "/library/books/ISBN/0743536703/edit", @library_url.rewrite(:action => "edit", :only_path => true) + end + + def test_action_from_index + assert_equal "http://www.singlefile.com/library/books/ISBN/0743536703/edit", @library_url_on_index.rewrite(:action => "edit") + end + + def test_action_from_index_on_clean + @clean_urls.each do |url| + assert_equal "http://www.singlefile.com/identity/edit", url.rewrite(:action => "edit") + end + end + + def test_action_without_prefix + assert_equal "http://www.singlefile.com/library/books/", @library_url.rewrite(:action => "index", :action_prefix => "") + end + + def test_action_with_prefix + assert_equal( + "http://www.singlefile.com/library/books/XTC/123/show", + @library_url.rewrite(:action => "show", :action_prefix => "XTC/123") + ) + end + + def test_action_prefix_alone + assert_equal( + "http://www.singlefile.com/library/books/XTC/123/", + @library_url.rewrite(:action_prefix => "XTC/123") + ) + end + + def test_action_with_suffix + assert_equal( + "http://www.singlefile.com/library/books/show/XTC/123", + @library_url.rewrite(:action => "show", :action_prefix => "", :action_suffix => "XTC/123") + ) + end + + def test_clean_controller + assert_equal "http://www.singlefile.com/library/settings/", @library_url.rewrite(:controller => "settings") + end + + def test_clean_controller_prefix + assert_equal "http://www.singlefile.com/shop/", @library_url.rewrite(:controller_prefix => "shop") + end + + def test_clean_controller_with_module + assert_equal "http://www.singlefile.com/shop/purchases/", @library_url.rewrite(:module => "shop", :controller => "purchases") + end + + def test_controller_and_action + assert_equal "http://www.singlefile.com/library/settings/show", @library_url.rewrite(:controller => "settings", :action => "show") + end + + def test_controller_and_action_and_anchor + assert_equal( + "http://www.singlefile.com/library/settings/show#5", + @library_url.rewrite(:controller => "settings", :action => "show", :anchor => "5") + ) + end + + def test_controller_and_action_and_empty_overwrite_params_and_anchor + assert_equal( + "http://www.singlefile.com/library/settings/show?code=0743536703&type=ISBN#5", + @library_url.rewrite(:controller => "settings", :action => "show", :overwrite_params => {}, :anchor => "5") + ) + end + + def test_controller_and_action_and_overwrite_params_and_anchor + assert_equal( + "http://www.singlefile.com/library/settings/show?code=0000001&type=ISBN#5", + @library_url.rewrite(:controller => "settings", :action => "show", :overwrite_params => {"code"=>"0000001"}, :anchor => "5") + ) + end + + def test_controller_and_action_and_overwrite_params_with_nil_value_and_anchor + assert_equal( + "http://www.singlefile.com/library/settings/show?type=ISBN#5", + @library_url.rewrite(:controller => "settings", :action => "show", :overwrite_params => {"code" => nil}, :anchor => "5") + ) + end + + def test_controller_and_action_params_and_overwrite_params_and_anchor + assert_equal( + "http://www.singlefile.com/library/settings/show?code=0000001&version=5.0#5", + @library_url.rewrite(:controller => "settings", :action => "show", :params=>{"version" => "5.0"}, :overwrite_params => {"code"=>"0000001"}, :anchor => "5") + ) + end + + def test_controller_and_action_and_params_anchor + assert_equal( + "http://www.singlefile.com/library/settings/show?update=1#5", + @library_url.rewrite(:controller => "settings", :action => "show", :params => { "update" => "1"}, :anchor => "5") + ) + end + + def test_controller_and_index_action + assert_equal "http://www.singlefile.com/library/settings/", @library_url.rewrite(:controller => "settings", :action => "index") + end + + def test_controller_and_action_with_same_name_as_controller + @clean_urls.each do |url| + assert_equal "http://www.singlefile.com/anything/identity", url.rewrite(:controller => "anything", :action => "identity") + end + end + + def test_controller_and_index_action_without_controller_prefix + assert_equal( + "http://www.singlefile.com/settings/", + @library_url.rewrite(:controller => "settings", :action => "index", :controller_prefix => "") + ) + end + + def test_controller_and_index_action_with_controller_prefix + assert_equal( + "http://www.singlefile.com/fantastic/settings/show", + @library_url.rewrite(:controller => "settings", :action => "show", :controller_prefix => "fantastic") + ) + end + + def test_path_parameters + assert_equal "http://www.singlefile.com/library/books/EXBC/0743536703/show", @library_url.rewrite(:path_params => {"type" => "EXBC"}) + end + + def test_parameters + assert_equal( + "http://www.singlefile.com/library/books/ISBN/0743536703/show?delete=1&name=David", + @library_url.rewrite(:params => {"delete" => "1", "name" => "David"}) + ) + end + + def test_parameters_with_id + @clean_urls.each do |url| + assert_equal( + "http://www.singlefile.com/identity/show?name=David&id=5", + url.rewrite( + :action => "show", + :params => { "id" => "5", "name" => "David" } + ) + ) + end + end + + def test_action_with_id + assert_equal( + "http://www.singlefile.com/identity/show/7", + @clean_url_with_id.rewrite( + :action => "show", + :id => 7 + ) + ) + @clean_urls.each do |url| + assert_equal( + "http://www.singlefile.com/identity/index/7", + url.rewrite(:id => 7) + ) + end + end + + def test_parameters_with_id_and_away + assert_equal( + "http://www.singlefile.com/identity/show/25?name=David", + @clean_url_with_id.rewrite( + :path_params => { "id" => "25" }, + :params => { "name" => "David" } + ) + ) + end + + def test_parameters_with_index_and_id + @clean_urls.each do |url| + assert_equal( + "http://www.singlefile.com/identity/index/25?name=David", + url.rewrite( + :path_params => { "id" => "25" }, + :params => { "name" => "David" } + ) + ) + end + end + + def test_action_going_away_from_id + assert_equal( + "http://www.singlefile.com/identity/list", + @clean_url_with_id.rewrite( + :action => "list" + ) + ) + end + + def test_parameters_with_direct_id_and_away + assert_equal( + "http://www.singlefile.com/identity/show/25?name=David", + @clean_url_with_id.rewrite( + :id => "25", + :params => { "name" => "David" } + ) + ) + end + + def test_parameters_with_direct_id_and_away + assert_equal( + "http://www.singlefile.com/store/open/25?name=David", + @clean_url_with_id.rewrite( + :controller => "store", + :action => "open", + :id => "25", + :params => { "name" => "David" } + ) + ) + end + + def test_parameters_to_id + @clean_urls.each do |url| + %w(show index).each do |action| + assert_equal( + "http://www.singlefile.com/identity/#{action}/25?name=David", + url.rewrite( + :action => action, + :path_params => { "id" => "25" }, + :params => { "name" => "David" } + ) + ) + end + end + end + + def test_parameters_from_id + assert_equal( + "http://www.singlefile.com/identity/", + @clean_url_with_id.rewrite( + :action => "index" + ) + ) + end + + def test_id_as_char_and_part_of_controller + assert_equal( + "http://www.singlefile.com/teachers/skill/5", + @clean_url_with_id_as_char.rewrite( + :action => "skill", + :id => 5 + ) + ) + end + + def test_from_clean_to_library + @clean_urls.each do |url| + assert_equal( + "http://www.singlefile.com/library/books/ISBN/0743536703/show?delete=1&name=David", + url.rewrite( + :controller_prefix => "library", + :controller => "books", + :action_prefix => "ISBN/0743536703", + :action => "show", + :params => { "delete" => "1", "name" => "David" } + ) + ) + end + end + + def test_from_library_to_clean + assert_equal( + "http://www.singlefile.com/identity/", + @library_url.rewrite( + :controller => "identity", :controller_prefix => "" + ) + ) + end + + def test_from_another_port + @library_url = ActionController::UrlRewriter.new(MockRequest.new( + "http://", + "www.singlefile.com", + 8080, + "/library/books/ISBN/0743536703/show", + { "type" => "ISBN", "code" => "0743536703" } + ), "books", "show") + + assert_equal( + "http://www.singlefile.com:8080/identity/", + @library_url.rewrite( + :controller => "identity", :controller_prefix => "" + ) + ) + end + + def test_basecamp + basecamp_url = ActionController::UrlRewriter.new(MockRequest.new( + "http://", + "projects.basecamp", + 80, + "/clients/disarray/1/msg/transcripts/", + {"category_name"=>"transcripts", "client_name"=>"disarray", "action"=>"index", "controller"=>"msg", "project_name"=>"1"} + ), "msg", "index") + + assert_equal( + "http://projects.basecamp/clients/disarray/1/msg/transcripts/1/comments", + basecamp_url.rewrite(:action_prefix => "transcripts/1", :action => "comments") + ) + end + + def test_on_explicit_index_page # My index page is very modest, thank you... + url = ActionController::UrlRewriter.new( + MockRequest.new( + "http://", "example.com", 80, "/controller/index", + {"controller"=>"controller", "action"=>"index"} + ), "controller", "index" + ) + assert_equal("http://example.com/controller/foo", url.rewrite(:action => 'foo')) + end + +end |