-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Validation.test.js
217 lines (180 loc) · 5.62 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
'use strict';
/* eslint-disable
no-shadow,
array-bracket-spacing
*/
const webpack = require('webpack');
const Server = require('../lib/Server');
const config = require('./fixtures/simple-config/webpack.config');
describe('Validation', () => {
let compiler;
before(() => {
compiler = webpack(config);
});
const tests = [
{
name: 'invalid `hot` configuration',
config: { hot: 'false' },
message: 'options.hot should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-hot)\n'
},
{
name: 'invalid `logLevel` configuration',
config: { logLevel: 1 },
message: 'options.logLevel should be {String} and equal to one of the allowed values'
},
{
name: 'invalid `writeToDisk` configuration',
config: { writeToDisk: 1 },
message: 'options.writeToDisk should be {Boolean|Function} (https://github.com/webpack/webpack-dev-middleware#writetodisk)\n'
},
{
name: 'invalid `overlay` configuration',
config: { overlay: { errors: 1 } },
message: 'options.overlay should be {Object|Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-overlay)\n'
},
{
name: 'invalid `contentBase` configuration',
config: { contentBase: [0] },
message: 'options.contentBase should be {Array} (https://webpack.js.org/configuration/dev-server/#devserver-contentbase)\n'
},
{
name: 'no additional properties',
config: { additional: true },
message: 'options should NOT have additional properties\n'
}
];
tests.forEach((test) => {
it(`should fail validation for ${test.name}`, () => {
try {
// eslint-disable-next-line no-new
new Server(compiler, test.config);
} catch (err) {
if (err.name !== 'ValidationError') {
throw err;
}
const [ title, message ] = err.message.split('\n\n');
title.should.be.eql('webpack Dev Server Invalid Options');
message.should.be.eql(test.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 (err) {
if (err === 'ValidationError') {
throw err;
}
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 tests = [
'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);
tests.forEach((test) => {
const headers = { host: test };
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 tests = [
'test.host',
'test2.host',
'test3.host'
];
const options = { allowedHosts: tests };
const server = new Server(compiler, options);
tests.forEach((test) => {
const headers = { host: test };
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 tests = [
'www.example.com',
'subdomain.example.com',
'example.com',
'subsubcomain.subdomain.example.com',
'example.com:80',
'subdomain.example.com:80'
];
tests.forEach((test) => {
const headers = { host: test };
if (!server.checkHost(headers)) {
throw new Error("Validation didn't fail");
}
});
});
});
});
});