2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2010-10-04 19:30:16 -04:00
|
|
|
|
|
|
|
module ActionDispatch
|
|
|
|
class UploadedFileTest < ActiveSupport::TestCase
|
2010-10-04 20:08:25 -04:00
|
|
|
def test_constructor_with_argument_error
|
|
|
|
assert_raises(ArgumentError) do
|
|
|
|
Http::UploadedFile.new({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-04 19:30:16 -04:00
|
|
|
def test_original_filename
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(filename: "foo", tempfile: Object.new)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "foo", uf.original_filename
|
2010-10-04 19:30:16 -04:00
|
|
|
end
|
2012-03-06 16:34:20 -05:00
|
|
|
|
2017-05-12 14:43:34 -04:00
|
|
|
def test_filename_is_different_object
|
|
|
|
file_str = "foo"
|
|
|
|
uf = Http::UploadedFile.new(filename: file_str, tempfile: Object.new)
|
2017-12-12 06:00:50 -05:00
|
|
|
assert_not_equal file_str.object_id, uf.original_filename.object_id
|
2017-05-12 14:43:34 -04:00
|
|
|
end
|
|
|
|
|
2011-12-24 07:57:54 -05:00
|
|
|
def test_filename_should_be_in_utf_8
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(filename: "foo", tempfile: Object.new)
|
2011-12-24 07:57:54 -05:00
|
|
|
assert_equal "UTF-8", uf.original_filename.encoding.to_s
|
2011-06-14 03:59:18 -04:00
|
|
|
end
|
2010-10-04 19:30:16 -04:00
|
|
|
|
2014-07-16 14:35:27 -04:00
|
|
|
def test_filename_should_always_be_in_utf_8
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(filename: "foo".encode(Encoding::SHIFT_JIS),
|
|
|
|
tempfile: Object.new)
|
2014-07-16 14:35:27 -04:00
|
|
|
assert_equal "UTF-8", uf.original_filename.encoding.to_s
|
|
|
|
end
|
|
|
|
|
2010-10-04 19:30:16 -04:00
|
|
|
def test_content_type
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(type: "foo", tempfile: Object.new)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "foo", uf.content_type
|
2010-10-04 19:30:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_headers
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(head: "foo", tempfile: Object.new)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "foo", uf.headers
|
2010-10-04 19:30:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_tempfile
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(tempfile: "foo")
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "foo", uf.tempfile
|
2010-10-04 19:30:16 -04:00
|
|
|
end
|
2010-10-04 19:56:45 -04:00
|
|
|
|
2014-04-17 13:49:02 -04:00
|
|
|
def test_to_io_returns_the_tempfile
|
2014-04-15 14:05:08 -04:00
|
|
|
tf = Object.new
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(tempfile: tf)
|
2014-04-15 14:05:08 -04:00
|
|
|
assert_equal tf, uf.to_io
|
|
|
|
end
|
|
|
|
|
2010-11-18 13:10:18 -05:00
|
|
|
def test_delegates_path_to_tempfile
|
2016-08-06 12:54:50 -04:00
|
|
|
tf = Class.new { def path; "thunderhorse" end }
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(tempfile: tf.new)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "thunderhorse", uf.path
|
2010-11-18 13:10:18 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_delegates_open_to_tempfile
|
2016-08-06 12:54:50 -04:00
|
|
|
tf = Class.new { def open; "thunderhorse" end }
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(tempfile: tf.new)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "thunderhorse", uf.open
|
2010-11-18 13:10:18 -05:00
|
|
|
end
|
|
|
|
|
2012-09-20 05:57:38 -04:00
|
|
|
def test_delegates_close_to_tempfile
|
2016-10-28 23:05:58 -04:00
|
|
|
tf = Class.new { def close(unlink_now = false); "thunderhorse" end }
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(tempfile: tf.new)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "thunderhorse", uf.close
|
2012-09-20 05:57:38 -04:00
|
|
|
end
|
|
|
|
|
2012-09-22 16:37:00 -04:00
|
|
|
def test_close_accepts_parameter
|
2016-10-28 23:05:58 -04:00
|
|
|
tf = Class.new { def close(unlink_now = false); "thunderhorse: #{unlink_now}" end }
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(tempfile: tf.new)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "thunderhorse: true", uf.close(true)
|
2012-09-22 16:37:00 -04:00
|
|
|
end
|
|
|
|
|
2012-09-22 18:50:30 -04:00
|
|
|
def test_delegates_read_to_tempfile
|
2016-10-28 23:05:58 -04:00
|
|
|
tf = Class.new { def read(length = nil, buffer = nil); "thunderhorse" end }
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(tempfile: tf.new)
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "thunderhorse", uf.read
|
2010-10-04 19:56:45 -04:00
|
|
|
end
|
|
|
|
|
2012-09-22 18:50:30 -04:00
|
|
|
def test_delegates_read_to_tempfile_with_params
|
2016-10-28 23:05:58 -04:00
|
|
|
tf = Class.new { def read(length = nil, buffer = nil); [length, buffer] end }
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(tempfile: tf.new)
|
2010-10-04 22:28:40 -04:00
|
|
|
assert_equal %w{ thunder horse }, uf.read(*%w{ thunder horse })
|
2010-10-04 19:56:45 -04:00
|
|
|
end
|
2010-10-04 20:11:50 -04:00
|
|
|
|
|
|
|
def test_delegate_respects_respond_to?
|
2010-10-04 22:28:40 -04:00
|
|
|
tf = Class.new { def read; yield end; private :read }
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(tempfile: tf.new)
|
2010-10-04 20:11:50 -04:00
|
|
|
assert_raises(NoMethodError) do
|
2010-10-04 22:28:40 -04:00
|
|
|
uf.read
|
2010-10-04 20:11:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-23 15:10:01 -04:00
|
|
|
def test_delegate_eof_to_tempfile
|
|
|
|
tf = Class.new { def eof?; true end; }
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(tempfile: tf.new)
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate uf, :eof?
|
2011-08-23 15:10:01 -04:00
|
|
|
end
|
|
|
|
|
2010-10-04 20:11:50 -04:00
|
|
|
def test_respond_to?
|
2010-10-04 22:28:40 -04:00
|
|
|
tf = Class.new { def read; yield end }
|
2016-08-06 13:35:13 -04:00
|
|
|
uf = Http::UploadedFile.new(tempfile: tf.new)
|
2018-01-24 21:14:10 -05:00
|
|
|
assert_respond_to uf, :headers
|
|
|
|
assert_respond_to uf, :read
|
2010-10-04 20:11:50 -04:00
|
|
|
end
|
2010-10-04 19:30:16 -04:00
|
|
|
end
|
|
|
|
end
|