1
|
'use strict';
|
2
|
|
3
|
var mime = require('..');
|
4
|
var mimeTypes = require('../node_modules/mime-types');
|
5
|
var assert = require('assert');
|
6
|
var chalk = require('chalk');
|
7
|
|
8
|
describe('class Mime', function() {
|
9
|
it('mime and mime/lite coexist', function() {
|
10
|
assert.doesNotThrow(function() {
|
11
|
require('../lite');
|
12
|
});
|
13
|
});
|
14
|
|
15
|
it('new constructor()', function() {
|
16
|
var Mime = require('../Mime');
|
17
|
|
18
|
var mime = new Mime(
|
19
|
{'text/a': ['a', 'a1']},
|
20
|
{'text/b': ['b', 'b1']}
|
21
|
);
|
22
|
|
23
|
assert.deepEqual(mime._types, {
|
24
|
a: 'text/a',
|
25
|
a1: 'text/a',
|
26
|
b: 'text/b',
|
27
|
b1: 'text/b',
|
28
|
});
|
29
|
|
30
|
assert.deepEqual(mime._extensions, {
|
31
|
'text/a': 'a',
|
32
|
'text/b': 'b',
|
33
|
});
|
34
|
});
|
35
|
|
36
|
it('define()', function() {
|
37
|
var Mime = require('../Mime');
|
38
|
|
39
|
var mime = new Mime({'text/a': ['a']}, {'text/b': ['b']});
|
40
|
|
41
|
assert.throws(function() {
|
42
|
mime.define({'text/c': ['b']});
|
43
|
});
|
44
|
|
45
|
assert.doesNotThrow(function() {
|
46
|
mime.define({'text/c': ['b']}, true);
|
47
|
});
|
48
|
|
49
|
assert.deepEqual(mime._types, {
|
50
|
a: 'text/a',
|
51
|
b: 'text/c',
|
52
|
});
|
53
|
|
54
|
assert.deepEqual(mime._extensions, {
|
55
|
'text/a': 'a',
|
56
|
'text/b': 'b',
|
57
|
'text/c': 'b',
|
58
|
});
|
59
|
});
|
60
|
|
61
|
it('define() *\'ed types', function() {
|
62
|
var Mime = require('../Mime');
|
63
|
|
64
|
var mime = new Mime(
|
65
|
{'text/a': ['*b']},
|
66
|
{'text/b': ['b']}
|
67
|
);
|
68
|
|
69
|
assert.deepEqual(mime._types, {
|
70
|
b: 'text/b',
|
71
|
});
|
72
|
|
73
|
assert.deepEqual(mime._extensions, {
|
74
|
'text/a': 'b',
|
75
|
'text/b': 'b',
|
76
|
});
|
77
|
});
|
78
|
|
79
|
it ('case-insensitive', function() {
|
80
|
var Mime = require('../Mime');
|
81
|
const mime = new Mime({
|
82
|
'TEXT/UPPER': ['UP'],
|
83
|
'text/lower': ['low'],
|
84
|
});
|
85
|
|
86
|
assert.equal(mime.getType('test.up'), 'text/upper');
|
87
|
assert.equal(mime.getType('test.UP'), 'text/upper');
|
88
|
assert.equal(mime.getType('test.low'), 'text/lower');
|
89
|
assert.equal(mime.getType('test.LOW'), 'text/lower');
|
90
|
|
91
|
assert.equal(mime.getExtension('text/upper'), 'up');
|
92
|
assert.equal(mime.getExtension('text/lower'), 'low');
|
93
|
assert.equal(mime.getExtension('TEXT/UPPER'), 'up');
|
94
|
assert.equal(mime.getExtension('TEXT/LOWER'), 'low');
|
95
|
});
|
96
|
|
97
|
it('getType()', function() {
|
98
|
// Upper/lower case
|
99
|
assert.equal(mime.getType('text.txt'), 'text/plain');
|
100
|
assert.equal(mime.getType('TEXT.TXT'), 'text/plain');
|
101
|
|
102
|
// Bare extension
|
103
|
assert.equal(mime.getType('txt'), 'text/plain');
|
104
|
assert.equal(mime.getType('.txt'), 'text/plain');
|
105
|
assert.strictEqual(mime.getType('.bogus'), null);
|
106
|
assert.strictEqual(mime.getType('bogus'), null);
|
107
|
|
108
|
// Non-sensical
|
109
|
assert.strictEqual(mime.getType(null), null);
|
110
|
assert.strictEqual(mime.getType(undefined), null);
|
111
|
assert.strictEqual(mime.getType(42), null);
|
112
|
assert.strictEqual(mime.getType({}), null);
|
113
|
|
114
|
// File paths
|
115
|
assert.equal(mime.getType('dir/text.txt'), 'text/plain');
|
116
|
assert.equal(mime.getType('dir\\text.txt'), 'text/plain');
|
117
|
assert.equal(mime.getType('.text.txt'), 'text/plain');
|
118
|
assert.equal(mime.getType('.txt'), 'text/plain');
|
119
|
assert.equal(mime.getType('txt'), 'text/plain');
|
120
|
assert.equal(mime.getType('/path/to/page.html'), 'text/html');
|
121
|
assert.equal(mime.getType('c:\\path\\to\\page.html'), 'text/html');
|
122
|
assert.equal(mime.getType('page.html'), 'text/html');
|
123
|
assert.equal(mime.getType('path/to/page.html'), 'text/html');
|
124
|
assert.equal(mime.getType('path\\to\\page.html'), 'text/html');
|
125
|
assert.strictEqual(mime.getType('/txt'), null);
|
126
|
assert.strictEqual(mime.getType('\\txt'), null);
|
127
|
assert.strictEqual(mime.getType('text.nope'), null);
|
128
|
assert.strictEqual(mime.getType('/path/to/file.bogus'), null);
|
129
|
assert.strictEqual(mime.getType('/path/to/json'), null);
|
130
|
assert.strictEqual(mime.getType('/path/to/.json'), null);
|
131
|
assert.strictEqual(mime.getType('/path/to/.config.json'), 'application/json');
|
132
|
assert.strictEqual(mime.getType('.config.json'), 'application/json');
|
133
|
});
|
134
|
|
135
|
it('getExtension()', function() {
|
136
|
assert.equal(mime.getExtension('text/html'), 'html');
|
137
|
assert.equal(mime.getExtension(' text/html'), 'html');
|
138
|
assert.equal(mime.getExtension('text/html '), 'html');
|
139
|
assert.strictEqual(mime.getExtension('application/x-bogus'), null);
|
140
|
assert.strictEqual(mime.getExtension('bogus'), null);
|
141
|
assert.strictEqual(mime.getExtension(null), null);
|
142
|
assert.strictEqual(mime.getExtension(undefined), null);
|
143
|
assert.strictEqual(mime.getExtension(42), null);
|
144
|
assert.strictEqual(mime.getExtension({}), null);
|
145
|
});
|
146
|
});
|
147
|
|
148
|
describe('DB', function() {
|
149
|
var diffs = [];
|
150
|
|
151
|
after(function() {
|
152
|
if (diffs.length) {
|
153
|
console.log('\n[INFO] The following inconsistencies with MDN (https://goo.gl/lHrFU6) and/or mime-types (https://github.com/jshttp/mime-types) are expected:');
|
154
|
diffs.forEach(function(d) {
|
155
|
console.warn(
|
156
|
' ' + d[0]+ '[' + chalk.blue(d[1]) + '] = ' + chalk.red(d[2]) +
|
157
|
', mime[' + d[1] + '] = ' + chalk.green(d[3])
|
158
|
);
|
159
|
});
|
160
|
}
|
161
|
});
|
162
|
|
163
|
it('Consistency', function() {
|
164
|
for (var ext in this.types) {
|
165
|
assert.equal(ext, this.extensions[this.types[ext]], '${ext} does not have consistent ext->type->ext mapping');
|
166
|
}
|
167
|
});
|
168
|
|
169
|
it('MDN types', function() {
|
170
|
// MDN types listed at https://goo.gl/lHrFU6
|
171
|
var MDN = {
|
172
|
aac: 'audio/aac',
|
173
|
bin: 'application/octet-stream',
|
174
|
css: 'text/css',
|
175
|
csv: 'text/csv',
|
176
|
doc: 'application/msword',
|
177
|
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
178
|
gif: 'image/gif',
|
179
|
html: 'text/html',
|
180
|
ico: 'image/vnd.microsoft.icon',
|
181
|
jpg: 'image/jpeg',
|
182
|
js: 'application/javascript',
|
183
|
json: 'application/json',
|
184
|
midi: 'audio/midi',
|
185
|
mjs: 'application/javascript',
|
186
|
mp3: 'audio/mpeg',
|
187
|
mpeg: 'video/mpeg',
|
188
|
oga: 'audio/ogg',
|
189
|
ogv: 'video/ogg',
|
190
|
otf: 'font/otf',
|
191
|
png: 'image/png',
|
192
|
pdf: 'application/pdf',
|
193
|
rtf: 'application/rtf',
|
194
|
svg: 'image/svg+xml',
|
195
|
swf: 'application/x-shockwave-flash',
|
196
|
tiff: 'image/tiff',
|
197
|
ttf: 'font/ttf',
|
198
|
txt: 'text/plain',
|
199
|
wav: 'audio/wav',
|
200
|
weba: 'audio/webm',
|
201
|
webm: 'video/webm',
|
202
|
webp: 'image/webp',
|
203
|
woff: 'font/woff',
|
204
|
xls: 'application/vnd.ms-excel',
|
205
|
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
206
|
xml: 'application/xml',
|
207
|
zip: 'application/zip',
|
208
|
'3gp': 'video/3gpp',
|
209
|
};
|
210
|
|
211
|
for (var ext in MDN) {
|
212
|
var expected = MDN[ext];
|
213
|
var actual = mime.getType(ext);
|
214
|
if (actual !== expected) diffs.push(['MDN', ext, expected, actual]);
|
215
|
}
|
216
|
|
217
|
for (var ext in mimeTypes.types) {
|
218
|
var expected = mimeTypes.types[ext];
|
219
|
var actual = mime.getType(ext);
|
220
|
if (actual !== expected) diffs.push(['mime-types', ext, expected, actual]);
|
221
|
}
|
222
|
});
|
223
|
|
224
|
it('Specific types', function() {
|
225
|
// Assortment of types we sanity check for good measure
|
226
|
assert.equal(mime.getType('html'), 'text/html');
|
227
|
assert.equal(mime.getType('js'), 'application/javascript');
|
228
|
assert.equal(mime.getType('json'), 'application/json');
|
229
|
assert.equal(mime.getType('rtf'), 'application/rtf');
|
230
|
assert.equal(mime.getType('txt'), 'text/plain');
|
231
|
assert.equal(mime.getType('xml'), 'application/xml');
|
232
|
|
233
|
assert.equal(mime.getType('wasm'), 'application/wasm');
|
234
|
});
|
235
|
|
236
|
it('Specific extensions', function() {
|
237
|
assert.equal(mime.getExtension('text/html;charset=UTF-8'), 'html');
|
238
|
assert.equal(mime.getExtension('text/HTML; charset=UTF-8'), 'html');
|
239
|
assert.equal(mime.getExtension('text/html; charset=UTF-8'), 'html');
|
240
|
assert.equal(mime.getExtension('text/html; charset=UTF-8 '), 'html');
|
241
|
assert.equal(mime.getExtension('text/html ; charset=UTF-8'), 'html');
|
242
|
assert.equal(mime.getExtension(mime._types.text), 'txt');
|
243
|
assert.equal(mime.getExtension(mime._types.htm), 'html');
|
244
|
assert.equal(mime.getExtension('application/octet-stream'), 'bin');
|
245
|
assert.equal(mime.getExtension('application/octet-stream '), 'bin');
|
246
|
assert.equal(mime.getExtension(' text/html; charset=UTF-8'), 'html');
|
247
|
assert.equal(mime.getExtension('text/html; charset=UTF-8 '), 'html');
|
248
|
assert.equal(mime.getExtension('text/html; charset=UTF-8'), 'html');
|
249
|
assert.equal(mime.getExtension('text/html ; charset=UTF-8'), 'html');
|
250
|
assert.equal(mime.getExtension('text/html;charset=UTF-8'), 'html');
|
251
|
assert.equal(mime.getExtension('text/Html;charset=UTF-8'), 'html');
|
252
|
assert.equal(mime.getExtension('unrecognized'), null);
|
253
|
|
254
|
assert.equal(mime.getExtension('text/xml'), 'xml'); // See #180
|
255
|
});
|
256
|
});
|