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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
# frozen_string_literal: true
require "mime/types"
require "minitest_helper"
describe MIME::Types, "registry" do
def setup
MIME::Types.send(:load_default_mime_types)
end
describe "is enumerable" do
it "correctly uses an Enumerable method like #any?" do
assert(MIME::Types.any? { |type| type.content_type == "text/plain" })
end
it "implements each with no parameters to return an Enumerator" do
assert_kind_of Enumerator, MIME::Types.each
assert_kind_of Enumerator, MIME::Types.map
end
it "will create a lazy enumerator" do
assert_kind_of Enumerator::Lazy, MIME::Types.lazy
assert_kind_of Enumerator::Lazy, MIME::Types.map.lazy
end
it "is countable with an enumerator" do
assert MIME::Types.each.count > 999
assert MIME::Types.lazy.count > 999
end
end
describe ".[]" do
it "can be searched with a MIME::Type" do
text_plain = MIME::Type.new("content-type" => "text/plain")
assert_includes MIME::Types[text_plain], "text/plain"
assert_equal 1, MIME::Types[text_plain].size
end
it "can be searched with a regular expression" do
assert_includes MIME::Types[/plain$/], "text/plain"
assert_equal 1, MIME::Types[/plain$/].size
end
it "sorts by priority with multiple matches" do
types = MIME::Types[/gzip$/].select { |t|
%w[application/gzip application/x-gzip multipart/x-gzip].include?(t)
}
# This is this way because of a new type ending with gzip that only
# appears in some data files.
assert_equal %w[application/gzip application/x-gzip multipart/x-gzip], types
assert_equal 3, types.size
end
it "can be searched with a string" do
assert_includes MIME::Types["text/plain"], "text/plain"
assert_equal 1, MIME::Types["text/plain"].size
end
it "can be searched with the complete flag" do
assert_empty MIME::Types[
"application/x-www-form-urlencoded",
complete: true
]
assert_includes MIME::Types["text/plain", complete: true], "text/plain"
assert_equal 1, MIME::Types["text/plain", complete: true].size
end
it "can be searched with the registered flag" do
assert_empty MIME::Types["application/x-wordperfect6.1", registered: true]
refute_empty MIME::Types[
"application/x-www-form-urlencoded",
registered: true
]
refute_empty MIME::Types[/gzip/, registered: true]
refute_equal MIME::Types[/gzip/], MIME::Types[/gzip/, registered: true]
end
end
describe ".type_for" do
it "finds all types for a given extension" do
assert_equal %w[application/gzip application/x-gzip],
MIME::Types.type_for("gz")
end
it "separates the extension from filenames" do
assert_equal %w[image/jpeg], MIME::Types.of(["foo.jpeg", "bar.jpeg"])
end
it "finds multiple extensions" do
assert_equal %w[image/jpeg text/plain],
MIME::Types.type_for(%w[foo.txt foo.jpeg])
end
it "does not find unknown extensions" do
assert_empty MIME::Types.type_for("zzz")
end
it "modifying type extensions causes reindexing" do
plain_text = MIME::Types["text/plain"].first
plain_text.add_extensions("xtxt")
assert_includes MIME::Types.type_for("xtxt"), "text/plain"
end
it "handles newline characters correctly" do
assert_includes MIME::Types.type_for("test.pdf\n.txt"), "text/plain"
assert_includes MIME::Types.type_for("test.txt\n.pdf"), "application/pdf"
end
end
describe ".count" do
it "can count the number of types inside" do
assert MIME::Types.count > 999
end
end
describe ".add" do
def setup
MIME::Types.instance_variable_set(:@__types__, nil)
MIME::Types.send(:load_default_mime_types)
end
let(:eruby) { MIME::Type.new("content-type" => "application/x-eruby") }
let(:jinja) { MIME::Type.new("content-type" => "application/jinja2") }
it "successfully adds a new type" do
MIME::Types.add(eruby)
assert_equal MIME::Types["application/x-eruby"], [eruby]
end
it "complains about adding a duplicate type" do
MIME::Types.add(eruby)
assert_output "", /is already registered as a variant/ do
MIME::Types.add(eruby)
end
assert_equal MIME::Types["application/x-eruby"], [eruby]
end
it "does not complain about adding a duplicate type when quiet" do
MIME::Types.add(eruby)
assert_silent do
MIME::Types.add(eruby, :silent)
end
assert_equal MIME::Types["application/x-eruby"], [eruby]
end
it "successfully adds from an array" do
MIME::Types.add([eruby, jinja])
assert_equal MIME::Types["application/x-eruby"], [eruby]
assert_equal MIME::Types["application/jinja2"], [jinja]
end
it "successfully adds from another MIME::Types" do
old_count = MIME::Types.count
mt = MIME::Types.new
mt.add(eruby)
MIME::Types.add(mt)
assert_equal old_count + 1, MIME::Types.count
assert_equal MIME::Types[eruby.content_type], [eruby]
end
end
end
|