This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mime-type-spec.io
executable file
·287 lines (249 loc) · 10.2 KB
/
mime-type-spec.io
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#! /usr/bin/env iospec2/iospec
describe(MimeType,
describe(simplify, "simplify",
simplify("does not change already simplified strings",
verify("text/plain" == MimeType simplify("text/plain"))
verify("image/jpeg" == MimeType simplify("image/jpeg"))
)
simplify("simplifies extension types",
verify("application/msword" == MimeType simplify("application/x-msword"))
verify("application/msword" == MimeType simplify("x-application/msword"))
)
simplify("lowercases the results",
verify("text/vcard" == MimeType simplify("text/vCard"))
verify("text/vcard" == MimeType simplify("text/X-vCard"))
)
simplify("does not remove a non-starting extension marker",
verify("text-x/vcard-x" == MimeType simplify("text-x/vcard-x"))
)
simplify("raises an exception for an invalid type",
r := nil
e := try(MimeType simplify("text"))
e catch(Exception,
verify(e error == "Invalid Content-Type text")
)
)
)
describe(isAscii, "isAscii",
isAscii("detects default ASCII types",
verify(MimeType with("text/plain") isAscii)
verify(MimeType with("text/vCard") isAscii)
)
isAscii("detects default binary types",
verify(MimeType with("application/x-msword") isAscii not)
verify(MimeType with("image/jpeg") isAscii not)
)
)
describe(isBinary, "isBinary",
isBinary("detects default ASCII types",
verify(MimeType with("text/plain") isBinary not)
verify(MimeType with("text/vCard") isBinary not)
)
isBinary("detects default binary types",
verify(MimeType with("application/x-msword") isBinary)
verify(MimeType with("image/jpeg") isBinary)
)
)
describe(isSignature, "isSignature",
isSignature("reports the signature slot properly",
verify(MimeType with("text/plain") isSignature not)
verify(
MimeType with("application/pkcs7") do(signature := true) isSignature
)
)
)
describe(isComplete, "isComplete",
isComplete("says a type is complete if there are extensions",
yaml := MimeType with(list("text/x-yaml", "yaml", "yml")) do(
encoding := "8bit"
)
verify(yaml isComplete)
)
isComplete("says a type is not complete if there are no extensions",
yaml := MimeType with("text/x-yaml") do(
encoding := "8bit"
)
verify(yaml isComplete not)
)
)
describe(construction, "construction",
construction("sets the simplified slot properly",
verify("text/plain" == MimeType with("text/plain") simplified)
verify(
"application/msword" == MimeType with("application/x-msword") simplified
)
verify(
"application/msword" == MimeType with("x-application/msword") simplified
)
verify(
"application/msx-word" == MimeType with("application/msx-word") simplified
)
)
construction("sets the subType slot properly",
verify("plain" == MimeType with("text/plain") subType)
verify("msword" == MimeType with("application/x-msword") subType)
verify("msword" == MimeType with("x-application/msword") subType)
verify("msx-word" == MimeType with("application/msx-word") subType)
)
construction("sets the contentType slot properly",
verify("text/plain" == MimeType with("text/plain") contentType)
verify("application/x-msword" == MimeType with("application/x-msword") contentType)
verify("x-application/msword" == MimeType with("x-application/msword") contentType)
verify("application/msx-word" == MimeType with("application/msx-word") contentType)
)
)
describe(asMap, "asMap",
asMap("provides the correct minimal keys",
t := MimeType with("a/b")
m := t asMap
keys := list("content-type", "registered", "encoding")
keys foreach(key, verify(m hasKey(key)))
)
asMap("provides the correct extra keys",
t := MimeType with("a/b") do(
docs := list("Something")
addExtensions("b")
obsolete := true
useInstead := list("c/d")
signature := true
)
m := t asMap
keys := list("docs", "extensions", "obsolete", "use-instead", "signature")
keys foreach(key, verify(m hasKey(key)))
)
)
describe(asJson, "asJson",
asJson("provides the correct output",
j := MimeType with("a/b")
e := MimeType with(Yajl parseJson(j asJson))
verify(j compare(e) == 0)
)
)
describe(asString, "asString",
asString("provides the correct output",
m := MimeType with("text/plain")
verify("text/plain" == "#{m}" interpolate)
)
)
/*
def test_CMP # '<=>'
assert(MIME::Type.new('text/plain') == MIME::Type.new('text/plain'))
assert(MIME::Type.new('text/plain') != MIME::Type.new('image/jpeg'))
assert(MIME::Type.new('text/plain') == 'text/plain')
assert(MIME::Type.new('text/plain') != 'image/jpeg')
assert(MIME::Type.new('text/plain') > MIME::Type.new('text/html'))
assert(MIME::Type.new('text/plain') > 'text/html')
assert(MIME::Type.new('text/html') < MIME::Type.new('text/plain'))
assert(MIME::Type.new('text/html') < 'text/plain')
assert('text/html' == MIME::Type.new('text/html'))
assert('text/html' < MIME::Type.new('text/plain'))
assert('text/plain' > MIME::Type.new('text/html'))
end
def test_encoding
assert_equal('quoted-printable', MIME::Type.new('text/plain').encoding)
assert_equal('base64', MIME::Type.new('image/jpeg').encoding)
assert_equal('base64', MIME::Type.new('application/x-msword').encoding)
assert_equal('quoted-printable', MIME::Type.new('text/vCard').encoding)
assert_equal('base64', MIME::Type.new('application/pkcs7-mime').encoding)
end
def test_encoding_equals
yaml = make_yaml_mime_type
assert_equal('8bit', yaml.encoding)
yaml.encoding = 'base64'
assert_equal('base64', yaml.encoding)
yaml.encoding = :default
assert_equal('quoted-printable', yaml.encoding)
begin
yaml.encoding = 'binary'
rescue MIME::Type::InvalidEncoding => ex
assert_equal('Invalid Encoding "binary" (valid values are [nil, :default, "base64", "8bit", "7bit", "quoted-printable"]).', ex.message)
end
end
def test_default_encoding
%w(text/plain text/html).each { |mt|
assert_equal('quoted-printable', MIME::Type.new(mt).default_encoding)
}
%w(image/jpeg applicatoin/pkcs7-mime).each { |mt|
assert_equal('base64', MIME::Type.new(mt).default_encoding)
}
end
def test_like_eh
assert(MIME::Type.new('text/plain').like?(MIME::Type.new('text/plain')))
assert(MIME::Type.new('text/plain').like?(MIME::Type.new('text/x-plain')))
refute(MIME::Type.new('text/plain').like?(MIME::Type.new('image/jpeg')))
assert(MIME::Type.new('text/plain').like?('text/plain'))
assert(MIME::Type.new('text/plain').like?('text/x-plain'))
refute(MIME::Type.new('text/plain').like?('image/jpeg'))
end
def test_media_type
assert_equal('text', MIME::Type.new('text/plain').media_type)
assert_equal('image', MIME::Type.new('image/jpeg').media_type)
assert_equal('application', MIME::Type.new('application/x-msword').media_type)
assert_equal('text', MIME::Type.new('text/vCard').media_type)
assert_equal('application', MIME::Type.new('application/pkcs7-mime').media_type)
assert_equal('chemical', MIME::Type.new('x-chemical/x-pdb').media_type)
assert_equal('appl', @applzip.media_type)
end
def test_obsolete_eh
type = MIME::Type.new('content-type' => 'test/type',
'obsolete' => true)
assert(type.obsolete?)
refute(make_yaml_mime_type.obsolete?)
end
def assert_priority(l, e, r)
assert_equal(-1, l.first.priority_compare(l.last))
assert_equal(0, e.first.priority_compare(e.last))
assert_equal(1, r.first.priority_compare(r.last))
end
def test_priority_compare
tl, te, tr = make('text/1'), make('text/1'), make('text/2')
assert_priority([tl, tr], [tl, te], [tr, tl])
tl.registered = te.registered = true
tr = make(tl) { |t| t.registered = false }
assert_priority([tl, tr], [tl, te], [tr, tl])
tl.system = te.system = nil
tr = make(tl) { |t| t.system = /#{RUBY_PLATFORM}/ }
assert_priority([tl, tr], [tl, te], [tr, tl])
tl.extensions = te.extensions = %w(1)
tr = make(tl) { |t| t.extensions = nil }
assert_priority([tl, tr], [tl, te], [tr, tl])
tl.obsolete = te.obsolete = false
tr = make(tl) { |t| t.obsolete = true }
assert_priority([tl, tr], [tl, te], [tr, tl])
tl.obsolete = te.obsolete = true
tl.use_instead = te.use_instead = 'abc/xyz'
tr = make(tl) { |t| t.use_instead = nil }
assert_priority([tl, tr], [tl, te], [tr, tl])
tr.use_instead = 'abc/zzz'
assert_priority([tl, tr], [tl, te], [tr, tl])
end
def test_raw_media_type
assert_equal('text', MIME::Type.new('text/plain').raw_media_type)
assert_equal('image', MIME::Type.new('image/jpeg').raw_media_type)
assert_equal('application', MIME::Type.new('application/x-msword').raw_media_type)
assert_equal('text', MIME::Type.new('text/vCard').raw_media_type)
assert_equal('application', MIME::Type.new('application/pkcs7-mime').raw_media_type)
assert_equal('x-chemical', MIME::Type.new('x-chemical/x-pdb').raw_media_type)
assert_equal('x-appl', @applzip.raw_media_type)
end
def test_raw_sub_type
assert_equal('plain', MIME::Type.new('text/plain').raw_sub_type)
assert_equal('jpeg', MIME::Type.new('image/jpeg').raw_sub_type)
assert_equal('x-msword', MIME::Type.new('application/x-msword').raw_sub_type)
assert_equal('vCard', MIME::Type.new('text/vCard').raw_sub_type)
assert_equal('pkcs7-mime', MIME::Type.new('application/pkcs7-mime').raw_sub_type)
assert_equal('x-zip', @applzip.raw_sub_type)
end
def test_registered_eh
assert(MIME::Type.new('text/plain').registered?)
assert(MIME::Type.new('image/jpeg').registered?)
refute(MIME::Type.new('application/x-msword').registered?)
assert(MIME::Type.new('text/vCard').registered?)
assert(MIME::Type.new('application/pkcs7-mime').registered?)
refute(@applzip.registered?)
refute(MIME::Types['image/webp'].first.registered?)
# Temporarily broken: requires the new data format to be enabled.
assert(MIME::Types['application/x-www-form-urlencoded'].first.registered?)
end
*/
)