From 0367317dd62ecd177d57d469a4d57974b75e425b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 22 May 2005 07:43:05 +0000 Subject: Deprecated redirect_to_path and redirect_to_url in favor of letting redirect_to do the right thing when passed either a path or url. Introduced r as a unified method for render (still under construction) git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1349 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../test/controller/action_pack_assertions_test.rb | 4 +- actionpack/test/controller/new_render_test.rb | 237 +++++++++++++++++++++ actionpack/test/controller/request_test.rb | 22 +- .../test/template/form_options_helper_test.rb | 5 +- 4 files changed, 260 insertions(+), 8 deletions(-) create mode 100644 actionpack/test/controller/new_render_test.rb (limited to 'actionpack/test') diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index 3fc8a2c99a..ccde516f31 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -13,7 +13,7 @@ class ActionPackAssertionsController < ActionController::Base def hello_xml_world() render "test/hello_xml_world"; end # a redirect to an internal location - def redirect_internal() redirect_to "nothing"; end + def redirect_internal() redirect_to "/nothing"; end def redirect_to_action() redirect_to :action => "flash_me", :id => 1, :params => { "panda" => "fun" }; end @@ -270,7 +270,7 @@ class ActionPackAssertionsControllerTest < Test::Unit::TestCase # check the redirection location def test_redirection_location process :redirect_internal - assert_equal 'nothing', @response.redirect_url + assert_equal 'http://test.host/nothing', @response.redirect_url process :redirect_external assert_equal 'http://www.rubyonrails.org', @response.redirect_url diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb new file mode 100644 index 0000000000..96037093fc --- /dev/null +++ b/actionpack/test/controller/new_render_test.rb @@ -0,0 +1,237 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +Customer = Struct.new("Customer", :name) + +module Fun + class GamesController < ActionController::Base + def hello_world + end + end +end + + +class TestController < ActionController::Base + layout :determine_layout + + def hello_world + end + + def render_hello_world + r :template => "test/hello_world" + end + + def render_hello_world_from_variable + @person = "david" + r :text => "hello #{@person}" + end + + def render_action_hello_world + r :action => "hello_world" + end + + def render_text_hello_world + r :text => "hello world" + end + + def render_custom_code + r :text => "hello world", :status => "404 Moved" + end + + def render_xml_hello + @name = "David" + r :template => "test/hello" + end + + def greeting + # let's just rely on the template + end + + def layout_test + r :action => "hello_world" + end + + def layout_test_with_different_layout + r :action => "hello_world", :layout => "standard" + end + + def rendering_without_layout + r :action => "hello_world", :layout => false + end + + def builder_layout_test + r :action => "hello" + end + + def partials_list + @customers = [ Customer.new("david"), Customer.new("mary") ] + r :action => "list" + end + + def partial_only + render_partial + end + + def hello_in_a_string + @customers = [ Customer.new("david"), Customer.new("mary") ] + r :text => "How's there? #{render_to_string("test/list")}" + end + + def accessing_params_in_template + r :inline => "Hello: <%= params[:name] %>" + end + + def rescue_action(e) raise end + + private + def determine_layout + case action_name + when "layout_test", "rendering_without_layout" + "layouts/standard" + when "builder_layout_test" + "layouts/builder" + end + end +end + +TestController.template_root = File.dirname(__FILE__) + "/../fixtures/" +Fun::GamesController.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 + +class RenderTest < Test::Unit::TestCase + 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_private_methods + @request.action = "determine_layout" + assert_raises(ActionController::UnknownAction, "No action responded to [determine_layout]") { 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 "\n

Hello David

\n

This is grand!

\n\n", process_request.body + end + + def test_render_xml_with_default + @request.action = "greeting" + assert_equal "

This is grand!

\n", process_request.body + end + + def test_layout_rendering + @request.action = "layout_test" + assert_equal "Hello world!", process_request.body + end + + def test_layout_test_with_different_layout + @request.action = "layout_test_with_different_layout" + assert_equal "Hello world!", process_request.body + end + + def test_rendering_without_layout + @request.action = "rendering_without_layout" + assert_equal "Hello world!", process_request.body + end + + def test_render_xml_with_layouts + @request.action = "builder_layout_test" + assert_equal "\n\n

Hello

\n

This is grand!

