From ab7366e656a40ccedb1bed94054eaec60742f8df Mon Sep 17 00:00:00 2001 From: Sava Chankov Date: Sun, 17 May 2009 18:57:39 +0300 Subject: [PATCH] Fix send_data length in Ruby 1.9 --- actionpack/lib/action_controller/base/streaming.rb | 2 +- actionpack/test/controller/send_file_test.rb | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_controller/base/streaming.rb b/actionpack/lib/action_controller/base/streaming.rb index 9f80f48..250ae3a 100644 --- a/actionpack/lib/action_controller/base/streaming.rb +++ b/actionpack/lib/action_controller/base/streaming.rb @@ -137,7 +137,7 @@ module ActionController #:nodoc: # instead. See ActionController::Base#render for more information. def send_data(data, options = {}) #:doc: logger.info "Sending data #{options[:filename]}" if logger - send_file_headers! options.merge(:length => data.size) + send_file_headers! options.merge(:length => (RUBY_VERSION > '1.9' ? data.bytesize : data.size)) @performed_render = false render :status => options[:status], :text => data end diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb index 6007ebe..52858fc 100644 --- a/actionpack/test/controller/send_file_test.rb +++ b/actionpack/test/controller/send_file_test.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 require 'abstract_unit' module TestFileUtils @@ -15,6 +16,7 @@ class SendFileController < ActionController::Base def file() send_file(file_path, options) end def data() send_data(file_data, options) end + def multibyte_text_data() send_data("Кирилица\n祝您好運", options) end def rescue_action(e) raise end end @@ -50,7 +52,11 @@ class SendFileTest < ActionController::TestCase output = StringIO.new output.binmode assert_nothing_raised { response.body_parts.each { |part| output << part.to_s } } - assert_equal file_data, output.string + if RUBY_VERSION < '1.9' + assert_equal file_data, output.string + else + assert_equal file_data.force_encoding('UTF-8'), output.string + end end def test_file_url_based_filename @@ -158,4 +164,12 @@ class SendFileTest < ActionController::TestCase assert_equal 200, @response.status end end + + def test_send_data_content_length_header + @controller.headers = {} + @controller.options = { :type => :text, :filename => 'file_with_utf8_text' } + response = process('multibyte_text_data') + headers = @controller.headers + assert_equal '29', headers['Content-Length'] + end end -- 1.6.2.3