Skip to content

Commit f8f1015

Browse files
test: more
1 parent b077d23 commit f8f1015

File tree

3 files changed

+212
-50
lines changed

3 files changed

+212
-50
lines changed

test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack4

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ Array [
107107

108108
exports[`web socket server URL should work behind proxy, when hostnames are same and ports are different ("ws"): page errors 1`] = `Array []`;
109109

110+
exports[`web socket server URL should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("sockjs"): console messages 1`] = `
111+
Array [
112+
"[HMR] Waiting for update signal from WDS...",
113+
"Hey.",
114+
"[webpack-dev-server] Hot Module Replacement enabled.",
115+
"[webpack-dev-server] Live Reloading enabled.",
116+
]
117+
`;
118+
119+
exports[`web socket server URL should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("sockjs"): page errors 1`] = `Array []`;
120+
121+
exports[`web socket server URL should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("ws"): console messages 1`] = `
122+
Array [
123+
"[HMR] Waiting for update signal from WDS...",
124+
"Hey.",
125+
"[webpack-dev-server] Hot Module Replacement enabled.",
126+
"[webpack-dev-server] Live Reloading enabled.",
127+
]
128+
`;
129+
130+
exports[`web socket server URL should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("ws"): page errors 1`] = `Array []`;
131+
110132
exports[`web socket server URL should work when "host" option is "local-ip" ("sockjs"): console messages 1`] = `
111133
Array [
112134
"[HMR] Waiting for update signal from WDS...",

test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack5

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ Array [
107107

108108
exports[`web socket server URL should work behind proxy, when hostnames are same and ports are different ("ws"): page errors 1`] = `Array []`;
109109

110+
exports[`web socket server URL should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("sockjs"): console messages 1`] = `
111+
Array [
112+
"[HMR] Waiting for update signal from WDS...",
113+
"Hey.",
114+
"[webpack-dev-server] Hot Module Replacement enabled.",
115+
"[webpack-dev-server] Live Reloading enabled.",
116+
]
117+
`;
118+
119+
exports[`web socket server URL should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("sockjs"): page errors 1`] = `Array []`;
120+
121+
exports[`web socket server URL should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("ws"): console messages 1`] = `
122+
Array [
123+
"[HMR] Waiting for update signal from WDS...",
124+
"Hey.",
125+
"[webpack-dev-server] Hot Module Replacement enabled.",
126+
"[webpack-dev-server] Live Reloading enabled.",
127+
]
128+
`;
129+
130+
exports[`web socket server URL should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("ws"): page errors 1`] = `Array []`;
131+
110132
exports[`web socket server URL should work when "host" option is "local-ip" ("sockjs"): console messages 1`] = `
111133
Array [
112134
"[HMR] Waiting for update signal from WDS...",

test/e2e/web-socket-server-url.test.js

Lines changed: 168 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,6 @@ describe('web socket server URL', () => {
2121
const proxyHost = devServerHost;
2222
const proxyPort = port2;
2323

24-
function startProxy(callback) {
25-
const app = express();
26-
app.use(
27-
'/',
28-
createProxyMiddleware({
29-
target: `http://${devServerHost}:${devServerPort}`,
30-
ws: true,
31-
changeOrigin: true,
32-
logLevel: 'warn',
33-
})
34-
);
35-
36-
return app.listen(proxyPort, proxyHost, callback);
37-
}
38-
39-
const proxy = await new Promise((resolve) => {
40-
const proxyCreated = startProxy(() => {
41-
resolve(proxyCreated);
42-
});
43-
});
44-
4524
const compiler = webpack(config);
4625
const devServerOptions = {
4726
webSocketServer,
@@ -63,6 +42,27 @@ describe('web socket server URL', () => {
6342
});
6443
});
6544

45+
function startProxy(callback) {
46+
const app = express();
47+
app.use(
48+
'/',
49+
createProxyMiddleware({
50+
target: `http://${devServerHost}:${devServerPort}`,
51+
ws: true,
52+
changeOrigin: true,
53+
logLevel: 'warn',
54+
})
55+
);
56+
57+
return app.listen(proxyPort, proxyHost, callback);
58+
}
59+
60+
const proxy = await new Promise((resolve) => {
61+
const proxyCreated = startProxy(() => {
62+
resolve(proxyCreated);
63+
});
64+
});
65+
6666
const { page, browser } = await runBrowser();
6767

6868
const pageErrors = [];
@@ -127,27 +127,6 @@ describe('web socket server URL', () => {
127127
const proxyHost = internalIp.v4.sync();
128128
const proxyPort = port1;
129129

130-
function startProxy(callback) {
131-
const app = express();
132-
app.use(
133-
'/',
134-
createProxyMiddleware({
135-
target: `http://${devServerHost}:${devServerPort}`,
136-
ws: true,
137-
changeOrigin: true,
138-
logLevel: 'warn',
139-
})
140-
);
141-
142-
return app.listen(proxyPort, proxyHost, callback);
143-
}
144-
145-
const proxy = await new Promise((resolve) => {
146-
const proxyCreated = startProxy(() => {
147-
resolve(proxyCreated);
148-
});
149-
});
150-
151130
const compiler = webpack(config);
152131
const devServerOptions = {
153132
webSocketServer,
@@ -169,6 +148,27 @@ describe('web socket server URL', () => {
169148
});
170149
});
171150

151+
function startProxy(callback) {
152+
const app = express();
153+
app.use(
154+
'/',
155+
createProxyMiddleware({
156+
target: `http://${devServerHost}:${devServerPort}`,
157+
ws: true,
158+
changeOrigin: true,
159+
logLevel: 'warn',
160+
})
161+
);
162+
163+
return app.listen(proxyPort, proxyHost, callback);
164+
}
165+
166+
const proxy = await new Promise((resolve) => {
167+
const proxyCreated = startProxy(() => {
168+
resolve(proxyCreated);
169+
});
170+
});
171+
172172
const { page, browser } = await runBrowser();
173173

174174
const pageErrors = [];
@@ -233,6 +233,32 @@ describe('web socket server URL', () => {
233233
const proxyHost = internalIp.v4.sync();
234234
const proxyPort = port2;
235235

236+
const compiler = webpack(config);
237+
const devServerOptions = {
238+
client: {
239+
webSocketURL: {
240+
hostname: devServerHost,
241+
},
242+
},
243+
webSocketServer,
244+
port: devServerPort,
245+
host: devServerHost,
246+
allowedHosts: 'all',
247+
};
248+
const server = new Server(devServerOptions, compiler);
249+
250+
await new Promise((resolve, reject) => {
251+
server.listen(devServerOptions.port, devServerOptions.host, (error) => {
252+
if (error) {
253+
reject(error);
254+
255+
return;
256+
}
257+
258+
resolve();
259+
});
260+
});
261+
236262
function startProxy(callback) {
237263
const app = express();
238264
app.use(
@@ -254,16 +280,76 @@ describe('web socket server URL', () => {
254280
});
255281
});
256282

283+
const { page, browser } = await runBrowser();
284+
285+
const pageErrors = [];
286+
const consoleMessages = [];
287+
288+
page
289+
.on('console', (message) => {
290+
consoleMessages.push(message);
291+
})
292+
.on('pageerror', (error) => {
293+
pageErrors.push(error);
294+
});
295+
296+
const webSocketRequests = [];
297+
298+
if (webSocketServer === 'ws') {
299+
const client = page._client;
300+
301+
client.on('Network.webSocketCreated', (test) => {
302+
webSocketRequests.push(test);
303+
});
304+
} else {
305+
page.on('request', (request) => {
306+
if (/\/ws\//.test(request.url())) {
307+
webSocketRequests.push({ url: request.url() });
308+
}
309+
});
310+
}
311+
312+
await page.goto(`http://${proxyHost}:${proxyPort}/main`, {
313+
waitUntil: 'networkidle0',
314+
});
315+
316+
const webSocketRequest = webSocketRequests[0];
317+
318+
expect(webSocketRequest.url).toContain(
319+
`${websocketURLProtocol}://${devServerHost}:${devServerPort}/ws`
320+
);
321+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
322+
'console messages'
323+
);
324+
expect(pageErrors).toMatchSnapshot('page errors');
325+
326+
proxy.close();
327+
328+
await browser.close();
329+
await new Promise((resolve, reject) => {
330+
server.close((error) => {
331+
if (error) {
332+
reject(error);
333+
334+
return;
335+
}
336+
337+
resolve();
338+
});
339+
});
340+
});
341+
342+
it(`should work behind proxy, when the "host" option is "local-ip" and the "port" option is "auto" ("${webSocketServer}")`, async () => {
343+
process.env.WEBPACK_DEV_SERVER_BASE_PORT = 40000;
344+
345+
const proxyHost = internalIp.v4.sync();
346+
const proxyPort = port2;
347+
257348
const compiler = webpack(config);
258349
const devServerOptions = {
259-
client: {
260-
webSocketURL: {
261-
hostname: devServerHost,
262-
},
263-
},
264350
webSocketServer,
265-
port: devServerPort,
266-
host: devServerHost,
351+
port: 'auto',
352+
host: 'local-ip',
267353
allowedHosts: 'all',
268354
};
269355
const server = new Server(devServerOptions, compiler);
@@ -280,6 +366,31 @@ describe('web socket server URL', () => {
280366
});
281367
});
282368

369+
const resolvedHost = server.options.host;
370+
const resolvedPort = server.options.port;
371+
372+
function startProxy(callback) {
373+
const app = express();
374+
375+
app.use(
376+
'/',
377+
createProxyMiddleware({
378+
target: `http://${resolvedHost}:${resolvedPort}`,
379+
ws: true,
380+
changeOrigin: true,
381+
logLevel: 'warn',
382+
})
383+
);
384+
385+
return app.listen(proxyPort, proxyHost, callback);
386+
}
387+
388+
const proxy = await new Promise((resolve) => {
389+
const proxyCreated = startProxy(() => {
390+
resolve(proxyCreated);
391+
});
392+
});
393+
283394
const { page, browser } = await runBrowser();
284395

285396
const pageErrors = [];
@@ -316,14 +427,15 @@ describe('web socket server URL', () => {
316427
const webSocketRequest = webSocketRequests[0];
317428

318429
expect(webSocketRequest.url).toContain(
319-
`${websocketURLProtocol}://${devServerHost}:${devServerPort}/ws`
430+
`${websocketURLProtocol}://${resolvedHost}:${resolvedPort}/ws`
320431
);
321432
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
322433
'console messages'
323434
);
324435
expect(pageErrors).toMatchSnapshot('page errors');
325436

326437
proxy.close();
438+
327439
await browser.close();
328440
await new Promise((resolve, reject) => {
329441
server.close((error) => {
@@ -336,6 +448,8 @@ describe('web socket server URL', () => {
336448
resolve();
337449
});
338450
});
451+
452+
delete process.env.WEBPACK_DEV_SERVER_BASE_PORT;
339453
});
340454

341455
it(`should work with the "client.webSocketURL.protocol" option ("${webSocketServer}")`, async () => {
@@ -1770,6 +1884,8 @@ describe('web socket server URL', () => {
17701884
});
17711885

17721886
it(`should work when "port" option is "auto" ("${webSocketServer}")`, async () => {
1887+
process.env.WEBPACK_DEV_SERVER_BASE_PORT = 50000;
1888+
17731889
const compiler = webpack(config);
17741890
const devServerOptions = {
17751891
webSocketServer,
@@ -1847,6 +1963,8 @@ describe('web socket server URL', () => {
18471963
resolve();
18481964
});
18491965
});
1966+
1967+
delete process.env.WEBPACK_DEV_SERVER_BASE_PORT;
18501968
});
18511969

18521970
it(`should work with "client.webSocketURL.*" options ("${webSocketServer}")`, async () => {

0 commit comments

Comments
 (0)