1
1
'use strict' ;
2
2
3
+ const internalIp = require ( 'internal-ip' ) ;
3
4
const webpack = require ( 'webpack' ) ;
4
5
const Server = require ( '../../lib/Server' ) ;
5
6
const config = require ( '../fixtures/simple-config/webpack.config' ) ;
@@ -20,14 +21,59 @@ describe('allowedHosts', () => {
20
21
} ) ;
21
22
} ) ;
22
23
23
- it ( 'should always allow any host if options.allowedHosts is enabled ' , ( ) => {
24
+ it ( 'should always allow `localhost` if options.allowedHosts is auto ' , ( ) => {
24
25
const options = {
26
+ allowedHosts : 'auto' ,
27
+ } ;
28
+ const headers = {
29
+ host : 'localhost' ,
30
+ } ;
31
+
32
+ server = createServer ( compiler , options ) ;
33
+
34
+ if ( ! server . checkHost ( headers ) ) {
35
+ throw new Error ( "Validation didn't fail" ) ;
36
+ }
37
+ } ) ;
38
+
39
+ it ( 'should always allow value from the `host` options if options.allowedHosts is auto' , ( ) => {
40
+ const networkIP = internalIp . v4 . sync ( ) ;
41
+ const options = {
42
+ host : networkIP ,
43
+ } ;
44
+ const headers = {
45
+ host : networkIP ,
46
+ } ;
47
+
48
+ server = createServer ( compiler , options ) ;
49
+
50
+ if ( ! server . checkHost ( headers ) ) {
51
+ throw new Error ( "Validation didn't fail" ) ;
52
+ }
53
+ } ) ;
54
+
55
+ it ( 'should always allow value of the `host` option from the `client.webSocketURL` option if options.allowedHosts is auto' , ( ) => {
56
+ const options = {
57
+ allowedHosts : 'auto' ,
25
58
client : {
26
59
webSocketURL : 'ws://test.host:80' ,
27
60
} ,
28
- allowedHosts : 'all' ,
61
+ } ;
62
+ const headers = {
63
+ host : 'test.host' ,
29
64
} ;
30
65
66
+ server = createServer ( compiler , options ) ;
67
+
68
+ if ( ! server . checkHost ( headers ) ) {
69
+ throw new Error ( "Validation didn't fail" ) ;
70
+ }
71
+ } ) ;
72
+
73
+ it ( 'should always allow any host if options.allowedHosts is all' , ( ) => {
74
+ const options = {
75
+ allowedHosts : 'all' ,
76
+ } ;
31
77
const headers = {
32
78
host : 'bad.host' ,
33
79
} ;
@@ -42,9 +88,11 @@ describe('allowedHosts', () => {
42
88
it ( 'should allow hosts in allowedHosts' , ( ) => {
43
89
const tests = [ 'test.host' , 'test2.host' , 'test3.host' ] ;
44
90
const options = { allowedHosts : tests } ;
91
+
45
92
server = createServer ( compiler , options ) ;
46
93
tests . forEach ( ( test ) => {
47
94
const headers = { host : test } ;
95
+
48
96
if ( ! server . checkHost ( headers ) ) {
49
97
throw new Error ( "Validation didn't fail" ) ;
50
98
}
@@ -53,7 +101,9 @@ describe('allowedHosts', () => {
53
101
54
102
it ( 'should allow hosts that pass a wildcard in allowedHosts' , ( ) => {
55
103
const options = { allowedHosts : [ '.example.com' ] } ;
104
+
56
105
server = createServer ( compiler , options ) ;
106
+
57
107
const tests = [
58
108
'www.example.com' ,
59
109
'subdomain.example.com' ,
@@ -62,8 +112,10 @@ describe('allowedHosts', () => {
62
112
'example.com:80' ,
63
113
'subdomain.example.com:80' ,
64
114
] ;
115
+
65
116
tests . forEach ( ( test ) => {
66
117
const headers = { host : test } ;
118
+
67
119
if ( ! server . checkHost ( headers ) ) {
68
120
throw new Error ( "Validation didn't fail" ) ;
69
121
}
0 commit comments