1
1
'use strict' ;
2
2
3
3
const express = require ( 'express' ) ;
4
+ const internalIp = require ( 'internal-ip' ) ;
4
5
const { createProxyMiddleware } = require ( 'http-proxy-middleware' ) ;
5
6
const request = require ( 'supertest' ) ;
6
7
const testServer = require ( '../helpers/test-server' ) ;
@@ -9,6 +10,266 @@ const runBrowser = require('../helpers/run-browser');
9
10
const [ port1 , port2 , port3 ] = require ( '../ports-map' ) . ClientOptions ;
10
11
const { beforeBrowserCloseDelay } = require ( '../helpers/puppeteer-constants' ) ;
11
12
13
+ describe . only ( 'sockjs client proxy, different hostnames and different ports' , ( ) => {
14
+ const devServerHost = '127.0.0.1' ;
15
+ const devServerPort = port1 ;
16
+ const proxyHost = internalIp . v4 . sync ( ) ;
17
+ const proxyPort = port2 ;
18
+
19
+ function startProxy ( cb ) {
20
+ const proxy = express ( ) ;
21
+
22
+ proxy . use (
23
+ '/' ,
24
+ createProxyMiddleware ( {
25
+ target : `http://${ devServerHost } ` ,
26
+ ws : true ,
27
+ changeOrigin : true ,
28
+ logLevel : 'warn' ,
29
+ } )
30
+ ) ;
31
+
32
+ return proxy . listen ( proxyPort , proxyHost , cb ) ;
33
+ }
34
+
35
+ beforeAll ( ( done ) => {
36
+ const options = {
37
+ transportMode : 'sockjs' ,
38
+ port : devServerPort ,
39
+ host : devServerHost ,
40
+ firewall : false ,
41
+ hot : true ,
42
+ } ;
43
+
44
+ testServer . startAwaitingCompilation ( config , options , done ) ;
45
+ } ) ;
46
+
47
+ afterAll ( testServer . close ) ;
48
+
49
+ // [HPM] Proxy created: / -> http://localhost:{port1}
50
+ describe ( 'behind a proxy' , ( ) => {
51
+ let proxy ;
52
+
53
+ beforeAll ( ( done ) => {
54
+ proxy = startProxy ( ( ) => {
55
+ done ( ) ;
56
+ } ) ;
57
+ } ) ;
58
+
59
+ afterAll ( ( done ) => {
60
+ proxy . close ( ( ) => {
61
+ done ( ) ;
62
+ } ) ;
63
+ } ) ;
64
+
65
+ it ( 'responds with a 200 code on proxy' , ( done ) => {
66
+ const req = request ( `http://${ proxyHost } :${ proxyPort } ` ) ;
67
+
68
+ req . get ( '/ws' ) . expect ( 200 , 'Welcome to SockJS!\n' , done ) ;
69
+ } ) ;
70
+
71
+ it ( 'responds with a 200 code on non-proxy' , ( done ) => {
72
+ const req = request ( `http://${ devServerHost } :${ devServerPort } ` ) ;
73
+
74
+ req . get ( '/ws' ) . expect ( 200 , 'Welcome to SockJS!\n' , done ) ;
75
+ } ) ;
76
+
77
+ it ( 'requests websocket through the proxy' , ( done ) => {
78
+ runBrowser ( ) . then ( async ( { page, browser } ) => {
79
+ page
80
+ . waitForRequest ( ( requestObj ) => requestObj . url ( ) . match ( / w s / ) )
81
+ . then ( ( requestObj ) => {
82
+ page . waitForTimeout ( beforeBrowserCloseDelay ) . then ( ( ) => {
83
+ browser . close ( ) . then ( ( ) => {
84
+ expect ( requestObj . url ( ) ) . toContain (
85
+ `http://${ proxyHost } :${ proxyPort } /ws`
86
+ ) ;
87
+
88
+ done ( ) ;
89
+ } ) ;
90
+ } ) ;
91
+ } ) ;
92
+
93
+ console . log ( `http://${ proxyHost } :${ proxyPort } /main` ) ;
94
+
95
+ page . goto ( `http://${ proxyHost } :${ proxyPort } /main` ) ;
96
+ } ) ;
97
+ } ) ;
98
+ } ) ;
99
+ } ) ;
100
+
101
+ describe ( 'sockjs client proxy, different hostnames and same ports' , ( ) => {
102
+ const devServerHost = '127.0.0.1' ;
103
+ const devServerPort = port1 ;
104
+ const proxyHost = internalIp . v4 . sync ( ) ;
105
+ const proxyPort = port1 ;
106
+
107
+ function startProxy ( cb ) {
108
+ const proxy = express ( ) ;
109
+
110
+ proxy . use (
111
+ '/' ,
112
+ createProxyMiddleware ( {
113
+ target : `http://${ devServerHost } :${ devServerPort } ` ,
114
+ ws : true ,
115
+ changeOrigin : true ,
116
+ logLevel : 'warn' ,
117
+ } )
118
+ ) ;
119
+
120
+ return proxy . listen ( proxyPort , proxyHost , cb ) ;
121
+ }
122
+
123
+ beforeAll ( ( done ) => {
124
+ const options = {
125
+ transportMode : 'sockjs' ,
126
+ port : devServerPort ,
127
+ host : devServerHost ,
128
+ firewall : false ,
129
+ hot : true ,
130
+ } ;
131
+
132
+ testServer . startAwaitingCompilation ( config , options , done ) ;
133
+ } ) ;
134
+
135
+ afterAll ( testServer . close ) ;
136
+
137
+ // [HPM] Proxy created: / -> http://localhost:{port1}
138
+ describe ( 'behind a proxy' , ( ) => {
139
+ let proxy ;
140
+
141
+ beforeAll ( ( done ) => {
142
+ proxy = startProxy ( ( ) => {
143
+ done ( ) ;
144
+ } ) ;
145
+ } ) ;
146
+
147
+ afterAll ( ( done ) => {
148
+ proxy . close ( ( ) => {
149
+ done ( ) ;
150
+ } ) ;
151
+ } ) ;
152
+
153
+ it ( 'responds with a 200 code on proxy' , ( done ) => {
154
+ const req = request ( `http://${ proxyHost } :${ proxyPort } ` ) ;
155
+
156
+ req . get ( '/ws' ) . expect ( 200 , 'Welcome to SockJS!\n' , done ) ;
157
+ } ) ;
158
+
159
+ it ( 'responds with a 200 code on non-proxy' , ( done ) => {
160
+ const req = request ( `http://${ devServerHost } :${ devServerPort } ` ) ;
161
+
162
+ req . get ( '/ws' ) . expect ( 200 , 'Welcome to SockJS!\n' , done ) ;
163
+ } ) ;
164
+
165
+ it ( 'requests websocket through the proxy' , ( done ) => {
166
+ runBrowser ( ) . then ( async ( { page, browser } ) => {
167
+ page
168
+ . waitForRequest ( ( requestObj ) => requestObj . url ( ) . match ( / w s / ) )
169
+ . then ( ( requestObj ) => {
170
+ page . waitForTimeout ( beforeBrowserCloseDelay ) . then ( ( ) => {
171
+ browser . close ( ) . then ( ( ) => {
172
+ expect ( requestObj . url ( ) ) . toContain (
173
+ `http://${ proxyHost } :${ proxyPort } /ws`
174
+ ) ;
175
+
176
+ done ( ) ;
177
+ } ) ;
178
+ } ) ;
179
+ } ) ;
180
+
181
+ page . goto ( `http://${ proxyHost } :${ proxyPort } /main` ) ;
182
+ } ) ;
183
+ } ) ;
184
+ } ) ;
185
+ } ) ;
186
+
187
+ describe ( 'sockjs client proxy, same hostnames and different ports' , ( ) => {
188
+ const devServerHost = '127.0.0.1' ;
189
+ const devServerPort = port1 ;
190
+ const proxyHost = devServerHost ;
191
+ const proxyPort = port2 ;
192
+
193
+ function startProxy ( cb ) {
194
+ const proxy = express ( ) ;
195
+
196
+ proxy . use (
197
+ '/' ,
198
+ createProxyMiddleware ( {
199
+ target : `http://${ devServerHost } :${ devServerPort } ` ,
200
+ ws : true ,
201
+ changeOrigin : true ,
202
+ logLevel : 'warn' ,
203
+ } )
204
+ ) ;
205
+
206
+ return proxy . listen ( proxyPort , proxyHost , cb ) ;
207
+ }
208
+
209
+ beforeAll ( ( done ) => {
210
+ const options = {
211
+ transportMode : 'sockjs' ,
212
+ port : devServerPort ,
213
+ host : devServerHost ,
214
+ firewall : false ,
215
+ hot : true ,
216
+ } ;
217
+
218
+ testServer . startAwaitingCompilation ( config , options , done ) ;
219
+ } ) ;
220
+
221
+ afterAll ( testServer . close ) ;
222
+
223
+ // [HPM] Proxy created: / -> http://localhost:{port1}
224
+ describe ( 'behind a proxy' , ( ) => {
225
+ let proxy ;
226
+
227
+ beforeAll ( ( done ) => {
228
+ proxy = startProxy ( ( ) => {
229
+ done ( ) ;
230
+ } ) ;
231
+ } ) ;
232
+
233
+ afterAll ( ( done ) => {
234
+ proxy . close ( ( ) => {
235
+ done ( ) ;
236
+ } ) ;
237
+ } ) ;
238
+
239
+ it ( 'responds with a 200 code on proxy' , ( done ) => {
240
+ const req = request ( `http://${ proxyHost } :${ proxyPort } ` ) ;
241
+
242
+ req . get ( '/ws' ) . expect ( 200 , 'Welcome to SockJS!\n' , done ) ;
243
+ } ) ;
244
+
245
+ it ( 'responds with a 200 code on non-proxy' , ( done ) => {
246
+ const req = request ( `http://${ devServerHost } :${ devServerPort } ` ) ;
247
+
248
+ req . get ( '/ws' ) . expect ( 200 , 'Welcome to SockJS!\n' , done ) ;
249
+ } ) ;
250
+
251
+ it ( 'requests websocket through the proxy' , ( done ) => {
252
+ runBrowser ( ) . then ( async ( { page, browser } ) => {
253
+ page
254
+ . waitForRequest ( ( requestObj ) => requestObj . url ( ) . match ( / w s / ) )
255
+ . then ( ( requestObj ) => {
256
+ page . waitForTimeout ( beforeBrowserCloseDelay ) . then ( ( ) => {
257
+ browser . close ( ) . then ( ( ) => {
258
+ expect ( requestObj . url ( ) ) . toContain (
259
+ `http://${ proxyHost } :${ devServerPort } /ws`
260
+ ) ;
261
+
262
+ done ( ) ;
263
+ } ) ;
264
+ } ) ;
265
+ } ) ;
266
+
267
+ page . goto ( `http://${ proxyHost } :${ proxyPort } /main` ) ;
268
+ } ) ;
269
+ } ) ;
270
+ } ) ;
271
+ } ) ;
272
+
12
273
describe ( 'sockjs client proxy' , ( ) => {
13
274
function startProxy ( port , cb ) {
14
275
const proxy = express ( ) ;
0 commit comments