mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add option to force binary mode on tempfile used for fixture_file_upload. Closes #6380.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7478 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
498d8ff72e
commit
c87206cc57
3 changed files with 37 additions and 3 deletions
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue