@@ -16,73 +16,86 @@ describe('Validation', () => {
16
16
compiler = webpack ( config ) ;
17
17
} ) ;
18
18
19
- afterAll ( ( done ) => {
20
- server . close ( ( ) => {
21
- done ( ) ;
19
+ describe ( 'validation' , ( ) => {
20
+ afterEach ( ( done ) => {
21
+ // `server` is undefined if a test is good
22
+ if ( server ) {
23
+ server . close ( ( ) => {
24
+ done ( ) ;
25
+ } ) ;
26
+ } else {
27
+ done ( ) ;
28
+ }
22
29
} ) ;
23
- } ) ;
24
30
25
- const tests = [
26
- {
27
- name : 'invalid `hot` configuration' ,
28
- config : { hot : 'false' } ,
29
- message :
30
- 'options.hot should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-hot)\n' ,
31
- } ,
32
- {
33
- name : 'invalid `logLevel` configuration' ,
34
- config : { logLevel : 1 } ,
35
- message :
36
- 'options.logLevel should be {String} and equal to one of the allowed values' ,
37
- } ,
38
- {
39
- name : 'invalid `writeToDisk` configuration' ,
40
- config : { writeToDisk : 1 } ,
41
- message :
42
- 'options.writeToDisk should be {Boolean|Function} (https://github.com/webpack/webpack-dev-middleware#writetodisk)\n' ,
43
- } ,
44
- {
45
- name : 'invalid `overlay` configuration' ,
46
- config : { overlay : { errors : 1 } } ,
47
- message :
48
- 'options.overlay should be {Object|Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-overlay)\n' ,
49
- } ,
50
- {
51
- name : 'invalid `contentBase` configuration' ,
52
- config : { contentBase : [ 0 ] } ,
53
- message :
54
- 'options.contentBase should be {Array} (https://webpack.js.org/configuration/dev-server/#devserver-contentbase)\n' ,
55
- } ,
56
- {
57
- name : 'no additional properties' ,
58
- config : { additional : true } ,
59
- message : 'options should NOT have additional properties\n' ,
60
- } ,
61
- ] ;
62
-
63
- tests . forEach ( ( test ) => {
64
- it ( `should fail validation for ${ test . name } ` , ( ) => {
65
- try {
66
- // eslint-disable-next-line no-new
67
- server = new Server ( compiler , test . config ) ;
68
- } catch ( err ) {
69
- if ( err . name !== 'ValidationError' ) {
70
- throw err ;
71
- }
31
+ const tests = [
32
+ {
33
+ name : 'invalid `hot` configuration' ,
34
+ config : { hot : 'false' } ,
35
+ message :
36
+ 'options.hot should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-hot)\n' ,
37
+ } ,
38
+ {
39
+ name : 'invalid `logLevel` configuration' ,
40
+ config : { logLevel : 1 } ,
41
+ message :
42
+ 'options.logLevel should be {String} and equal to one of the allowed values' ,
43
+ } ,
44
+ {
45
+ name : 'invalid `writeToDisk` configuration' ,
46
+ config : { writeToDisk : 1 } ,
47
+ message :
48
+ 'options.writeToDisk should be {Boolean|Function} (https://github.com/webpack/webpack-dev-middleware#writetodisk)\n' ,
49
+ } ,
50
+ {
51
+ name : 'invalid `overlay` configuration' ,
52
+ config : { overlay : { errors : 1 } } ,
53
+ message :
54
+ 'options.overlay should be {Object|Boolean} (https://webpack.js.org/configuration/dev-server/#devserver-overlay)\n' ,
55
+ } ,
56
+ {
57
+ name : 'invalid `contentBase` configuration' ,
58
+ config : { contentBase : [ 0 ] } ,
59
+ message :
60
+ 'options.contentBase should be {Array} (https://webpack.js.org/configuration/dev-server/#devserver-contentbase)\n' ,
61
+ } ,
62
+ {
63
+ name : 'no additional properties' ,
64
+ config : { additional : true } ,
65
+ message : 'options should NOT have additional properties\n' ,
66
+ } ,
67
+ ] ;
68
+
69
+ tests . forEach ( ( test ) => {
70
+ it ( `should fail validation for ${ test . name } ` , ( ) => {
71
+ try {
72
+ // eslint-disable-next-line no-new
73
+ server = new Server ( compiler , test . config ) ;
74
+ } catch ( err ) {
75
+ if ( err . name !== 'ValidationError' ) {
76
+ throw err ;
77
+ }
72
78
73
- const [ title , message ] = err . message . split ( '\n\n' ) ;
79
+ const [ title , message ] = err . message . split ( '\n\n' ) ;
74
80
75
- expect ( title ) . toEqual ( 'webpack Dev Server Invalid Options' ) ;
76
- expect ( message ) . toEqual ( test . message ) ;
81
+ expect ( title ) . toEqual ( 'webpack Dev Server Invalid Options' ) ;
82
+ expect ( message ) . toEqual ( test . message ) ;
77
83
78
- return ;
79
- }
84
+ return ;
85
+ }
80
86
81
- throw new Error ( "Validation didn't fail" ) ;
87
+ throw new Error ( "Validation didn't fail" ) ;
88
+ } ) ;
82
89
} ) ;
83
90
} ) ;
84
91
85
92
describe ( 'filename' , ( ) => {
93
+ afterEach ( ( done ) => {
94
+ server . close ( ( ) => {
95
+ done ( ) ;
96
+ } ) ;
97
+ } ) ;
98
+
86
99
it ( 'should allow filename to be a function' , ( ) => {
87
100
try {
88
101
// eslint-disable-next-line no-new
@@ -98,6 +111,12 @@ describe('Validation', () => {
98
111
} ) ;
99
112
100
113
describe ( 'checkHost' , ( ) => {
114
+ afterEach ( ( done ) => {
115
+ server . close ( ( ) => {
116
+ done ( ) ;
117
+ } ) ;
118
+ } ) ;
119
+
101
120
it ( 'should always allow any host if options.disableHostCheck is set' , ( ) => {
102
121
const options = {
103
122
public : 'test.host:80' ,
@@ -108,7 +127,7 @@ describe('Validation', () => {
108
127
host : 'bad.host' ,
109
128
} ;
110
129
111
- const server = new Server ( compiler , options ) ;
130
+ server = new Server ( compiler , options ) ;
112
131
113
132
if ( ! server . checkHost ( headers ) ) {
114
133
throw new Error ( "Validation didn't fail" ) ;
@@ -122,7 +141,7 @@ describe('Validation', () => {
122
141
const headers = {
123
142
host : 'localhost' ,
124
143
} ;
125
- const server = new Server ( compiler , options ) ;
144
+ server = new Server ( compiler , options ) ;
126
145
if ( ! server . checkHost ( headers ) ) {
127
146
throw new Error ( "Validation didn't fail" ) ;
128
147
}
@@ -137,7 +156,7 @@ describe('Validation', () => {
137
156
host : '127.0.0.1' ,
138
157
} ;
139
158
140
- const server = new Server ( compiler , options ) ;
159
+ server = new Server ( compiler , options ) ;
141
160
142
161
if ( ! server . checkHost ( headers ) ) {
143
162
throw new Error ( "Validation didn't fail" ) ;
@@ -156,7 +175,7 @@ describe('Validation', () => {
156
175
'[ad42::1de2:54c2:c2fa:1234]:8080' ,
157
176
] ;
158
177
159
- const server = new Server ( compiler , options ) ;
178
+ server = new Server ( compiler , options ) ;
160
179
161
180
tests . forEach ( ( test ) => {
162
181
const headers = { host : test } ;
@@ -176,7 +195,7 @@ describe('Validation', () => {
176
195
host : 'test.hostname:80' ,
177
196
} ;
178
197
179
- const server = new Server ( compiler , options ) ;
198
+ server = new Server ( compiler , options ) ;
180
199
181
200
if ( server . checkHost ( headers ) ) {
182
201
throw new Error ( "Validation didn't fail" ) ;
@@ -190,7 +209,7 @@ describe('Validation', () => {
190
209
const headers = {
191
210
origin : 'https://test.host' ,
192
211
} ;
193
- const server = new Server ( compiler , options ) ;
212
+ server = new Server ( compiler , options ) ;
194
213
if ( ! server . checkOrigin ( headers ) ) {
195
214
throw new Error ( "Validation didn't fail" ) ;
196
215
}
@@ -200,17 +219,18 @@ describe('Validation', () => {
200
219
it ( 'should allow hosts in allowedHosts' , ( ) => {
201
220
const tests = [ 'test.host' , 'test2.host' , 'test3.host' ] ;
202
221
const options = { allowedHosts : tests } ;
203
- const server = new Server ( compiler , options ) ;
222
+ server = new Server ( compiler , options ) ;
204
223
tests . forEach ( ( test ) => {
205
224
const headers = { host : test } ;
206
225
if ( ! server . checkHost ( headers ) ) {
207
226
throw new Error ( "Validation didn't fail" ) ;
208
227
}
209
228
} ) ;
210
229
} ) ;
230
+
211
231
it ( 'should allow hosts that pass a wildcard in allowedHosts' , ( ) => {
212
232
const options = { allowedHosts : [ '.example.com' ] } ;
213
- const server = new Server ( compiler , options ) ;
233
+ server = new Server ( compiler , options ) ;
214
234
const tests = [
215
235
'www.example.com' ,
216
236
'subdomain.example.com' ,
0 commit comments