diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-04-23 01:09:27 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-04-23 01:09:27 +0100 |
commit | 5a1fe9039b33414b7088813712e03bbe38567cff (patch) | |
tree | 10e3a15c06494dd09c0324dca894ddcbb68ce582 /actionpack | |
parent | 3384f073e35c5391deaeb209a62a485a0c4dd7b1 (diff) | |
parent | 7f6779c1d5e4ec7f642839caa7e86320720f77c8 (diff) | |
download | rails-5a1fe9039b33414b7088813712e03bbe38567cff.tar.gz rails-5a1fe9039b33414b7088813712e03bbe38567cff.tar.bz2 rails-5a1fe9039b33414b7088813712e03bbe38567cff.zip |
Merge commit 'mainstream/master'
Diffstat (limited to 'actionpack')
24 files changed, 571 insertions, 362 deletions
diff --git a/actionpack/lib/action_controller.rb b/actionpack/lib/action_controller.rb index 100a0be1db..aee6b9dc9f 100644 --- a/actionpack/lib/action_controller.rb +++ b/actionpack/lib/action_controller.rb @@ -30,6 +30,7 @@ rescue LoadError require 'active_support' end end +require 'active_support/core/all' require File.join(File.dirname(__FILE__), "action_pack") diff --git a/actionpack/lib/action_controller/abstract/renderer.rb b/actionpack/lib/action_controller/abstract/renderer.rb index 68c3b07b84..a86eef889e 100644 --- a/actionpack/lib/action_controller/abstract/renderer.rb +++ b/actionpack/lib/action_controller/abstract/renderer.rb @@ -17,28 +17,50 @@ module AbstractController end def render(options = {}) - self.response_body = render_to_string(options) + self.response_body = render_to_body(options) end - # Raw rendering of a template. + # Raw rendering of a template to a Rack-compatible body. # ==== # @option _prefix<String> The template's path prefix # @option _layout<String> The relative path to the layout template to use # # :api: plugin - def render_to_string(options = {}) + def render_to_body(options = {}) name = options[:_template_name] || action_name template = options[:_template] || view_paths.find_by_parts(name.to_s, formats, options[:_prefix]) _render_template(template, options) end + # Raw rendering of a template to a string. + # ==== + # @option _prefix<String> The template's path prefix + # @option _layout<String> The relative path to the layout template to use + # + # :api: plugin + def render_to_string(options = {}) + AbstractController::Renderer.body_to_s(render_to_body(options)) + end + def _render_template(template, options) _action_view._render_template_with_layout(template) end def view_paths() _view_paths end + # Return a string representation of a Rack-compatible response body. + def self.body_to_s(body) + if body.respond_to?(:to_str) + body + else + strings = [] + body.each { |part| strings << part.to_s } + body.close if body.respond_to?(:close) + strings.join + end + end + module ClassMethods def append_view_path(path) @@ -55,4 +77,4 @@ module AbstractController end end end -end
\ No newline at end of file +end diff --git a/actionpack/lib/action_controller/base/render.rb b/actionpack/lib/action_controller/base/render.rb index 0d24f18633..606df58518 100644 --- a/actionpack/lib/action_controller/base/render.rb +++ b/actionpack/lib/action_controller/base/render.rb @@ -1,3 +1,5 @@ +require 'action_controller/abstract/renderer' + module ActionController DEFAULT_RENDER_STATUS_CODE = "200 OK" @@ -306,9 +308,9 @@ module ActionController (name.is_a?(String) ? name.sub(/^#{controller_path}\//, '') : name).to_s end - # Renders according to the same rules as <tt>render</tt>, but returns the result in a string instead - # of sending it as the response body to the browser. - def render_to_string(options = nil, &block) #:doc: + # Same rules as <tt>render</tt>, but returns a Rack-compatible body + # instead of sending the response. + def render_to_body(options = nil, &block) #:doc: render(options, &block) response.body ensure @@ -316,7 +318,11 @@ module ActionController erase_render_results reset_variables_added_to_assigns end - + + def render_to_string(options = {}) + AbstractController::Renderer.body_to_s(render_to_body(options)) + end + # Clears the rendered results, allowing for another render to be performed. def erase_render_results #:nodoc: response.body = [] @@ -387,4 +393,4 @@ module ActionController render_for_parts(parts, layout, options) end end -end
\ No newline at end of file +end diff --git a/actionpack/lib/action_controller/base/responder.rb b/actionpack/lib/action_controller/base/responder.rb index 989f82444b..1aee980da6 100644 --- a/actionpack/lib/action_controller/base/responder.rb +++ b/actionpack/lib/action_controller/base/responder.rb @@ -5,18 +5,19 @@ module ActionController end private - def render_for_text(text = nil, append_response = false) #:nodoc: + def render_for_text(text) #:nodoc: @performed_render = true - if append_response - response.body ||= '' - response.body << text.to_s - else - response.body = case text - when Proc then text - when nil then " " # Safari doesn't pass the headers of the return if the response is zero length - else text.to_s + case text + when Proc + response.body = text + when nil + # Safari 2 doesn't pass response headers if the response is zero-length + if response.body_parts.empty? + response.body_parts << ' ' end + else + response.body_parts << text end end @@ -39,4 +40,4 @@ module ActionController end end end -end
\ No newline at end of file +end diff --git a/actionpack/lib/action_controller/new_base/base.rb b/actionpack/lib/action_controller/new_base/base.rb index 0400ddbf7a..08e7a1a0e7 100644 --- a/actionpack/lib/action_controller/new_base/base.rb +++ b/actionpack/lib/action_controller/new_base/base.rb @@ -42,7 +42,6 @@ module ActionController # :api: plugin def response_body=(body) - @_response["Content-Length"] = body.length @_response.body = body end @@ -58,4 +57,4 @@ module ActionController response.to_a end end -end
\ No newline at end of file +end diff --git a/actionpack/lib/action_controller/new_base/layouts.rb b/actionpack/lib/action_controller/new_base/layouts.rb index 149847c968..a8e0809ac6 100644 --- a/actionpack/lib/action_controller/new_base/layouts.rb +++ b/actionpack/lib/action_controller/new_base/layouts.rb @@ -9,7 +9,7 @@ module ActionController end end - def render_to_string(options) + def render_to_body(options) # render :text => ..., :layout => ... # or # render :anything_else @@ -34,4 +34,4 @@ module ActionController end end -end
\ No newline at end of file +end diff --git a/actionpack/lib/action_controller/new_base/renderer.rb b/actionpack/lib/action_controller/new_base/renderer.rb index 044c15f409..ed34c46aed 100644 --- a/actionpack/lib/action_controller/new_base/renderer.rb +++ b/actionpack/lib/action_controller/new_base/renderer.rb @@ -8,7 +8,7 @@ module ActionController end def render(action, options = {}) - # TODO: Move this into #render_to_string + # TODO: Move this into #render_to_body if action.is_a?(Hash) options, action = action, nil else @@ -17,10 +17,10 @@ module ActionController _process_options(options) - self.response_body = render_to_string(options) + self.response_body = render_to_body(options) end - def render_to_string(options) + def render_to_body(options) unless options.is_a?(Hash) options = {:action => options} end @@ -59,4 +59,4 @@ module ActionController end end end -end
\ No newline at end of file +end diff --git a/actionpack/lib/action_controller/testing/process.rb b/actionpack/lib/action_controller/testing/process.rb index 86e193efa9..7e2857614c 100644 --- a/actionpack/lib/action_controller/testing/process.rb +++ b/actionpack/lib/action_controller/testing/process.rb @@ -287,6 +287,7 @@ module ActionController #:nodoc: include TestResponseBehavior def recycle! + body_parts.clear headers.delete('ETag') headers.delete('Last-Modified') end diff --git a/actionpack/lib/action_dispatch.rb b/actionpack/lib/action_dispatch.rb index bd5a38cc82..5feb8a4863 100644 --- a/actionpack/lib/action_dispatch.rb +++ b/actionpack/lib/action_dispatch.rb @@ -30,6 +30,7 @@ rescue LoadError require 'active_support' end end +require 'active_support/core/all' $:.unshift "#{File.dirname(__FILE__)}/action_dispatch/vendor/rack-1.0" begin diff --git a/actionpack/lib/action_dispatch/middleware/params_parser.rb b/actionpack/lib/action_dispatch/middleware/params_parser.rb index 6df572268c..abaee2829e 100644 --- a/actionpack/lib/action_dispatch/middleware/params_parser.rb +++ b/actionpack/lib/action_dispatch/middleware/params_parser.rb @@ -1,3 +1,5 @@ +require 'active_support/json' + module ActionDispatch class ParamsParser ActionController::Base.param_parsers[Mime::XML] = :xml_simple diff --git a/actionpack/lib/action_dispatch/middleware/rewindable_input.rb b/actionpack/lib/action_dispatch/middleware/rewindable_input.rb index 725414efc4..c818f28cce 100644 --- a/actionpack/lib/action_dispatch/middleware/rewindable_input.rb +++ b/actionpack/lib/action_dispatch/middleware/rewindable_input.rb @@ -1,27 +1,18 @@ module ActionDispatch class RewindableInput - class RewindableIO < ActiveSupport::BasicObject - def initialize(io) - @io = io - @rewindable = io.is_a?(::StringIO) - end - - def method_missing(method, *args, &block) - unless @rewindable - @io = ::StringIO.new(@io.read) - @rewindable = true - end - - @io.__send__(method, *args, &block) - end - end - def initialize(app) @app = app end def call(env) - env['rack.input'] = RewindableIO.new(env['rack.input']) + begin + env['rack.input'].rewind + rescue NoMethodError, Errno::ESPIPE + # Handles exceptions raised by input streams that cannot be rewound + # such as when using plain CGI under Apache + env['rack.input'] = StringIO.new(env['rack.input'].read) + end + @app.call(env) end end diff --git a/actionpack/lib/action_view.rb b/actionpack/lib/action_view.rb index e604c2a581..dd352f9ce0 100644 --- a/actionpack/lib/action_view.rb +++ b/actionpack/lib/action_view.rb @@ -30,6 +30,7 @@ rescue LoadError require 'active_support' end end +require 'active_support/core/all' require File.join(File.dirname(__FILE__), "action_pack") diff --git a/actionpack/lib/action_view/template/template.rb b/actionpack/lib/action_view/template/template.rb index 0d2f201458..b6967a2013 100644 --- a/actionpack/lib/action_view/template/template.rb +++ b/actionpack/lib/action_view/template/template.rb @@ -1,3 +1,4 @@ +require 'set' require "action_view/template/path" module ActionView #:nodoc: diff --git a/actionpack/test/abstract_controller/abstract_controller_test.rb b/actionpack/test/abstract_controller/abstract_controller_test.rb index 6d33888821..918062c7db 100644 --- a/actionpack/test/abstract_controller/abstract_controller_test.rb +++ b/actionpack/test/abstract_controller/abstract_controller_test.rb @@ -56,6 +56,14 @@ module AbstractController def naked_render render end + + def rendering_to_body + render_to_body "naked_render.erb" + end + + def rendering_to_string + render_to_string "naked_render.erb" + end end class TestRenderer < ActiveSupport::TestCase @@ -73,6 +81,16 @@ module AbstractController result = Me2.process(:naked_render) assert_equal "Hello from naked_render.erb", result.response_obj[:body] end + + test "rendering to a rack body" do + result = Me2.process(:rendering_to_body) + assert_equal "Hello from naked_render.erb", result.response_obj[:body] + end + + test "rendering to a string" do + result = Me2.process(:rendering_to_string) + assert_equal "Hello from naked_render.erb", result.response_obj[:body] + end end # Test rendering with prefixes @@ -134,7 +152,7 @@ module AbstractController self.class.layout(formats) end - def render_to_string(options = {}) + def render_to_body(options = {}) options[:_layout] = options[:layout] || _layout super end @@ -223,4 +241,4 @@ module AbstractController end end -end
\ No newline at end of file +end diff --git a/actionpack/test/abstract_controller/layouts_test.rb b/actionpack/test/abstract_controller/layouts_test.rb index 5dd68ec28e..3d4570bfef 100644 --- a/actionpack/test/abstract_controller/layouts_test.rb +++ b/actionpack/test/abstract_controller/layouts_test.rb @@ -23,7 +23,7 @@ module AbstractControllerTests def controller_path() self.class.controller_path end - def render_to_string(options) + def render_to_body(options) options[:_layout] = _default_layout super end @@ -229,4 +229,4 @@ module AbstractControllerTests end end end -end
\ No newline at end of file +end diff --git a/actionpack/test/activerecord/polymorphic_routes_test.rb b/actionpack/test/activerecord/polymorphic_routes_test.rb new file mode 100644 index 0000000000..35139fbb7f --- /dev/null +++ b/actionpack/test/activerecord/polymorphic_routes_test.rb @@ -0,0 +1,394 @@ +require 'active_record_unit' + +class Task < ActiveRecord::Base + set_table_name 'projects' +end + +class Step < ActiveRecord::Base + set_table_name 'projects' +end + +class Bid < ActiveRecord::Base + set_table_name 'projects' +end + +class Tax < ActiveRecord::Base + set_table_name 'projects' +end + +class Fax < ActiveRecord::Base + set_table_name 'projects' +end + +class Series < ActiveRecord::Base + set_table_name 'projects' +end + +class PolymorphicRoutesTest < ActionController::TestCase + include ActionController::UrlWriter + self.default_url_options[:host] = 'example.com' + + def setup + @project = Project.new + @task = Task.new + @step = Step.new + @bid = Bid.new + @tax = Tax.new + @fax = Fax.new + @series = Series.new + end + + def test_with_record + with_test_routes do + @project.save + assert_equal "http://example.com/projects/#{@project.id}", polymorphic_url(@project) + end + end + + def test_with_new_record + with_test_routes do + assert_equal "http://example.com/projects", polymorphic_url(@project) + end + end + + def test_with_record_and_action + with_test_routes do + assert_equal "http://example.com/projects/new", polymorphic_url(@project, :action => 'new') + end + end + + def test_url_helper_prefixed_with_new + with_test_routes do + assert_equal "http://example.com/projects/new", new_polymorphic_url(@project) + end + end + + def test_url_helper_prefixed_with_edit + with_test_routes do + @project.save + assert_equal "http://example.com/projects/#{@project.id}/edit", edit_polymorphic_url(@project) + end + end + + def test_url_helper_prefixed_with_edit_with_url_options + with_test_routes do + @project.save + assert_equal "http://example.com/projects/#{@project.id}/edit?param1=10", edit_polymorphic_url(@project, :param1 => '10') + end + end + + def test_url_helper_with_url_options + with_test_routes do + @project.save + assert_equal "http://example.com/projects/#{@project.id}?param1=10", polymorphic_url(@project, :param1 => '10') + end + end + + def test_formatted_url_helper_is_deprecated + with_test_routes do + assert_deprecated do + formatted_polymorphic_url([@project, :pdf]) + end + end + end + + def test_format_option + with_test_routes do + @project.save + assert_equal "http://example.com/projects/#{@project.id}.pdf", polymorphic_url(@project, :format => :pdf) + end + end + + def test_format_option_with_url_options + with_test_routes do + @project.save + assert_equal "http://example.com/projects/#{@project.id}.pdf?param1=10", polymorphic_url(@project, :format => :pdf, :param1 => '10') + end + end + + def test_id_and_format_option + with_test_routes do + @project.save + assert_equal "http://example.com/projects/#{@project.id}.pdf", polymorphic_url(:id => @project, :format => :pdf) + end + end + + def test_with_nested + with_test_routes do + @project.save + @task.save + assert_equal "http://example.com/projects/#{@project.id}/tasks/#{@task.id}", polymorphic_url([@project, @task]) + end + end + + def test_with_nested_unsaved + with_test_routes do + @project.save + assert_equal "http://example.com/projects/#{@project.id}/tasks", polymorphic_url([@project, @task]) + end + end + + def test_new_with_array_and_namespace + with_admin_test_routes do + assert_equal "http://example.com/admin/projects/new", polymorphic_url([:admin, @project], :action => 'new') + end + end + + def test_unsaved_with_array_and_namespace + with_admin_test_routes do + assert_equal "http://example.com/admin/projects", polymorphic_url([:admin, @project]) + end + end + + def test_nested_unsaved_with_array_and_namespace + with_admin_test_routes do + @project.save + assert_equal "http://example.com/admin/projects/#{@project.id}/tasks", polymorphic_url([:admin, @project, @task]) + end + end + + def test_nested_with_array_and_namespace + with_admin_test_routes do + @project.save + @task.save + assert_equal "http://example.com/admin/projects/#{@project.id}/tasks/#{@task.id}", polymorphic_url([:admin, @project, @task]) + end + end + + def test_ordering_of_nesting_and_namespace + with_admin_and_site_test_routes do + @project.save + @task.save + @step.save + assert_equal "http://example.com/admin/projects/#{@project.id}/site/tasks/#{@task.id}/steps/#{@step.id}", polymorphic_url([:admin, @project, :site, @task, @step]) + end + end + + def test_nesting_with_array_ending_in_singleton_resource + with_test_routes do + @project.save + assert_equal "http://example.com/projects/#{@project.id}/bid", polymorphic_url([@project, :bid]) + end + end + + def test_nesting_with_array_containing_singleton_resource + with_test_routes do + @project.save + @task.save + assert_equal "http://example.com/projects/#{@project.id}/bid/tasks/#{@task.id}", polymorphic_url([@project, :bid, @task]) + end + end + + def test_nesting_with_array_containing_singleton_resource_and_format + with_test_routes do + @project.save + @task.save + assert_equal "http://example.com/projects/#{@project.id}/bid/tasks/#{@task.id}.pdf", polymorphic_url([@project, :bid, @task], :format => :pdf) + end + end + + def test_nesting_with_array_containing_namespace_and_singleton_resource + with_admin_test_routes do + @project.save + @task.save + assert_equal "http://example.com/admin/projects/#{@project.id}/bid/tasks/#{@task.id}", polymorphic_url([:admin, @project, :bid, @task]) + end + end + + def test_nesting_with_array_containing_nil + with_test_routes do + @project.save + assert_equal "http://example.com/projects/#{@project.id}/bid", polymorphic_url([@project, nil, :bid]) + end + end + + def test_with_array_containing_single_object + with_test_routes do + @project.save + assert_equal "http://example.com/projects/#{@project.id}", polymorphic_url([nil, @project]) + end + end + + def test_with_array_containing_single_name + with_test_routes do + @project.save + assert_equal "http://example.com/projects", polymorphic_url([:projects]) + end + end + + def test_with_hash + with_test_routes do + @project.save + assert_equal "http://example.com/projects/#{@project.id}", polymorphic_url(:id => @project) + end + end + + def test_polymorphic_path_accepts_options + with_test_routes do + assert_equal "/projects/new", polymorphic_path(@project, :action => 'new') + end + end + + def test_polymorphic_path_does_not_modify_arguments + with_admin_test_routes do + @project.save + @task.save + object_array = [:admin, @project, @task] + assert_no_difference 'object_array.size' do + polymorphic_url(object_array) + end + end + end + + # Tests for names where .plural.singular doesn't round-trip + def test_with_irregular_plural_record + with_test_routes do + @tax.save + assert_equal "http://example.com/taxes/#{@tax.id}", polymorphic_url(@tax) + end + end + + def test_with_irregular_plural_new_record + with_test_routes do + assert_equal "http://example.com/taxes", polymorphic_url(@tax) + end + end + + def test_with_irregular_plural_record_and_action + with_test_routes do + assert_equal "http://example.com/taxes/new", polymorphic_url(@tax, :action => 'new') + end + end + + def test_irregular_plural_url_helper_prefixed_with_new + with_test_routes do + assert_equal "http://example.com/taxes/new", new_polymorphic_url(@tax) + end + end + + def test_irregular_plural_url_helper_prefixed_with_edit + with_test_routes do + @tax.save + assert_equal "http://example.com/taxes/#{@tax.id}/edit", edit_polymorphic_url(@tax) + end + end + + def test_with_nested_irregular_plurals + with_test_routes do + @tax.save + @fax.save + assert_equal "http://example.com/taxes/#{@tax.id}/faxes/#{@fax.id}", polymorphic_url([@tax, @fax]) + end + end + + def test_with_nested_unsaved_irregular_plurals + with_test_routes do + @tax.save + assert_equal "http://example.com/taxes/#{@tax.id}/faxes", polymorphic_url([@tax, @fax]) + end + end + + def test_new_with_irregular_plural_array_and_namespace + with_admin_test_routes do + assert_equal "http://example.com/admin/taxes/new", polymorphic_url([:admin, @tax], :action => 'new') + end + end + + def test_unsaved_with_irregular_plural_array_and_namespace + with_admin_test_routes do + assert_equal "http://example.com/admin/taxes", polymorphic_url([:admin, @tax]) + end + end + + def test_nesting_with_irregular_plurals_and_array_ending_in_singleton_resource + with_test_routes do + @tax.save + assert_equal "http://example.com/taxes/#{@tax.id}/bid", polymorphic_url([@tax, :bid]) + end + end + + def test_with_array_containing_single_irregular_plural_object + with_test_routes do + @tax.save + assert_equal "http://example.com/taxes/#{@tax.id}", polymorphic_url([nil, @tax]) + end + end + + def test_with_array_containing_single_name_irregular_plural + with_test_routes do + @tax.save + assert_equal "http://example.com/taxes", polymorphic_url([:taxes]) + end + end + + # Tests for uncountable names + def test_uncountable_resource + with_test_routes do + @series.save + assert_equal "http://example.com/series/#{@series.id}", polymorphic_url(@series) + end + end + + def with_test_routes(options = {}) + with_routing do |set| + set.draw do |map| + map.resources :projects do |projects| + projects.resources :tasks + projects.resource :bid do |bid| + bid.resources :tasks + end + end + map.resources :taxes do |taxes| + taxes.resources :faxes + taxes.resource :bid + end + map.resources :series + end + + ActionController::Routing::Routes.install_helpers(self.class) + yield + end + end + + def with_admin_test_routes(options = {}) + with_routing do |set| + set.draw do |map| + map.namespace :admin do |admin| + admin.resources :projects do |projects| + projects.resources :tasks + projects.resource :bid do |bid| + bid.resources :tasks + end + end + admin.resources :taxes do |taxes| + taxes.resources :faxes + end + admin.resources :series + end + end + + ActionController::Routing::Routes.install_helpers(self.class) + yield + end + end + + def with_admin_and_site_test_routes(options = {}) + with_routing do |set| + set.draw do |map| + map.namespace :admin do |admin| + admin.resources :projects do |projects| + projects.namespace :site do |site| + site.resources :tasks do |tasks| + tasks.resources :steps + end + end + end + end + end + + ActionController::Routing::Routes.install_helpers(self.class) + yield + end + end + +end
\ No newline at end of file diff --git a/actionpack/test/controller/dispatcher_test.rb b/actionpack/test/controller/dispatcher_test.rb index 721bcf6136..569d698a03 100644 --- a/actionpack/test/controller/dispatcher_test.rb +++ b/actionpack/test/controller/dispatcher_test.rb @@ -94,7 +94,7 @@ class DispatcherTest < Test::Unit::TestCase def dispatch(cache_classes = true) ActionController::Routing::RouteSet.any_instance.stubs(:call).returns([200, {}, 'response']) Dispatcher.define_dispatcher_callbacks(cache_classes) - Dispatcher.new.call({}) + Dispatcher.new.call({'rack.input' => StringIO.new('')}) end def assert_subclasses(howmany, klass, message = klass.subclasses.inspect) diff --git a/actionpack/test/controller/polymorphic_routes_test.rb b/actionpack/test/controller/polymorphic_routes_test.rb deleted file mode 100644 index 146d703619..0000000000 --- a/actionpack/test/controller/polymorphic_routes_test.rb +++ /dev/null @@ -1,293 +0,0 @@ -require 'abstract_unit' - -class Article - attr_reader :id - def save; @id = 1 end - def new_record?; @id.nil? end - def name - model = self.class.name.downcase - @id.nil? ? "new #{model}" : "#{model} ##{@id}" - end -end - -class Response < Article - def post_id; 1 end -end - -class Tag < Article - def response_id; 1 end -end - -class Tax - attr_reader :id - def save; @id = 1 end - def new_record?; @id.nil? end - def name - model = self.class.name.downcase - @id.nil? ? "new #{model}" : "#{model} ##{@id}" - end -end - -class Fax < Tax - def store_id; 1 end -end - -# TODO: test nested models -class Response::Nested < Response; end - -class PolymorphicRoutesTest < ActiveSupport::TestCase - include ActionController::PolymorphicRoutes - - def setup - @article = Article.new - @response = Response.new - @tax = Tax.new - @fax = Fax.new - end - - def test_with_record - @article.save - expects(:article_url).with(@article) - polymorphic_url(@article) - end - - def test_with_new_record - expects(:articles_url).with() - @article.expects(:new_record?).returns(true) - polymorphic_url(@article) - end - - def test_with_record_and_action - expects(:new_article_url).with() - @article.expects(:new_record?).never - polymorphic_url(@article, :action => 'new') - end - - def test_url_helper_prefixed_with_new - expects(:new_article_url).with() - new_polymorphic_url(@article) - end - - def test_url_helper_prefixed_with_edit - @article.save - expects(:edit_article_url).with(@article) - edit_polymorphic_url(@article) - end - - def test_url_helper_prefixed_with_edit_with_url_options - @article.save - expects(:edit_article_url).with(@article, :param1 => '10') - edit_polymorphic_url(@article, :param1 => '10') - end - - def test_url_helper_with_url_options - @article.save - expects(:article_url).with(@article, :param1 => '10') - polymorphic_url(@article, :param1 => '10') - end - - def test_formatted_url_helper_is_deprecated - expects(:articles_url).with(:format => :pdf) - assert_deprecated do - formatted_polymorphic_url([@article, :pdf]) - end - end - - def test_format_option - @article.save - expects(:article_url).with(@article, :format => :pdf) - polymorphic_url(@article, :format => :pdf) - end - - def test_format_option_with_url_options - @article.save - expects(:article_url).with(@article, :format => :pdf, :param1 => '10') - polymorphic_url(@article, :format => :pdf, :param1 => '10') - end - - def test_id_and_format_option - @article.save - expects(:article_url).with(:id => @article, :format => :pdf) - polymorphic_url(:id => @article, :format => :pdf) - end - - def test_with_nested - @response.save - expects(:article_response_url).with(@article, @response) - polymorphic_url([@article, @response]) - end - - def test_with_nested_unsaved - expects(:article_responses_url).with(@article) - polymorphic_url([@article, @response]) - end - - def test_new_with_array_and_namespace - expects(:new_admin_article_url).with() - polymorphic_url([:admin, @article], :action => 'new') - end - - def test_unsaved_with_array_and_namespace - expects(:admin_articles_url).with() - polymorphic_url([:admin, @article]) - end - - def test_nested_unsaved_with_array_and_namespace - @article.save - expects(:admin_article_url).with(@article) - polymorphic_url([:admin, @article]) - expects(:admin_article_responses_url).with(@article) - polymorphic_url([:admin, @article, @response]) - end - - def test_nested_with_array_and_namespace - @response.save - expects(:admin_article_response_url).with(@article, @response) - polymorphic_url([:admin, @article, @response]) - - # a ridiculously long named route tests correct ordering of namespaces and nesting: - @tag = Tag.new - @tag.save - expects(:site_admin_article_response_tag_url).with(@article, @response, @tag) - polymorphic_url([:site, :admin, @article, @response, @tag]) - end - - def test_nesting_with_array_ending_in_singleton_resource - expects(:article_response_url).with(@article) - polymorphic_url([@article, :response]) - end - - def test_nesting_with_array_containing_singleton_resource - @tag = Tag.new - @tag.save - expects(:article_response_tag_url).with(@article, @tag) - polymorphic_url([@article, :response, @tag]) - end - - def test_nesting_with_array_containing_namespace_and_singleton_resource - @tag = Tag.new - @tag.save - expects(:admin_article_response_tag_url).with(@article, @tag) - polymorphic_url([:admin, @article, :response, @tag]) - end - - def test_nesting_with_array_containing_singleton_resource_and_format - @tag = Tag.new - @tag.save - expects(:article_response_tag_url).with(@article, @tag, :format => :pdf) - polymorphic_url([@article, :response, @tag], :format => :pdf) - end - - def test_nesting_with_array_containing_singleton_resource_and_format_option - @tag = Tag.new - @tag.save - expects(:article_response_tag_url).with(@article, @tag, :format => :pdf) - polymorphic_url([@article, :response, @tag], :format => :pdf) - end - - def test_nesting_with_array_containing_nil - expects(:article_response_url).with(@article) - polymorphic_url([@article, nil, :response]) - end - - def test_with_array_containing_single_object - @article.save - expects(:article_url).with(@article) - polymorphic_url([nil, @article]) - end - - def test_with_array_containing_single_name - @article.save - expects(:articles_url) - polymorphic_url([:articles]) - end - - # TODO: Needs to be updated to correctly know about whether the object is in a hash or not - def xtest_with_hash - expects(:article_url).with(@article) - @article.save - polymorphic_url(:id => @article) - end - - def test_polymorphic_path_accepts_options - expects(:new_article_path).with() - polymorphic_path(@article, :action => :new) - end - - def test_polymorphic_path_does_not_modify_arguments - expects(:admin_article_responses_url).with(@article) - path = [:admin, @article, @response] - assert_no_difference 'path.size' do - polymorphic_url(path) - end - end - - # Tests for names where .plural.singular doesn't round-trip - def test_with_irregular_plural_record - @tax.save - expects(:taxis_url).with(@tax) - polymorphic_url(@tax) - end - - def test_with_irregular_plural_new_record - expects(:taxes_url).with() - @tax.expects(:new_record?).returns(true) - polymorphic_url(@tax) - end - - def test_with_irregular_plural_record_and_action - expects(:new_taxis_url).with() - @tax.expects(:new_record?).never - polymorphic_url(@tax, :action => 'new') - end - - def test_irregular_plural_url_helper_prefixed_with_new - expects(:new_taxis_url).with() - new_polymorphic_url(@tax) - end - - def test_irregular_plural_url_helper_prefixed_with_edit - @tax.save - expects(:edit_taxis_url).with(@tax) - edit_polymorphic_url(@tax) - end - - def test_with_nested_irregular_plurals - @fax.save - expects(:taxis_faxis_url).with(@tax, @fax) - polymorphic_url([@tax, @fax]) - end - - def test_with_nested_unsaved_irregular_plurals - expects(:taxis_faxes_url).with(@tax) - polymorphic_url([@tax, @fax]) - end - - def test_new_with_irregular_plural_array_and_namespace - expects(:new_admin_taxis_url).with() - polymorphic_url([:admin, @tax], :action => 'new') - end - - def test_unsaved_with_irregular_plural_array_and_namespace - expects(:admin_taxes_url).with() - polymorphic_url([:admin, @tax]) - end - - def test_nesting_with_irregular_plurals_and_array_ending_in_singleton_resource - expects(:taxis_faxis_url).with(@tax) - polymorphic_url([@tax, :faxis]) - end - - def test_with_array_containing_single_irregular_plural_object - @tax.save - expects(:taxis_url).with(@tax) - polymorphic_url([nil, @tax]) - end - - def test_with_array_containing_single_name_irregular_plural - @tax.save - expects(:taxes_url) - polymorphic_url([:taxes]) - end - -end diff --git a/actionpack/test/controller/rescue_test.rb b/actionpack/test/controller/rescue_test.rb index 741b01caa8..894420a910 100644 --- a/actionpack/test/controller/rescue_test.rb +++ b/actionpack/test/controller/rescue_test.rb @@ -400,22 +400,6 @@ class RescueControllerTest < ActionController::TestCase assert_equal "RescueController::ResourceUnavailableToRescueAsString", @response.body end - def test_rescue_dispatcher_exceptions - env = @request.env - env["action_controller.rescue.request"] = @request - env["action_controller.rescue.response"] = @response - - RescueController.call_with_exception(env, ActionController::RoutingError.new("Route not found")) - assert_equal "no way", @response.body - end - - def test_rescue_dispatcher_exceptions_without_request_set - @request.env['REQUEST_URI'] = '/no_way' - response = RescueController.call_with_exception(@request.env, ActionController::RoutingError.new("Route not found")) - assert_kind_of ActionDispatch::Response, response - assert_equal "no way", response.body - end - protected def with_all_requests_local(local = true) old_local, ActionController::Base.consider_all_requests_local = @@ -537,3 +521,83 @@ class ControllerInheritanceRescueControllerTest < ActionController::TestCase assert_response :created end end + +class ApplicationController < ActionController::Base + rescue_from ActionController::RoutingError do + render :text => 'no way' + end +end + +class RescueTest < ActionController::IntegrationTest + class TestController < ActionController::Base + class RecordInvalid < StandardError + def message + 'invalid' + end + end + rescue_from RecordInvalid, :with => :show_errors + + def foo + render :text => "foo" + end + + def invalid + raise RecordInvalid + end + + def b00m + raise 'b00m' + end + + protected + def show_errors(exception) + render :text => exception.message + end + end + + test 'normal request' do + with_test_routing do + get '/foo' + assert_equal 'foo', response.body + end + end + + test 'rescue exceptions inside controller' do + with_test_routing do + get '/invalid' + assert_equal 'invalid', response.body + end + end + + test 'rescue routing exceptions' do + assert_equal 1, ApplicationController.rescue_handlers.length + + begin + with_test_routing do + get '/no_way' + assert_equal 'no way', response.body + end + ensure + ActionController::Base.rescue_handlers.clear + end + end + + test 'unrescued exception' do + with_test_routing do + get '/b00m' + assert_match(/Action Controller: Exception caught/, response.body) + end + end + + private + def with_test_routing + with_routing do |set| + set.draw do |map| + map.connect 'foo', :controller => "rescue_test/test", :action => 'foo' + map.connect 'invalid', :controller => "rescue_test/test", :action => 'invalid' + map.connect 'b00m', :controller => "rescue_test/test", :action => 'b00m' + end + yield + end + end +end diff --git a/actionpack/test/dispatch/request/multipart_params_parsing_test.rb b/actionpack/test/dispatch/request/multipart_params_parsing_test.rb index 2f409f020d..88b81dc493 100644 --- a/actionpack/test/dispatch/request/multipart_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/multipart_params_parsing_test.rb @@ -206,8 +206,8 @@ class MultipartParamsParsingTest < ActionController::IntegrationTest end def call(env) - req = Rack::Request.new(env) - req.params # Parse params + env['rack.input'].read + env['rack.input'].rewind @app.call(env) end end diff --git a/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb b/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb index 51a660f614..8de4a83d76 100644 --- a/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb @@ -150,8 +150,8 @@ class UrlEncodedParamsParsingTest < ActionController::IntegrationTest end def call(env) - req = Rack::Request.new(env) - req.params # Parse params + env['rack.input'].read + env['rack.input'].rewind @app.call(env) end end diff --git a/actionpack/test/controller/session/cookie_store_test.rb b/actionpack/test/dispatch/session/cookie_store_test.rb index b9bf8cf411..b9bf8cf411 100644 --- a/actionpack/test/controller/session/cookie_store_test.rb +++ b/actionpack/test/dispatch/session/cookie_store_test.rb diff --git a/actionpack/test/controller/session/mem_cache_store_test.rb b/actionpack/test/dispatch/session/mem_cache_store_test.rb index 7561c93e4a..7561c93e4a 100644 --- a/actionpack/test/controller/session/mem_cache_store_test.rb +++ b/actionpack/test/dispatch/session/mem_cache_store_test.rb diff --git a/actionpack/test/controller/session/test_session_test.rb b/actionpack/test/dispatch/session/test_session_test.rb index de6539e1cc..de6539e1cc 100644 --- a/actionpack/test/controller/session/test_session_test.rb +++ b/actionpack/test/dispatch/session/test_session_test.rb |