mime-types 3.2.1 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Code-of-Conduct.md +10 -10
- data/Contributing.md +82 -89
- data/History.md +178 -120
- data/Licence.md +3 -3
- data/README.rdoc +2 -8
- data/Rakefile +153 -139
- data/lib/mime/type/columnar.rb +4 -4
- data/lib/mime/type.rb +155 -94
- data/lib/mime/types/_columnar.rb +20 -20
- data/lib/mime/types/cache.rb +11 -11
- data/lib/mime/types/columnar.rb +1 -1
- data/lib/mime/types/container.rb +14 -13
- data/lib/mime/types/deprecations.rb +15 -13
- data/lib/mime/types/full.rb +2 -2
- data/lib/mime/types/loader.rb +28 -15
- data/lib/mime/types/logger.rb +4 -6
- data/lib/mime/types/registry.rb +9 -9
- data/lib/mime/types.rb +19 -16
- data/lib/mime-types.rb +1 -1
- data/test/minitest_helper.rb +7 -11
- data/test/test_mime_type.rb +269 -262
- data/test/test_mime_types.rb +72 -74
- data/test/test_mime_types_cache.rb +37 -39
- data/test/test_mime_types_class.rb +61 -63
- data/test/test_mime_types_lazy.rb +16 -18
- data/test/test_mime_types_loader.rb +14 -16
- metadata +22 -47
data/test/test_mime_type.rb
CHANGED
@@ -1,311 +1,307 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
require 'mime/types'
|
6
|
-
require 'minitest_helper'
|
3
|
+
require "mime/types"
|
4
|
+
require "minitest_helper"
|
7
5
|
|
8
6
|
describe MIME::Type do
|
9
|
-
# it { fail }
|
10
|
-
|
11
7
|
def mime_type(content_type)
|
12
8
|
MIME::Type.new(content_type) { |mt| yield mt if block_given? }
|
13
9
|
end
|
14
10
|
|
15
11
|
let(:x_appl_x_zip) {
|
16
|
-
mime_type(
|
12
|
+
mime_type("x-appl/x-zip") { |t| t.extensions = %w[zip zp] }
|
17
13
|
}
|
18
|
-
let(:text_plain) { mime_type(
|
19
|
-
let(:text_html) { mime_type(
|
20
|
-
let(:image_jpeg) { mime_type(
|
14
|
+
let(:text_plain) { mime_type("text/plain") }
|
15
|
+
let(:text_html) { mime_type("text/html") }
|
16
|
+
let(:image_jpeg) { mime_type("image/jpeg") }
|
21
17
|
let(:application_javascript) {
|
22
|
-
mime_type(
|
23
|
-
js.friendly(
|
18
|
+
mime_type("application/javascript") do |js|
|
19
|
+
js.friendly("en" => "JavaScript")
|
24
20
|
js.xrefs = {
|
25
|
-
|
26
|
-
|
21
|
+
"rfc" => %w[rfc4239 rfc4239],
|
22
|
+
"template" => %w[application/javascript]
|
27
23
|
}
|
28
|
-
js.encoding =
|
29
|
-
js.extensions = %w
|
24
|
+
js.encoding = "8bit"
|
25
|
+
js.extensions = %w[js sj]
|
30
26
|
js.registered = true
|
31
27
|
end
|
32
28
|
}
|
33
29
|
let(:text_x_yaml) {
|
34
|
-
mime_type(
|
35
|
-
yaml.extensions = %w
|
36
|
-
yaml.encoding
|
37
|
-
yaml.friendly(
|
30
|
+
mime_type("text/x-yaml") do |yaml|
|
31
|
+
yaml.extensions = %w[yaml yml]
|
32
|
+
yaml.encoding = "8bit"
|
33
|
+
yaml.friendly("en" => "YAML Structured Document")
|
38
34
|
end
|
39
35
|
}
|
40
36
|
let(:text_x_yaml_with_docs) {
|
41
37
|
text_x_yaml.dup.tap do |yaml|
|
42
|
-
yaml.docs =
|
38
|
+
yaml.docs = "Test YAML"
|
43
39
|
end
|
44
40
|
}
|
45
41
|
|
46
|
-
describe
|
47
|
-
it
|
48
|
-
assert_equal
|
42
|
+
describe ".simplified" do
|
43
|
+
it "leaves normal types alone" do
|
44
|
+
assert_equal "text/plain", MIME::Type.simplified("text/plain")
|
49
45
|
end
|
50
46
|
|
51
|
-
it
|
52
|
-
assert_equal
|
53
|
-
MIME::Type.simplified(
|
54
|
-
assert_equal
|
47
|
+
it "does not remove x- prefixes by default" do
|
48
|
+
assert_equal "application/x-msword",
|
49
|
+
MIME::Type.simplified("application/x-msword")
|
50
|
+
assert_equal "x-xyz/abc", MIME::Type.simplified("x-xyz/abc")
|
55
51
|
end
|
56
52
|
|
57
|
-
it
|
58
|
-
assert_equal
|
59
|
-
MIME::Type.simplified(
|
60
|
-
assert_equal
|
61
|
-
MIME::Type.simplified(
|
53
|
+
it "removes x- prefixes when requested" do
|
54
|
+
assert_equal "application/msword",
|
55
|
+
MIME::Type.simplified("application/x-msword", remove_x_prefix: true)
|
56
|
+
assert_equal "xyz/abc",
|
57
|
+
MIME::Type.simplified("x-xyz/abc", remove_x_prefix: true)
|
62
58
|
end
|
63
59
|
|
64
|
-
it
|
65
|
-
assert_equal
|
60
|
+
it "lowercases mixed-case types" do
|
61
|
+
assert_equal "text/vcard", MIME::Type.simplified("text/vCard")
|
66
62
|
end
|
67
63
|
|
68
|
-
it
|
69
|
-
assert_nil MIME::Type.simplified(
|
64
|
+
it "returns nil when the value provided is not a valid content type" do
|
65
|
+
assert_nil MIME::Type.simplified("text")
|
70
66
|
end
|
71
67
|
end
|
72
68
|
|
73
|
-
describe
|
74
|
-
it
|
75
|
-
assert_equal
|
69
|
+
describe ".i18n_key" do
|
70
|
+
it "converts text/plain to text.plain" do
|
71
|
+
assert_equal "text.plain", MIME::Type.i18n_key("text/plain")
|
76
72
|
end
|
77
73
|
|
78
|
-
it
|
79
|
-
assert_equal
|
80
|
-
MIME::Type.i18n_key(
|
74
|
+
it "does not remove x-prefixes" do
|
75
|
+
assert_equal "application.x-msword",
|
76
|
+
MIME::Type.i18n_key("application/x-msword")
|
81
77
|
end
|
82
78
|
|
83
|
-
it
|
84
|
-
assert_equal
|
79
|
+
it "converts text/vCard to text.vcard" do
|
80
|
+
assert_equal "text.vcard", MIME::Type.i18n_key("text/vCard")
|
85
81
|
end
|
86
82
|
|
87
|
-
it
|
88
|
-
assert_nil MIME::Type.i18n_key(
|
83
|
+
it "returns nil when the value provided is not a valid content type" do
|
84
|
+
assert_nil MIME::Type.i18n_key("text")
|
89
85
|
end
|
90
86
|
end
|
91
87
|
|
92
|
-
describe
|
93
|
-
it
|
88
|
+
describe ".new" do
|
89
|
+
it "fails if an invalid content type is provided" do
|
94
90
|
exception = assert_raises MIME::Type::InvalidContentType do
|
95
|
-
MIME::Type.new(
|
91
|
+
MIME::Type.new("apps")
|
96
92
|
end
|
97
93
|
assert_equal 'Invalid Content-Type "apps"', exception.to_s
|
98
94
|
end
|
99
95
|
|
100
|
-
it
|
101
|
-
type = MIME::Type.new(
|
96
|
+
it "creates a valid content type just from a string" do
|
97
|
+
type = MIME::Type.new("text/x-yaml")
|
102
98
|
|
103
99
|
assert_instance_of MIME::Type, type
|
104
|
-
assert_equal
|
100
|
+
assert_equal "text/x-yaml", type.content_type
|
105
101
|
end
|
106
102
|
|
107
|
-
it
|
108
|
-
MIME::Type.new(
|
103
|
+
it "yields the content type in a block" do
|
104
|
+
MIME::Type.new("text/x-yaml") do |type|
|
109
105
|
assert_instance_of MIME::Type, type
|
110
|
-
assert_equal
|
106
|
+
assert_equal "text/x-yaml", type.content_type
|
111
107
|
end
|
112
108
|
end
|
113
109
|
|
114
|
-
it
|
110
|
+
it "creates a valid content type from a hash" do
|
115
111
|
type = MIME::Type.new(
|
116
|
-
|
117
|
-
|
112
|
+
"content-type" => "text/x-yaml",
|
113
|
+
"obsolete" => true
|
118
114
|
)
|
119
115
|
assert_instance_of MIME::Type, type
|
120
|
-
assert_equal
|
116
|
+
assert_equal "text/x-yaml", type.content_type
|
121
117
|
assert type.obsolete?
|
122
118
|
end
|
123
119
|
|
124
|
-
it
|
125
|
-
type = MIME::Type.new(%w
|
120
|
+
it "creates a valid content type from an array" do
|
121
|
+
type = MIME::Type.new(%w[text/x-yaml yaml yml yz])
|
126
122
|
assert_instance_of MIME::Type, type
|
127
|
-
assert_equal
|
128
|
-
assert_equal %w
|
123
|
+
assert_equal "text/x-yaml", type.content_type
|
124
|
+
assert_equal %w[yaml yml yz], type.extensions
|
129
125
|
end
|
130
126
|
end
|
131
127
|
|
132
|
-
describe
|
133
|
-
it
|
128
|
+
describe "#like?" do
|
129
|
+
it "compares two MIME::Types on #simplified values without x- prefixes" do
|
134
130
|
assert text_plain.like?(text_plain)
|
135
131
|
refute text_plain.like?(text_html)
|
136
132
|
end
|
137
133
|
|
138
|
-
it
|
134
|
+
it "compares MIME::Type against string without x- prefixes" do
|
139
135
|
assert text_plain.like?(text_plain.to_s)
|
140
136
|
refute text_plain.like?(text_html.to_s)
|
141
137
|
end
|
142
138
|
end
|
143
139
|
|
144
|
-
describe
|
145
|
-
it
|
140
|
+
describe "#<=>" do
|
141
|
+
it "correctly compares identical types" do
|
146
142
|
assert_equal text_plain, text_plain
|
147
143
|
end
|
148
144
|
|
149
|
-
it
|
150
|
-
right = mime_type(
|
145
|
+
it "correctly compares equivalent types" do
|
146
|
+
right = mime_type("text/Plain")
|
151
147
|
refute_same text_plain, right
|
152
148
|
assert_equal text_plain, right
|
153
149
|
end
|
154
150
|
|
155
|
-
it
|
151
|
+
it "correctly compares types that sort earlier" do
|
156
152
|
refute_equal text_html, text_plain
|
157
153
|
assert_operator text_html, :<, text_plain
|
158
154
|
end
|
159
155
|
|
160
|
-
it
|
156
|
+
it "correctly compares types that sort later" do
|
161
157
|
refute_equal text_plain, text_html
|
162
158
|
assert_operator text_plain, :>, text_html
|
163
159
|
end
|
164
160
|
|
165
|
-
it
|
166
|
-
assert_equal text_plain,
|
161
|
+
it "correctly compares types against equivalent strings" do
|
162
|
+
assert_equal text_plain, "text/plain"
|
167
163
|
end
|
168
164
|
|
169
|
-
it
|
170
|
-
refute_equal text_html,
|
171
|
-
assert_operator text_html, :<,
|
165
|
+
it "correctly compares types against strings that sort earlier" do
|
166
|
+
refute_equal text_html, "text/plain"
|
167
|
+
assert_operator text_html, :<, "text/plain"
|
172
168
|
end
|
173
169
|
|
174
|
-
it
|
175
|
-
refute_equal text_plain,
|
176
|
-
assert_operator text_plain, :>,
|
170
|
+
it "correctly compares types against strings that sort later" do
|
171
|
+
refute_equal text_plain, "text/html"
|
172
|
+
assert_operator text_plain, :>, "text/html"
|
177
173
|
end
|
178
174
|
|
179
|
-
it
|
175
|
+
it "correctly compares against nil" do
|
180
176
|
refute_equal text_html, nil
|
181
177
|
assert_operator text_plain, :<, nil
|
182
178
|
end
|
183
179
|
end
|
184
180
|
|
185
|
-
describe
|
186
|
-
it
|
181
|
+
describe "#ascii?" do
|
182
|
+
it "defaults to true for text/* types" do
|
187
183
|
assert text_plain.ascii?
|
188
184
|
end
|
189
185
|
|
190
|
-
it
|
186
|
+
it "defaults to false for non-text/* types" do
|
191
187
|
refute image_jpeg.ascii?
|
192
188
|
end
|
193
189
|
end
|
194
190
|
|
195
|
-
describe
|
196
|
-
it
|
191
|
+
describe "#binary?" do
|
192
|
+
it "defaults to false for text/* types" do
|
197
193
|
refute text_plain.binary?
|
198
194
|
end
|
199
195
|
|
200
|
-
it
|
196
|
+
it "defaults to true for non-text/* types" do
|
201
197
|
assert image_jpeg.binary?
|
202
198
|
end
|
203
199
|
end
|
204
200
|
|
205
|
-
describe
|
206
|
-
it
|
201
|
+
describe "#complete?" do
|
202
|
+
it "is true when there are extensions" do
|
207
203
|
assert text_x_yaml.complete?
|
208
204
|
end
|
209
205
|
|
210
|
-
it
|
211
|
-
refute mime_type(
|
206
|
+
it "is false when there are no extensions" do
|
207
|
+
refute mime_type("text/plain").complete?
|
212
208
|
end
|
213
209
|
end
|
214
210
|
|
215
|
-
describe
|
216
|
-
it
|
217
|
-
assert_equal
|
218
|
-
assert_equal
|
211
|
+
describe "#content_type" do
|
212
|
+
it "preserves the original case" do
|
213
|
+
assert_equal "text/plain", text_plain.content_type
|
214
|
+
assert_equal "text/vCard", mime_type("text/vCard").content_type
|
219
215
|
end
|
220
216
|
|
221
|
-
it
|
222
|
-
assert_equal
|
217
|
+
it "does not remove x- prefixes" do
|
218
|
+
assert_equal "x-appl/x-zip", x_appl_x_zip.content_type
|
223
219
|
end
|
224
220
|
end
|
225
221
|
|
226
|
-
describe
|
227
|
-
it
|
228
|
-
assert_equal
|
222
|
+
describe "#default_encoding" do
|
223
|
+
it "is quoted-printable for text/* types" do
|
224
|
+
assert_equal "quoted-printable", text_plain.default_encoding
|
229
225
|
end
|
230
226
|
|
231
|
-
it
|
232
|
-
assert_equal
|
227
|
+
it "is base64 for non-text/* types" do
|
228
|
+
assert_equal "base64", image_jpeg.default_encoding
|
233
229
|
end
|
234
230
|
end
|
235
231
|
|
236
|
-
describe
|
237
|
-
it
|
238
|
-
assert_equal
|
239
|
-
assert_equal
|
232
|
+
describe "#encoding, #encoding=" do
|
233
|
+
it "returns #default_encoding if not set explicitly" do
|
234
|
+
assert_equal "quoted-printable", text_plain.encoding
|
235
|
+
assert_equal "base64", image_jpeg.encoding
|
240
236
|
end
|
241
237
|
|
242
|
-
it
|
243
|
-
text_plain.encoding =
|
244
|
-
assert_equal
|
238
|
+
it "returns the set value when set" do
|
239
|
+
text_plain.encoding = "8bit"
|
240
|
+
assert_equal "8bit", text_plain.encoding
|
245
241
|
end
|
246
242
|
|
247
|
-
it
|
248
|
-
text_plain.encoding =
|
243
|
+
it "resets to the default encoding when set to nil or :default" do
|
244
|
+
text_plain.encoding = "8bit"
|
249
245
|
text_plain.encoding = nil
|
250
246
|
assert_equal text_plain.default_encoding, text_plain.encoding
|
251
247
|
text_plain.encoding = :default
|
252
248
|
assert_equal text_plain.default_encoding, text_plain.encoding
|
253
249
|
end
|
254
250
|
|
255
|
-
it
|
251
|
+
it "raises a MIME::Type::InvalidEncoding for an invalid encoding" do
|
256
252
|
exception = assert_raises MIME::Type::InvalidEncoding do
|
257
|
-
text_plain.encoding =
|
253
|
+
text_plain.encoding = "binary"
|
258
254
|
end
|
259
255
|
assert_equal 'Invalid Encoding "binary"', exception.to_s
|
260
256
|
end
|
261
257
|
end
|
262
258
|
|
263
|
-
describe
|
264
|
-
it
|
265
|
-
refute text_plain.eql?(
|
259
|
+
describe "#eql?" do
|
260
|
+
it "is not true for a non-MIME::Type" do
|
261
|
+
refute text_plain.eql?("text/plain")
|
266
262
|
end
|
267
263
|
|
268
|
-
it
|
264
|
+
it "is not true for a different MIME::Type" do
|
269
265
|
refute text_plain.eql?(image_jpeg)
|
270
266
|
end
|
271
267
|
|
272
|
-
it
|
273
|
-
assert text_plain, mime_type(
|
268
|
+
it "is true for an equivalent MIME::Type" do
|
269
|
+
assert text_plain, mime_type("text/Plain")
|
274
270
|
end
|
275
271
|
end
|
276
272
|
|
277
|
-
describe
|
278
|
-
it
|
279
|
-
assert_equal %w
|
280
|
-
assert_equal %w
|
273
|
+
describe "#extensions, #extensions=" do
|
274
|
+
it "returns an array of extensions" do
|
275
|
+
assert_equal %w[yaml yml], text_x_yaml.extensions
|
276
|
+
assert_equal %w[zip zp], x_appl_x_zip.extensions
|
281
277
|
end
|
282
278
|
|
283
|
-
it
|
284
|
-
text_x_yaml.extensions =
|
285
|
-
assert_equal %w
|
279
|
+
it "sets a single extension when provided a single value" do
|
280
|
+
text_x_yaml.extensions = "yaml"
|
281
|
+
assert_equal %w[yaml], text_x_yaml.extensions
|
286
282
|
end
|
287
283
|
|
288
|
-
it
|
289
|
-
text_x_yaml.extensions = %w
|
290
|
-
assert_equal %w
|
284
|
+
it "deduplicates extensions" do
|
285
|
+
text_x_yaml.extensions = %w[yaml yaml]
|
286
|
+
assert_equal %w[yaml], text_x_yaml.extensions
|
291
287
|
end
|
292
288
|
end
|
293
289
|
|
294
|
-
describe
|
295
|
-
it
|
290
|
+
describe "#add_extensions" do
|
291
|
+
it "does not modify extensions when provided nil" do
|
296
292
|
text_x_yaml.add_extensions(nil)
|
297
|
-
assert_equal %w
|
293
|
+
assert_equal %w[yaml yml], text_x_yaml.extensions
|
298
294
|
end
|
299
295
|
|
300
|
-
it
|
301
|
-
text_x_yaml.add_extensions(
|
302
|
-
assert_equal %w
|
303
|
-
text_x_yaml.add_extensions(%w
|
304
|
-
assert_equal %w
|
296
|
+
it "remains deduplicated with duplicate values" do
|
297
|
+
text_x_yaml.add_extensions("yaml")
|
298
|
+
assert_equal %w[yaml yml], text_x_yaml.extensions
|
299
|
+
text_x_yaml.add_extensions(%w[yaml yz])
|
300
|
+
assert_equal %w[yaml yml yz], text_x_yaml.extensions
|
305
301
|
end
|
306
302
|
end
|
307
303
|
|
308
|
-
describe
|
304
|
+
describe "#priority_compare" do
|
309
305
|
def assert_priority_less(left, right)
|
310
306
|
assert_equal(-1, left.priority_compare(right))
|
311
307
|
end
|
@@ -324,15 +320,15 @@ describe MIME::Type do
|
|
324
320
|
assert_priority_more right, left
|
325
321
|
end
|
326
322
|
|
327
|
-
let(:text_1) { mime_type(
|
328
|
-
let(:text_1p) { mime_type(
|
329
|
-
let(:text_2) { mime_type(
|
323
|
+
let(:text_1) { mime_type("text/1") }
|
324
|
+
let(:text_1p) { mime_type("text/1") }
|
325
|
+
let(:text_2) { mime_type("text/2") }
|
330
326
|
|
331
|
-
it
|
327
|
+
it "sorts (1) based on the simplified type" do
|
332
328
|
assert_priority text_1, text_1p, text_2
|
333
329
|
end
|
334
330
|
|
335
|
-
it
|
331
|
+
it "sorts (2) based on extensions" do
|
336
332
|
text_1.extensions = ["foo", "bar"]
|
337
333
|
text_2.extensions = ["foo"]
|
338
334
|
|
@@ -343,271 +339,282 @@ describe MIME::Type do
|
|
343
339
|
assert_priority_more text_1, text_2
|
344
340
|
end
|
345
341
|
|
346
|
-
it
|
342
|
+
it "sorts (3) based on the registration state" do
|
347
343
|
text_1.registered = text_1p.registered = true
|
348
344
|
text_1b = mime_type(text_1) { |t| t.registered = false }
|
349
345
|
|
350
346
|
assert_priority text_1, text_1p, text_1b
|
351
347
|
end
|
352
348
|
|
353
|
-
it
|
354
|
-
text_1.extensions = text_1p.extensions =
|
349
|
+
it "sorts (4) based on the completeness" do
|
350
|
+
text_1.extensions = text_1p.extensions = "1"
|
355
351
|
text_1b = mime_type(text_1) { |t| t.extensions = nil }
|
356
352
|
|
357
353
|
assert_priority text_1, text_1p, text_1b
|
358
354
|
end
|
359
355
|
|
360
|
-
it
|
356
|
+
it "sorts (5) based on obsolete status" do
|
361
357
|
text_1.obsolete = text_1p.obsolete = false
|
362
358
|
text_1b = mime_type(text_1) { |t| t.obsolete = true }
|
363
359
|
|
364
360
|
assert_priority text_1, text_1p, text_1b
|
365
361
|
end
|
366
362
|
|
367
|
-
it
|
363
|
+
it "sorts (5) based on the use-instead value" do
|
368
364
|
text_1.obsolete = text_1p.obsolete = true
|
369
|
-
text_1.use_instead = text_1p.use_instead =
|
365
|
+
text_1.use_instead = text_1p.use_instead = "abc/xyz"
|
370
366
|
text_1b = mime_type(text_1) { |t| t.use_instead = nil }
|
371
367
|
|
372
368
|
assert_priority text_1, text_1p, text_1b
|
373
369
|
|
374
|
-
text_1b.use_instead =
|
370
|
+
text_1b.use_instead = "abc/zzz"
|
375
371
|
|
376
372
|
assert_priority text_1, text_1p, text_1b
|
377
373
|
end
|
378
374
|
end
|
379
375
|
|
380
|
-
describe
|
381
|
-
it
|
382
|
-
assert_equal
|
376
|
+
describe "#raw_media_type" do
|
377
|
+
it "extracts the media type as case-preserved" do
|
378
|
+
assert_equal "Text", mime_type("Text/plain").raw_media_type
|
383
379
|
end
|
384
380
|
|
385
|
-
it
|
386
|
-
assert_equal(
|
381
|
+
it "does not remove x- prefixes" do
|
382
|
+
assert_equal("x-appl", x_appl_x_zip.raw_media_type)
|
387
383
|
end
|
388
384
|
end
|
389
385
|
|
390
|
-
describe
|
391
|
-
it
|
392
|
-
assert_equal
|
386
|
+
describe "#media_type" do
|
387
|
+
it "extracts the media type as lowercase" do
|
388
|
+
assert_equal "text", text_plain.media_type
|
393
389
|
end
|
394
390
|
|
395
|
-
it
|
396
|
-
assert_equal(
|
391
|
+
it "does not remove x- prefixes" do
|
392
|
+
assert_equal("x-appl", x_appl_x_zip.media_type)
|
397
393
|
end
|
398
394
|
end
|
399
395
|
|
400
|
-
describe
|
401
|
-
it
|
402
|
-
assert_equal
|
396
|
+
describe "#raw_media_type" do
|
397
|
+
it "extracts the media type as case-preserved" do
|
398
|
+
assert_equal "Text", mime_type("Text/plain").raw_media_type
|
403
399
|
end
|
404
400
|
|
405
|
-
it
|
406
|
-
assert_equal(
|
401
|
+
it "does not remove x- prefixes" do
|
402
|
+
assert_equal("x-appl", x_appl_x_zip.raw_media_type)
|
407
403
|
end
|
408
404
|
end
|
409
405
|
|
410
|
-
describe
|
411
|
-
it
|
412
|
-
assert_equal
|
406
|
+
describe "#sub_type" do
|
407
|
+
it "extracts the sub type as lowercase" do
|
408
|
+
assert_equal "plain", text_plain.sub_type
|
413
409
|
end
|
414
410
|
|
415
|
-
it
|
416
|
-
assert_equal(
|
411
|
+
it "does not remove x- prefixes" do
|
412
|
+
assert_equal("x-zip", x_appl_x_zip.sub_type)
|
417
413
|
end
|
418
414
|
end
|
419
415
|
|
420
|
-
describe
|
421
|
-
it
|
422
|
-
assert_equal
|
416
|
+
describe "#raw_sub_type" do
|
417
|
+
it "extracts the sub type as case-preserved" do
|
418
|
+
assert_equal "Plain", mime_type("text/Plain").raw_sub_type
|
423
419
|
end
|
424
420
|
|
425
|
-
it
|
426
|
-
assert_equal(
|
421
|
+
it "does not remove x- prefixes" do
|
422
|
+
assert_equal("x-zip", x_appl_x_zip.raw_sub_type)
|
427
423
|
end
|
428
424
|
end
|
429
425
|
|
430
|
-
describe
|
431
|
-
let(:t) { mime_type(
|
426
|
+
describe "#to_h" do
|
427
|
+
let(:t) { mime_type("a/b") }
|
432
428
|
|
433
|
-
it
|
434
|
-
assert_has_keys t.to_h, %w
|
429
|
+
it "has the required keys (content-type, registered, encoding)" do
|
430
|
+
assert_has_keys t.to_h, %w[content-type registered encoding]
|
435
431
|
end
|
436
432
|
|
437
|
-
it
|
438
|
-
assert_has_keys mime_type(t) { |v| v.docs =
|
433
|
+
it "has the docs key if there are documents" do
|
434
|
+
assert_has_keys mime_type(t) { |v| v.docs = "a" }.to_h, %w[docs]
|
439
435
|
end
|
440
436
|
|
441
|
-
it
|
442
|
-
assert_has_keys mime_type(t) { |v| v.extensions =
|
443
|
-
|
437
|
+
it "has the extensions key if set" do
|
438
|
+
assert_has_keys mime_type(t) { |v| v.extensions = "a" }.to_h,
|
439
|
+
"extensions"
|
444
440
|
end
|
445
441
|
|
446
|
-
it
|
447
|
-
assert_has_keys mime_type(t) { |v| v.preferred_extension =
|
448
|
-
|
442
|
+
it "has the preferred-extension key if set" do
|
443
|
+
assert_has_keys mime_type(t) { |v| v.preferred_extension = "a" }.to_h,
|
444
|
+
"preferred-extension"
|
449
445
|
end
|
450
446
|
|
451
|
-
it
|
452
|
-
assert_has_keys mime_type(t) { |v| v.obsolete = true }.to_h,
|
447
|
+
it "has the obsolete key if set" do
|
448
|
+
assert_has_keys mime_type(t) { |v| v.obsolete = true }.to_h, "obsolete"
|
453
449
|
end
|
454
450
|
|
455
|
-
it
|
451
|
+
it "has the obsolete and use-instead keys if set" do
|
456
452
|
assert_has_keys mime_type(t) { |v|
|
457
453
|
v.obsolete = true
|
458
|
-
v.use_instead =
|
459
|
-
}.to_h, %w
|
454
|
+
v.use_instead = "c/d"
|
455
|
+
}.to_h, %w[obsolete use-instead]
|
460
456
|
end
|
461
457
|
|
462
|
-
it
|
463
|
-
assert_has_keys mime_type(t) { |v| v.signature = true }.to_h,
|
458
|
+
it "has the signature key if set" do
|
459
|
+
assert_has_keys mime_type(t) { |v| v.signature = true }.to_h, "signature"
|
464
460
|
end
|
465
461
|
end
|
466
462
|
|
467
|
-
describe
|
468
|
-
let(:
|
463
|
+
describe "#to_json" do
|
464
|
+
let(:expected_1) {
|
469
465
|
'{"content-type":"a/b","encoding":"base64","registered":false}'
|
470
466
|
}
|
467
|
+
let(:expected_2) {
|
468
|
+
'{"content-type":"a/b","encoding":"base64","registered":true,"provisional":true}'
|
469
|
+
}
|
471
470
|
|
472
|
-
it
|
473
|
-
assert_equal
|
471
|
+
it "converts to JSON when requested" do
|
472
|
+
assert_equal expected_1, mime_type("a/b").to_json
|
473
|
+
end
|
474
|
+
|
475
|
+
it "converts to JSON with provisional when requested" do
|
476
|
+
type = mime_type("a/b") do |t|
|
477
|
+
t.registered = true
|
478
|
+
t.provisional = true
|
479
|
+
end
|
480
|
+
assert_equal expected_2, type.to_json
|
474
481
|
end
|
475
482
|
end
|
476
483
|
|
477
|
-
describe
|
478
|
-
it
|
479
|
-
assert_equal
|
484
|
+
describe "#to_s, #to_str" do
|
485
|
+
it "represents itself as a string of the canonical content_type" do
|
486
|
+
assert_equal "text/plain", text_plain.to_s
|
480
487
|
end
|
481
488
|
|
482
|
-
it
|
483
|
-
assert_equal text_plain,
|
489
|
+
it "acts like a string of the canonical content_type for comparison" do
|
490
|
+
assert_equal text_plain, "text/plain"
|
484
491
|
end
|
485
492
|
|
486
|
-
it
|
487
|
-
assert_equal
|
493
|
+
it "acts like a string for other purposes" do
|
494
|
+
assert_equal "stringy", "text/plain".sub(text_plain, "stringy")
|
488
495
|
end
|
489
496
|
end
|
490
497
|
|
491
|
-
describe
|
498
|
+
describe "#xrefs, #xrefs=" do
|
492
499
|
let(:expected) {
|
493
|
-
MIME::Types::Container.new(
|
500
|
+
MIME::Types::Container.new("rfc" => Set["rfc1234", "rfc5678"])
|
494
501
|
}
|
495
502
|
|
496
|
-
it
|
503
|
+
it "returns the expected results" do
|
497
504
|
application_javascript.xrefs = {
|
498
|
-
|
505
|
+
"rfc" => %w[rfc5678 rfc1234 rfc1234]
|
499
506
|
}
|
500
507
|
|
501
508
|
assert_equal expected, application_javascript.xrefs
|
502
509
|
end
|
503
510
|
end
|
504
511
|
|
505
|
-
describe
|
512
|
+
describe "#xref_urls" do
|
506
513
|
let(:expected) {
|
507
514
|
[
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
+
"http://www.iana.org/go/draft1",
|
516
|
+
"http://www.iana.org/assignments/media-types/a/b",
|
517
|
+
"http://www.iana.org/assignments/media-types/media-types.xhtml#p-1",
|
518
|
+
"http://www.iana.org/go/rfc-1",
|
519
|
+
"http://www.rfc-editor.org/errata_search.php?eid=err-1",
|
520
|
+
"http://example.org",
|
521
|
+
"text"
|
515
522
|
]
|
516
523
|
}
|
517
524
|
|
518
525
|
let(:type) {
|
519
|
-
mime_type(
|
526
|
+
mime_type("a/b").tap do |t|
|
520
527
|
t.xrefs = {
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
+
"draft" => ["RFC1"],
|
529
|
+
"template" => ["a/b"],
|
530
|
+
"person" => ["p-1"],
|
531
|
+
"rfc" => ["rfc-1"],
|
532
|
+
"rfc-errata" => ["err-1"],
|
533
|
+
"uri" => ["http://example.org"],
|
534
|
+
"text" => ["text"]
|
528
535
|
}
|
529
536
|
end
|
530
537
|
}
|
531
538
|
|
532
|
-
it
|
539
|
+
it "translates according to given rules" do
|
533
540
|
assert_equal expected, type.xref_urls
|
534
541
|
end
|
535
542
|
end
|
536
543
|
|
537
|
-
describe
|
538
|
-
it
|
544
|
+
describe "#use_instead" do
|
545
|
+
it "is nil unless the type is obsolete" do
|
539
546
|
assert_nil text_plain.use_instead
|
540
547
|
end
|
541
548
|
|
542
|
-
it
|
549
|
+
it "is nil if not set and the type is obsolete" do
|
543
550
|
text_plain.obsolete = true
|
544
551
|
assert_nil text_plain.use_instead
|
545
552
|
end
|
546
553
|
|
547
|
-
it
|
554
|
+
it "is a different type if set and the type is obsolete" do
|
548
555
|
text_plain.obsolete = true
|
549
|
-
text_plain.use_instead =
|
550
|
-
assert_equal
|
556
|
+
text_plain.use_instead = "text/html"
|
557
|
+
assert_equal "text/html", text_plain.use_instead
|
551
558
|
end
|
552
559
|
end
|
553
560
|
|
554
|
-
describe
|
555
|
-
it
|
561
|
+
describe "#preferred_extension, #preferred_extension=" do
|
562
|
+
it "is nil when not set and there are no extensions" do
|
556
563
|
assert_nil text_plain.preferred_extension
|
557
564
|
end
|
558
565
|
|
559
|
-
it
|
560
|
-
assert_equal
|
566
|
+
it "is the first extension when not set but there are extensions" do
|
567
|
+
assert_equal "yaml", text_x_yaml.preferred_extension
|
561
568
|
end
|
562
569
|
|
563
|
-
it
|
564
|
-
text_x_yaml.preferred_extension =
|
565
|
-
assert_equal
|
570
|
+
it "is the extension provided when set" do
|
571
|
+
text_x_yaml.preferred_extension = "yml"
|
572
|
+
assert_equal "yml", text_x_yaml.preferred_extension
|
566
573
|
end
|
567
574
|
|
568
|
-
it
|
569
|
-
text_x_yaml.preferred_extension =
|
570
|
-
assert_equal
|
571
|
-
assert_includes text_x_yaml.extensions,
|
575
|
+
it "is adds the preferred extension if it does not exist" do
|
576
|
+
text_x_yaml.preferred_extension = "yz"
|
577
|
+
assert_equal "yz", text_x_yaml.preferred_extension
|
578
|
+
assert_includes text_x_yaml.extensions, "yz"
|
572
579
|
end
|
573
580
|
end
|
574
581
|
|
575
|
-
describe
|
576
|
-
it
|
577
|
-
assert_equal
|
582
|
+
describe "#friendly" do
|
583
|
+
it "returns English by default" do
|
584
|
+
assert_equal "YAML Structured Document", text_x_yaml.friendly
|
578
585
|
end
|
579
586
|
|
580
|
-
it
|
581
|
-
assert_equal
|
582
|
-
assert_equal
|
587
|
+
it "returns English when requested" do
|
588
|
+
assert_equal "YAML Structured Document", text_x_yaml.friendly("en")
|
589
|
+
assert_equal "YAML Structured Document", text_x_yaml.friendly(:en)
|
583
590
|
end
|
584
591
|
|
585
|
-
it
|
586
|
-
assert_nil text_x_yaml.friendly(
|
592
|
+
it "returns nothing for an unknown language" do
|
593
|
+
assert_nil text_x_yaml.friendly("zz")
|
587
594
|
end
|
588
595
|
|
589
|
-
it
|
590
|
-
expected = {
|
591
|
-
assert_equal expected, text_plain.friendly([
|
592
|
-
expected.update(
|
596
|
+
it "merges new values from an array parameter" do
|
597
|
+
expected = {"en" => "Text files"}
|
598
|
+
assert_equal expected, text_plain.friendly(["en", "Text files"])
|
599
|
+
expected.update("fr" => "des fichiers texte")
|
593
600
|
assert_equal expected,
|
594
|
-
text_plain.friendly([
|
601
|
+
text_plain.friendly(["fr", "des fichiers texte"])
|
595
602
|
end
|
596
603
|
|
597
|
-
it
|
598
|
-
expected = {
|
604
|
+
it "merges new values from a hash parameter" do
|
605
|
+
expected = {"en" => "Text files"}
|
599
606
|
assert_equal expected, text_plain.friendly(expected)
|
600
|
-
french = {
|
607
|
+
french = {"fr" => "des fichiers texte"}
|
601
608
|
expected.update(french)
|
602
609
|
assert_equal expected, text_plain.friendly(french)
|
603
610
|
end
|
604
611
|
|
605
|
-
it
|
612
|
+
it "raises an ArgumentError if an unknown value is provided" do
|
606
613
|
exception = assert_raises ArgumentError do
|
607
614
|
text_plain.friendly(1)
|
608
615
|
end
|
609
616
|
|
610
|
-
assert_equal
|
617
|
+
assert_equal "Expected a language or translation set, not 1",
|
611
618
|
exception.message
|
612
619
|
end
|
613
620
|
end
|