-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Validation.test.js
197 lines (185 loc) · 6.24 KB
/
Validation.test.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
'use strict';
const webpack = require('webpack');
const OptionsValidationError = require('../lib/OptionsValidationError');
const Server = require('../lib/Server');
const config = require('./fixtures/simple-config/webpack.config');
describe('Validation', () => {
let compiler;
before(() => {
compiler = webpack(config);
});
const testCases = [{
name: 'invalid `hot` configuration',
config: { hot: 'asdf' },
message: [
' - configuration.hot should be a boolean.'
]
}, {
name: 'invalid `public` configuration',
config: { public: 1 },
message: [
' - configuration.public should be a string.'
]
}, {
name: 'invalid `allowedHosts` configuration',
config: { allowedHosts: 1 },
message: [
' - configuration.allowedHosts should be an array:',
' [string]',
' Specifies which hosts are allowed to access the dev server.'
]
}, {
name: 'invalid `contentBase` configuration',
config: { contentBase: [0] },
message: [
' - configuration.contentBase should be one of these:',
' [string] | false | number | string',
' A directory to serve files non-webpack files from.',
' Details:',
' * configuration.contentBase[0] should be a string.',
' * configuration.contentBase should be false',
' * configuration.contentBase should be a number.',
' * configuration.contentBase should be a string.'
]
}, {
name: 'non-existing key configuration',
config: { asdf: true },
message: [
" - configuration has an unknown property 'asdf'. These properties are valid:",
' object { hot?, hotOnly?, lazy?, bonjour?, host?, allowedHosts?, filename?, publicPath?, port?, socket?, ' +
'watchOptions?, headers?, logLevel?, clientLogLevel?, overlay?, progress?, key?, cert?, ca?, pfx?, pfxPassphrase?, requestCert?, ' +
'inline?, disableHostCheck?, public?, https?, contentBase?, watchContentBase?, open?, useLocalIp?, openPage?, features?, ' +
'compress?, proxy?, historyApiFallback?, staticOptions?, setup?, before?, after?, stats?, reporter?, logTime?, ' +
'noInfo?, quiet?, serverSideRender?, index?, log?, warn? }'
]
}];
testCases.forEach((testCase) => {
it(`should fail validation for ${testCase.name}`, () => {
try {
// eslint-disable-next-line no-new
new Server(compiler, testCase.config);
} catch (e) {
if (!(e instanceof OptionsValidationError)) { throw e; }
e.message.should.startWith('Invalid configuration object.');
e.message.split('\n').slice(1).should.be.eql(testCase.message);
return;
}
throw new Error("Validation didn't fail");
});
});
describe('filename', () => {
it('should allow filename to be a function', () => {
try {
// eslint-disable-next-line no-new
new Server(compiler, { filename: () => {} });
} catch (e) {
if (!(e instanceof OptionsValidationError)) { throw e; }
throw new Error("Validation failed and it shouldn't");
}
});
});
describe('checkHost', () => {
it('should always allow any host if options.disableHostCheck is set', () => {
const options = {
public: 'test.host:80',
disableHostCheck: true
};
const headers = {
host: 'bad.host'
};
const server = new Server(compiler, options);
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
it('should allow any valid options.public when host is localhost', () => {
const options = {
public: 'test.host:80'
};
const headers = {
host: 'localhost'
};
const server = new Server(compiler, options);
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
it('should allow any valid options.public when host is 127.0.0.1', () => {
const options = {
public: 'test.host:80'
};
const headers = {
host: '127.0.0.1'
};
const server = new Server(compiler, options);
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
it('should allow access for every requests using an IP', () => {
const options = {};
const testHosts = [
'192.168.1.123',
'192.168.1.2:8080',
'[::1]',
'[::1]:8080',
'[ad42::1de2:54c2:c2fa:1234]',
'[ad42::1de2:54c2:c2fa:1234]:8080'
];
const server = new Server(compiler, options);
testHosts.forEach((testHost) => {
const headers = { host: testHost };
if (!server.checkHost(headers)) {
throw new Error("Validation didn't pass");
}
});
});
it("should not allow hostnames that don't match options.public", () => {
const options = {
public: 'test.host:80'
};
const headers = {
host: 'test.hostname:80'
};
const server = new Server(compiler, options);
if (server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
describe('allowedHosts', () => {
it('should allow hosts in allowedHosts', () => {
const testHosts = [
'test.host',
'test2.host',
'test3.host'
];
const options = { allowedHosts: testHosts };
const server = new Server(compiler, options);
testHosts.forEach((testHost) => {
const headers = { host: testHost };
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
});
it('should allow hosts that pass a wildcard in allowedHosts', () => {
const options = { allowedHosts: ['.example.com'] };
const server = new Server(compiler, options);
const testHosts = [
'www.example.com',
'subdomain.example.com',
'example.com',
'subsubcomain.subdomain.example.com',
'example.com:80',
'subdomain.example.com:80'
];
testHosts.forEach((testHost) => {
const headers = { host: testHost };
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
});
});
});
});