aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/CHANGELOG2
-rw-r--r--actionpack/lib/action_controller/test_process.rb14
-rw-r--r--actionpack/test/controller/test_test.rb24
3 files changed, 37 insertions, 3 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index d2d0212cbd..ec7cb735aa 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add option to force binary mode on tempfile used for fixture_file_upload. #6380 [Jonathan Viney]
+
* Fixed that resource namespaces wouldn't stick to all nested resources #9399 [pixeltrix]
* Moved ActionController::Macros::AutoComplete into the auto_complete plugin on the official Rails svn #9512 [lifofifo]
diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb
index 80abe2fecd..9b34f76950 100644
--- a/actionpack/lib/action_controller/test_process.rb
+++ b/actionpack/lib/action_controller/test_process.rb
@@ -330,6 +330,9 @@ module ActionController #:nodoc:
#
# Usage example, within a functional test:
# post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png')
+ #
+ # Pass a true third parameter to ensure the uploaded file is opened in binary mode (only required for Windows):
+ # post :change_avatar, :avatar => ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + '/files/spongebob.png', 'image/png', :binary)
require 'tempfile'
class TestUploadedFile
# The filename, *not* including the path, of the "uploaded" file
@@ -338,11 +341,12 @@ module ActionController #:nodoc:
# The content type of the "uploaded" file
attr_reader :content_type
- def initialize(path, content_type = Mime::TEXT)
+ 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 }
@tempfile = Tempfile.new(@original_filename)
+ @tempfile.binmode if binary
FileUtils.copy_file(path, @tempfile.path)
end
@@ -468,10 +472,14 @@ module ActionController #:nodoc:
# Shortcut for ActionController::TestUploadedFile.new(Test::Unit::TestCase.fixture_path + path, type). Example:
# post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png')
- def fixture_file_upload(path, mime_type = nil)
+ #
+ # To upload binary files on Windows, pass :binary as the last parameter. This will not affect other platforms.
+ # post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
+ def fixture_file_upload(path, mime_type = nil, binary = false)
ActionController::TestUploadedFile.new(
Test::Unit::TestCase.respond_to?(:fixture_path) ? Test::Unit::TestCase.fixture_path + path : path,
- mime_type
+ mime_type,
+ binary
)
end
diff --git a/actionpack/test/controller/test_test.rb b/actionpack/test/controller/test_test.rb
index 10818fe1c9..8a57f37b68 100644
--- a/actionpack/test/controller/test_test.rb
+++ b/actionpack/test/controller/test_test.rb
@@ -460,6 +460,30 @@ XML
assert_equal file.path, file.local_path
assert_equal File.read(path), file.read
end
+
+ def test_test_uploaded_file_with_binary
+ filename = 'mona_lisa.jpg'
+ path = "#{FILES_DIR}/#{filename}"
+ content_type = 'image/png'
+
+ binary_uploaded_file = ActionController::TestUploadedFile.new(path, content_type, :binary)
+ assert_equal File.open(path, 'rb').read, binary_uploaded_file.read
+
+ plain_uploaded_file = ActionController::TestUploadedFile.new(path, content_type)
+ assert_equal File.open(path, 'r').read, plain_uploaded_file.read
+ end
+
+ def test_fixture_file_upload_with_binary
+ filename = 'mona_lisa.jpg'
+ path = "#{FILES_DIR}/#{filename}"
+ content_type = 'image/jpg'
+
+ binary_file_upload = fixture_file_upload(path, content_type, :binary)
+ assert_equal File.open(path, 'rb').read, binary_file_upload.read
+
+ plain_file_upload = fixture_file_upload(path, content_type)
+ assert_equal File.open(path, 'r').read, plain_file_upload.read
+ end
def test_fixture_file_upload
post :test_file_upload, :file => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")