From 91feac74b8de9a7aefe42970d523af3984e5330d Mon Sep 17 00:00:00 2001 From: KAKUTANI Shintaro Date: Tue, 17 Mar 2009 15:12:23 +0900 Subject: [PATCH] Use File.basename to extract original filename instead (String|Pathname)#sub Rails.root returns Pathname instance since Rails 2.3. --- actionpack/lib/action_controller/test_process.rb | 2 +- actionpack/test/controller/test_test.rb | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index dbaec00..1854fcc 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -365,7 +365,7 @@ module ActionController #:nodoc: def initialize(path, content_type = Mime::TEXT, binary = false) raise "#{path} file does not exist" unless File.exist?(path) @content_type = content_type - @original_filename = path.sub(/^.*#{File::SEPARATOR}([^#{File::SEPARATOR}]+)$/) { $1 } + @original_filename = File.basename(path.to_s) @tempfile = Tempfile.new(@original_filename) @tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding) @tempfile.binmode if binary diff --git a/actionpack/test/controller/test_test.rb b/actionpack/test/controller/test_test.rb index 65c894c..1cd3818 100644 --- a/actionpack/test/controller/test_test.rb +++ b/actionpack/test/controller/test_test.rb @@ -573,6 +573,21 @@ XML assert_equal File.open(path, READ_PLAIN).read, plain_uploaded_file.read end + def test_test_upload_file_with_pathname + filename = 'mona_lisa.jpg' + # Rails.root return Pathname instance since Rails 2.3.x + path = Pathname.new("#{FILES_DIR}").join(filename) + content_type = 'image/png' + expected = File.read(path) + expected.force_encoding(Encoding::BINARY) if expected.respond_to?(:force_encoding) + + file = ActionController::TestUploadedFile.new(path, content_type) + assert_equal filename, file.original_filename + assert_equal content_type, file.content_type + assert_equal file.path, file.local_path + assert_equal expected, file.read + end + def test_fixture_file_upload_with_binary filename = 'mona_lisa.jpg' path = "#{FILES_DIR}/#{filename}" -- 1.5.4.3