-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathtest.js
More file actions
338 lines (274 loc) · 12.4 KB
/
test.js
File metadata and controls
338 lines (274 loc) · 12.4 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
var assert = require('assert')
var mimeTypes = require('..')
const extname = require('../extname')
describe('mimeTypes', function () {
describe('.charset(type)', function () {
it('should return "UTF-8" for "application/json"', function () {
assert.strictEqual(mimeTypes.charset('application/json'), 'UTF-8')
})
it('should return "UTF-8" for "application/json; foo=bar"', function () {
assert.strictEqual(mimeTypes.charset('application/json; foo=bar'), 'UTF-8')
})
it('should return "UTF-8" for "application/javascript"', function () {
assert.strictEqual(mimeTypes.charset('application/javascript'), 'UTF-8')
})
it('should return "UTF-8" for "application/JavaScript"', function () {
assert.strictEqual(mimeTypes.charset('application/JavaScript'), 'UTF-8')
})
it('should return "UTF-8" for "text/html"', function () {
assert.strictEqual(mimeTypes.charset('text/html'), 'UTF-8')
})
it('should return "UTF-8" for "TEXT/HTML"', function () {
assert.strictEqual(mimeTypes.charset('TEXT/HTML'), 'UTF-8')
})
it('should return "UTF-8" for any text/*', function () {
assert.strictEqual(mimeTypes.charset('text/x-bogus'), 'UTF-8')
})
it('should return false for unknown types', function () {
assert.strictEqual(mimeTypes.charset('application/x-bogus'), false)
})
it('should return false for any application/octet-stream', function () {
assert.strictEqual(mimeTypes.charset('application/octet-stream'), false)
})
it('should return false for invalid arguments', function () {
assert.strictEqual(mimeTypes.charset({}), false)
assert.strictEqual(mimeTypes.charset(null), false)
assert.strictEqual(mimeTypes.charset(true), false)
assert.strictEqual(mimeTypes.charset(42), false)
})
})
describe('.contentType(extension)', function () {
it('should return content-type for "html"', function () {
assert.strictEqual(mimeTypes.contentType('html'), 'text/html; charset=utf-8')
})
it('should return content-type for ".html"', function () {
assert.strictEqual(mimeTypes.contentType('.html'), 'text/html; charset=utf-8')
})
it('should return content-type for "jade"', function () {
assert.strictEqual(mimeTypes.contentType('jade'), 'text/jade; charset=utf-8')
})
it('should return content-type for "json"', function () {
assert.strictEqual(mimeTypes.contentType('json'), 'application/json; charset=utf-8')
})
it('should return false for unknown extensions', function () {
assert.strictEqual(mimeTypes.contentType('bogus'), false)
})
it('should return false for invalid arguments', function () {
assert.strictEqual(mimeTypes.contentType({}), false)
assert.strictEqual(mimeTypes.contentType(null), false)
assert.strictEqual(mimeTypes.contentType(true), false)
assert.strictEqual(mimeTypes.contentType(42), false)
})
})
describe('.contentType(type)', function () {
it('should attach charset to "application/json"', function () {
assert.strictEqual(mimeTypes.contentType('application/json'), 'application/json; charset=utf-8')
})
it('should attach charset to "application/json; foo=bar"', function () {
assert.strictEqual(mimeTypes.contentType('application/json; foo=bar'), 'application/json; foo=bar; charset=utf-8')
})
it('should attach charset to "TEXT/HTML"', function () {
assert.strictEqual(mimeTypes.contentType('TEXT/HTML'), 'TEXT/HTML; charset=utf-8')
})
it('should attach charset to "text/html"', function () {
assert.strictEqual(mimeTypes.contentType('text/html'), 'text/html; charset=utf-8')
})
it('should not alter "text/html; charset=iso-8859-1"', function () {
assert.strictEqual(mimeTypes.contentType('text/html; charset=iso-8859-1'), 'text/html; charset=iso-8859-1')
})
it('should return type for unknown types', function () {
assert.strictEqual(mimeTypes.contentType('application/x-bogus'), 'application/x-bogus')
})
})
describe('.extension(type)', function () {
it('should return extension for mime type', function () {
assert.strictEqual(mimeTypes.extension('text/html'), 'html')
assert.strictEqual(mimeTypes.extension(' text/html'), 'html')
assert.strictEqual(mimeTypes.extension('text/html '), 'html')
})
it('should return false for unknown type', function () {
assert.strictEqual(mimeTypes.extension('application/x-bogus'), false)
})
it('should return false for non-type string', function () {
assert.strictEqual(mimeTypes.extension('bogus'), false)
})
it('should return false for non-strings', function () {
assert.strictEqual(mimeTypes.extension(null), false)
assert.strictEqual(mimeTypes.extension(undefined), false)
assert.strictEqual(mimeTypes.extension(42), false)
assert.strictEqual(mimeTypes.extension({}), false)
})
it('should return extension for mime type with parameters', function () {
assert.strictEqual(mimeTypes.extension('text/html;charset=UTF-8'), 'html')
assert.strictEqual(mimeTypes.extension('text/HTML; charset=UTF-8'), 'html')
assert.strictEqual(mimeTypes.extension('text/html; charset=UTF-8'), 'html')
assert.strictEqual(mimeTypes.extension('text/html; charset=UTF-8 '), 'html')
assert.strictEqual(mimeTypes.extension('text/html ; charset=UTF-8'), 'html')
})
})
describe('.lookup(extension)', function () {
it('should return mime type for ".html"', function () {
assert.strictEqual(mimeTypes.lookup('.html'), 'text/html')
})
it('should return mime type for ".js"', function () {
assert.strictEqual(mimeTypes.lookup('.js'), 'text/javascript')
})
it('should return mime type for ".json"', function () {
assert.strictEqual(mimeTypes.lookup('.json'), 'application/json')
})
it('should return mime type for ".rtf"', function () {
assert.strictEqual(mimeTypes.lookup('.rtf'), 'application/rtf')
})
it('should return mime type for ".txt"', function () {
assert.strictEqual(mimeTypes.lookup('.txt'), 'text/plain')
})
it('should return mime type for ".xml"', function () {
assert.strictEqual(mimeTypes.lookup('.xml'), 'application/xml')
})
it('should work without the leading dot', function () {
assert.strictEqual(mimeTypes.lookup('html'), 'text/html')
assert.strictEqual(mimeTypes.lookup('xml'), 'application/xml')
})
it('should be case insensitive', function () {
assert.strictEqual(mimeTypes.lookup('HTML'), 'text/html')
assert.strictEqual(mimeTypes.lookup('.Xml'), 'application/xml')
})
it('should return false for unknown extension', function () {
assert.strictEqual(mimeTypes.lookup('.bogus'), false)
assert.strictEqual(mimeTypes.lookup('bogus'), false)
})
it('should return false for non-strings', function () {
assert.strictEqual(mimeTypes.lookup(null), false)
assert.strictEqual(mimeTypes.lookup(undefined), false)
assert.strictEqual(mimeTypes.lookup(42), false)
assert.strictEqual(mimeTypes.lookup({}), false)
})
})
describe('.lookup(path)', function () {
it('should return mime type for file name', function () {
assert.strictEqual(mimeTypes.lookup('page.html'), 'text/html')
})
it('should return mime type for relative path', function () {
assert.strictEqual(mimeTypes.lookup('path/to/page.html'), 'text/html')
assert.strictEqual(mimeTypes.lookup('path\\to\\page.html'), 'text/html')
})
it('should return mime type for absolute path', function () {
assert.strictEqual(mimeTypes.lookup('/path/to/page.html'), 'text/html')
assert.strictEqual(mimeTypes.lookup('C:\\path\\to\\page.html'), 'text/html')
})
it('should be case insensitive', function () {
assert.strictEqual(mimeTypes.lookup('/path/to/PAGE.HTML'), 'text/html')
assert.strictEqual(mimeTypes.lookup('C:\\path\\to\\PAGE.HTML'), 'text/html')
})
it('should return false for unknown extension', function () {
assert.strictEqual(mimeTypes.lookup('/path/to/file.bogus'), false)
})
it('should return false for path without extension', function () {
assert.strictEqual(mimeTypes.lookup('/path/to/json'), false)
})
describe('path with dotfile', function () {
it('should return false when extension-less', function () {
assert.strictEqual(mimeTypes.lookup('/path/to/.json'), false)
})
it('should return mime type when there is extension', function () {
assert.strictEqual(mimeTypes.lookup('/path/to/.config.json'), 'application/json')
})
it('should return mime type when there is extension, but no path', function () {
assert.strictEqual(
mimeTypes.lookup('.config.json'),
'application/json'
)
})
})
})
// Note the changes in extension->type mapping that result from using mime-score
describe('extension conflicts', function () {
console.warn('Mime-score logic changes extension->type mappings for the following:')
for (var [extension, legacy, current] of mimeTypes._extensionConflicts) {
console.warn(`* ${extension} -> ${legacy} is now ${current}`)
}
})
})
describe('extname', function () {
it('should return the correct extension for a file with a valid extension', function () {
const result = extname('file.txt')
assert.strictEqual(result, '.txt')
})
it('should return an empty string if no extension exists', function () {
const result = extname('file')
assert.strictEqual(result, '')
})
it('should return an empty string for files that start with a dot but have no extension', function () {
const result = extname('.hiddenfile')
assert.strictEqual(result, '')
})
it('should return the correct extension for files with multiple dots in the name', function () {
const result = extname('archive.tar.gz')
assert.strictEqual(result, '.gz')
})
it('should return the correct extension for a file with mixed case extension', function () {
const result = extname('file.TXT')
assert.strictEqual(result, '.TXT')
})
it('should return an empty string if the dot is at the start of the filename', function () {
const result = extname('.file')
assert.strictEqual(result, '')
})
// POSIX:
it('should return the correct extension for a file in a nested directory', function () {
const result = extname('folder/subfolder/file.txt')
assert.strictEqual(result, '.txt')
})
it('should handle paths with multiple slashes correctly', function () {
const result = extname('/home/user/file.name.ext')
assert.strictEqual(result, '.ext')
})
// Windows:
it('should return the correct extension for a Windows path with backslashes', function () {
const result = extname('C:\\Users\\file.txt')
assert.strictEqual(result, '.txt')
})
it('should return the correct extension for a Windows path with multiple backslashes', function () {
const result = extname('C:\\Users\\Documents\\Projects\\file.tar.gz')
assert.strictEqual(result, '.gz')
})
it('should return an empty string for a Windows path with no extension', function () {
const result = extname('C:\\Users\\file')
assert.strictEqual(result, '')
})
it('should return an empty string for a hidden Windows file (starts with a dot)', function () {
const result = extname('C:\\Users\\.hiddenfile')
assert.strictEqual(result, '')
})
it('should return the correct extension for a Windows path with multiple dots', function () {
const result = extname('C:\\Users\\file.name.with.dots.ext')
assert.strictEqual(result, '.ext')
})
it('should return the correct extension for a Windows path with mixed case extension', function () {
const result = extname('C:\\Users\\file.TXT')
assert.strictEqual(result, '.TXT')
})
// Test for TypeError when input is not a string
it('should throw a TypeError if the input is not a string', function () {
assert.throws(() => extname(123), {
name: 'TypeError',
message: 'path is not a string'
})
assert.throws(() => extname(null), {
name: 'TypeError',
message: 'path is not a string'
})
assert.throws(() => extname(undefined), {
name: 'TypeError',
message: 'path is not a string'
})
assert.throws(() => extname({}), {
name: 'TypeError',
message: 'path is not a string'
})
assert.throws(() => extname([]), {
name: 'TypeError',
message: 'path is not a string'
})
})
})