diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index 9de3fab..c310f9c 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -266,15 +266,26 @@ module ActionController #:nodoc: # Returns binary content (downloadable file), converted to a String def binary_content - raise "Response body is not a Proc: #{body.inspect}" unless body.kind_of?(Proc) + raise "Response body is not a Proc: #{body.inspect}" unless @body.kind_of?(Proc) require 'stringio' sio = StringIO.new - body.call(self, sio) + @body.call(self, sio) sio.rewind sio.read end + + def body(call_proc = true) + if call_proc && @body.respond_to?(:call) + out = StringIO.new + @body.call(@response, out) + out.rewind + @body = out.read + else + @body + end + end end # Integration test methods such as ActionController::Integration::Session#get diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 112e9ef..5adc6f1 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -235,6 +235,12 @@ class TestController < ActionController::Base render :text => false end + def render_text_with_proc + render :text => Proc.new{ |resp, out| + 1.upto(10){ |i| out.write "#{i}" } + } + end + def render_nothing_with_appendix render :text => "appended" end @@ -949,6 +955,12 @@ class RenderTest < ActionController::TestCase get :render_text_with_false assert_equal 'false', @response.body end + + def test_render_text_with_proc + get :render_text_with_proc + assert_response :success + assert_select "element", 10 + end def test_render_nothing_with_appendix get :render_nothing_with_appendix diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb index f61bbc6..a37e6d1 100644 --- a/actionpack/test/controller/send_file_test.rb +++ b/actionpack/test/controller/send_file_test.rb @@ -46,13 +46,13 @@ class SendFileTest < ActionController::TestCase response = nil assert_nothing_raised { response = process('file') } assert_not_nil response - assert_kind_of Proc, response.body + assert_kind_of Proc, response.body(false) require 'stringio' output = StringIO.new output.binmode output.string.force_encoding(file_data.encoding) if output.string.respond_to?(:force_encoding) - assert_nothing_raised { response.body.call(response, output) } + assert_nothing_raised { response.body(false).call(response, output) } assert_equal file_data, output.string end