diff options
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_controller/test_process.rb | 14 |
1 files changed, 11 insertions, 3 deletions
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 |