-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
executable file
·200 lines (146 loc) · 5.32 KB
/
index.js
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
'use strict';
const Code = require('@hapi/code');
const Lab = require('@hapi/lab');
const Mimos = require('..');
const internals = {};
const { describe, it } = exports.lab = Lab.script();
const expect = Code.expect;
describe('Mimos', () => {
describe('path()', () => {
it('returns the mime type from a file path', () => {
const mimos = new Mimos.Mimos();
expect(mimos.path('/static/javascript/app.js')).equal({
source: 'iana',
charset: 'UTF-8',
compressible: true,
extensions: ['js', 'mjs'],
type: 'text/javascript'
});
});
it('returns empty object if a match can not be found', () => {
const mimos = new Mimos.Mimos();
expect(mimos.path('/static/javascript')).to.equal({});
});
it('can distinguish an empty return using instanceof', () => {
const mimos = new Mimos.Mimos();
expect(mimos.path('/static/javascript/app.js')).to.be.instanceof(Mimos.MimosEntry);
expect(mimos.path('/static/javascript')).to.not.be.instanceof(Mimos.MimosEntry);
});
it('ignores extension upper case', () => {
const lower = '/static/image/image.jpg';
const upper = '/static/image/image.JPG';
const mimos = new Mimos.Mimos();
expect(mimos.path(lower).type).to.equal(mimos.path(upper).type);
});
});
describe('type()', () => {
it('returns a found type', () => {
const mimos = new Mimos.Mimos();
expect(mimos.type('text/plain')).to.equal({
source: 'iana',
compressible: true,
extensions: ['txt', 'text', 'conf', 'def', 'list', 'log', 'in', 'ini'],
type: 'text/plain'
});
});
it('returns a type when option is included', () => {
const mimos = new Mimos.Mimos();
expect(mimos.type('text/plain;charset=UTF-8')).to.equal({
source: 'iana',
compressible: true,
extensions: ['txt', 'text', 'conf', 'def', 'list', 'log', 'in', 'ini'],
type: 'text/plain'
});
});
it('returns a missing type', () => {
const mimos = new Mimos.Mimos();
expect(mimos.type('hapi/test')).to.equal({
source: 'mimos',
compressible: false,
extensions: [],
type: 'hapi/test'
});
});
});
it('accepts an override object to make adjustments to the internal mime database', () => {
const nodeModule = {
source: 'iana',
compressible: false,
extensions: ['node', 'module', 'npm'],
type: 'node/module'
};
const dbOverwrite = {
override: {
'node/module': nodeModule
}
};
const mimos = new Mimos.Mimos(dbOverwrite);
expect(mimos.type('node/module')).to.equal(nodeModule);
expect(mimos.path('/node_modules/node/module.npm')).to.equal(nodeModule);
});
it('allows built-in types to be replaced with user mime data', () => {
const jsModule = {
source: 'iana',
charset: 'UTF-8',
compressible: true,
extensions: ['js', 'javascript'],
type: 'text/javascript'
};
const dbOverwrite = {
override: {
'application/javascript': jsModule
}
};
const mimos = new Mimos.Mimos(dbOverwrite);
expect(mimos.type('application/javascript')).to.equal(jsModule);
expect(mimos.path('/static/js/app.js')).to.equal(jsModule);
});
it('executes a predicate function if it is provided', () => {
const jsModule = {
predicate: function (mime) {
return {
foo: 'bar',
type: mime.type
};
},
type: 'text/javascript'
};
const dbOverwrite = {
override: {
'application/javascript': jsModule
}
};
const mimos = new Mimos.Mimos(dbOverwrite);
const typeResult = mimos.type('application/javascript');
expect(typeResult).to.equal({
foo: 'bar',
type: 'text/javascript'
});
const pathResult = mimos.path('/static/js/app.js');
expect(pathResult).to.equal({
foo: 'bar',
type: 'text/javascript'
});
});
it('throws an error if created without new', () => {
expect(() => {
Mimos.Mimos();
}).to.throw(/cannot be invoked without 'new'/g);
});
it('throws an error if override is not an object', () => {
expect(() => {
new Mimos.Mimos({ override: true });
}).to.throw('overrides option must be an object');
});
it('throws an error if the predicate option is not a functino', () => {
expect(() => {
new Mimos.Mimos({
override: {
'application/javascript': {
predicate: 'foo'
}
}
});
}).to.throw('predicate option must be a function');
});
});