\n\n
\n", process_request.body + end + + def test_partials_list + @request.action = "partials_list" + assert_equal "Hello: davidHello: mary", process_request.body + end + + def test_partial_only + @request.action = "partial_only" + assert_equal "only partial", process_request.body + end + + def test_render_to_string + @request.action = "hello_in_a_string" + assert_equal "How's there? Hello: davidHello: mary", process_request.body + end + + def test_nested_rendering + @request.action = "hello_world" + assert_equal "Living in a nested world", Fun::GamesController.process(@request, @response).body + end + + def test_accessing_params_in_template + @request.action = "accessing_params_in_template" + @request.query_parameters[:name] = "David" + assert_equal "Hello: David", 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/request_test.rb b/actionpack/test/controller/request_test.rb index 76082c2dbd..00fd41b8dd 100644 --- a/actionpack/test/controller/request_test.rb +++ b/actionpack/test/controller/request_test.rb @@ -65,7 +65,7 @@ class RequestTest < Test::Unit::TestCase assert_equal ":8080", @request.port_string end - def test_relative_url_root + def test_relative_url_root @request.env['SCRIPT_NAME'] = nil assert_equal "", @request.relative_url_root @@ -75,92 +75,108 @@ class RequestTest < Test::Unit::TestCase @request.env['SCRIPT_NAME'] = "/myapp.rb" assert_equal "", @request.relative_url_root + @request.relative_url_root = nil @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi" assert_equal "/hieraki", @request.relative_url_root + @request.relative_url_root = nil @request.env['SCRIPT_NAME'] = "/collaboration/hieraki/dispatch.cgi" assert_equal "/collaboration/hieraki", @request.relative_url_root end def test_request_uri + @request.relative_url_root = nil @request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri?mapped=1" assert_equal "/path/of/some/uri?mapped=1", @request.request_uri assert_equal "/path/of/some/uri", @request.path + @request.relative_url_root = nil @request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri" assert_equal "/path/of/some/uri", @request.request_uri assert_equal "/path/of/some/uri", @request.path + @request.relative_url_root = nil @request.set_REQUEST_URI "/path/of/some/uri" assert_equal "/path/of/some/uri", @request.request_uri assert_equal "/path/of/some/uri", @request.path + @request.relative_url_root = nil @request.set_REQUEST_URI "/" assert_equal "/", @request.request_uri assert_equal "/", @request.path + @request.relative_url_root = nil @request.set_REQUEST_URI "/?m=b" assert_equal "/?m=b", @request.request_uri assert_equal "/", @request.path + @request.relative_url_root = nil @request.set_REQUEST_URI "/" @request.env['SCRIPT_NAME'] = "/dispatch.cgi" assert_equal "/", @request.request_uri assert_equal "/", @request.path + @request.relative_url_root = nil @request.set_REQUEST_URI "/hieraki/" @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi" assert_equal "/hieraki/", @request.request_uri assert_equal "/", @request.path + @request.relative_url_root = nil @request.set_REQUEST_URI "/collaboration/hieraki/books/edit/2" @request.env['SCRIPT_NAME'] = "/collaboration/hieraki/dispatch.cgi" assert_equal "/collaboration/hieraki/books/edit/2", @request.request_uri assert_equal "/books/edit/2", @request.path # The following tests are for when REQUEST_URI is not supplied (as in IIS) + @request.relative_url_root = nil @request.set_REQUEST_URI nil @request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1" @request.env['SCRIPT_NAME'] = nil #"/path/dispatch.rb" assert_equal "/path/of/some/uri?mapped=1", @request.request_uri assert_equal "/path/of/some/uri", @request.path + @request.relative_url_root = nil @request.env['PATH_INFO'] = "/path/of/some/uri?mapped=1" @request.env['SCRIPT_NAME'] = "/path/dispatch.rb" assert_equal "/path/of/some/uri?mapped=1", @request.request_uri assert_equal "/of/some/uri", @request.path + @request.relative_url_root = nil @request.env['PATH_INFO'] = "/path/of/some/uri" @request.env['SCRIPT_NAME'] = nil assert_equal "/path/of/some/uri", @request.request_uri assert_equal "/path/of/some/uri", @request.path + @request.relative_url_root = nil @request.env['PATH_INFO'] = "/" assert_equal "/", @request.request_uri assert_equal "/", @request.path + @request.relative_url_root = nil @request.env['PATH_INFO'] = "/?m=b" assert_equal "/?m=b", @request.request_uri assert_equal "/", @request.path + @request.relative_url_root = nil @request.env['PATH_INFO'] = "/" @request.env['SCRIPT_NAME'] = "/dispatch.cgi" assert_equal "/", @request.request_uri assert_equal "/", @request.path + @request.relative_url_root = nil @request.env['PATH_INFO'] = "/hieraki/" @request.env['SCRIPT_NAME'] = "/hieraki/dispatch.cgi" assert_equal "/hieraki/", @request.request_uri assert_equal "/", @request.path # This test ensures that Rails uses REQUEST_URI over PATH_INFO + @request.relative_url_root = nil @request.env['REQUEST_URI'] = "/some/path" @request.env['PATH_INFO'] = "/another/path" @request.env['SCRIPT_NAME'] = "/dispatch.cgi" assert_equal "/some/path", @request.request_uri assert_equal "/some/path", @request.path - - end diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index cedf17c61b..453afa33e2 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -1,5 +1,4 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_options_helper' +require File.dirname(__FILE__) + '/../abstract_unit' class MockTimeZone attr_reader :name @@ -260,7 +259,7 @@ class FormOptionsHelperTest < Test::Unit::TestCase assert_equal( "", country_select("post", "origin") ) -- cgit v1.2.3