1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
# frozen_string_literal: true
require "abstract_unit"
require "tempfile"
require "stringio"
module ActionDispatch
class UploadedFileTest < ActiveSupport::TestCase
def test_constructor_with_argument_error
assert_raises(ArgumentError) do
Http::UploadedFile.new({})
end
end
def test_original_filename
uf = Http::UploadedFile.new(filename: "foo", tempfile: Tempfile.new)
assert_equal "foo", uf.original_filename
end
def test_filename_is_different_object
file_str = "foo"
uf = Http::UploadedFile.new(filename: file_str, tempfile: Tempfile.new)
assert_not_equal file_str.object_id, uf.original_filename.object_id
end
def test_filename_should_be_in_utf_8
uf = Http::UploadedFile.new(filename: "foo", tempfile: Tempfile.new)
assert_equal "UTF-8", uf.original_filename.encoding.to_s
end
def test_filename_should_always_be_in_utf_8
uf = Http::UploadedFile.new(filename: "foo".encode(Encoding::SHIFT_JIS),
tempfile: Tempfile.new)
assert_equal "UTF-8", uf.original_filename.encoding.to_s
end
def test_content_type
uf = Http::UploadedFile.new(type: "foo", tempfile: Tempfile.new)
assert_equal "foo", uf.content_type
end
def test_headers
uf = Http::UploadedFile.new(head: "foo", tempfile: Tempfile.new)
assert_equal "foo", uf.headers
end
def test_headers_should_be_in_utf_8
uf = Http::UploadedFile.new(filename: "foo", head: "foo", tempfile: Tempfile.new)
assert_equal "UTF-8", uf.headers.encoding.to_s
end
def test_headers_should_always_be_in_utf_8
uf = Http::UploadedFile.new(filename: "foo",
head: "\xC3foo".dup.force_encoding(Encoding::ASCII_8BIT),
tempfile: Tempfile.new)
assert_equal "UTF-8", uf.headers.encoding.to_s
end
def test_tempfile
tf = Tempfile.new
uf = Http::UploadedFile.new(tempfile: tf)
assert_equal tf, uf.tempfile
end
def test_to_io_returns_file
tf = Tempfile.new
uf = Http::UploadedFile.new(tempfile: tf)
assert_equal tf.to_io, uf.to_io
end
def test_delegates_path_to_tempfile
tf = Tempfile.new
uf = Http::UploadedFile.new(tempfile: tf)
assert_equal tf.path, uf.path
end
def test_delegates_open_to_tempfile
tf = Tempfile.new
tf.close
uf = Http::UploadedFile.new(tempfile: tf)
assert_equal tf, uf.open
assert_not tf.closed?
end
def test_delegates_close_to_tempfile
tf = Tempfile.new
uf = Http::UploadedFile.new(tempfile: tf)
uf.close
assert_predicate tf, :closed?
end
def test_close_accepts_parameter
tf = Tempfile.new
uf = Http::UploadedFile.new(tempfile: tf)
uf.close(true)
assert_predicate tf, :closed?
assert_nil tf.path
end
def test_delegates_read_to_tempfile
tf = Tempfile.new
tf << "thunderhorse"
tf.rewind
uf = Http::UploadedFile.new(tempfile: tf)
assert_equal "thunderhorse", uf.read
end
def test_delegates_read_to_tempfile_with_params
tf = Tempfile.new
tf << "thunderhorse"
tf.rewind
uf = Http::UploadedFile.new(tempfile: tf)
assert_equal "thunder", uf.read(7)
assert_equal "horse", uf.read(5, String.new)
end
def test_delegate_eof_to_tempfile
tf = Tempfile.new
tf << "thunderhorse"
uf = Http::UploadedFile.new(tempfile: tf)
assert_equal true, uf.eof?
tf.rewind
assert_equal false, uf.eof?
end
def test_delegate_to_path_to_tempfile
tf = Tempfile.new
uf = Http::UploadedFile.new(tempfile: tf)
assert_equal tf.to_path, uf.to_path
end
def test_io_copy_stream
tf = Tempfile.new
tf << "thunderhorse"
tf.rewind
uf = Http::UploadedFile.new(tempfile: tf)
result = StringIO.new
IO.copy_stream(uf, result)
assert_equal "thunderhorse", result.string
end
end
end
|