Skip to content

Commit 8e34251

Browse files
committed
test: add e2e tests for proxy option
1 parent 56b58ce commit 8e34251

File tree

2 files changed

+128
-3
lines changed

2 files changed

+128
-3
lines changed

test/e2e/__snapshots__/proxy.test.js.snap.webpack5

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,30 @@ exports[`proxy option as an option is an object with the \`router\` option respe
182182

183183
exports[`proxy option as an option is an object with the \`router\` option respects a proxy option: response text 1`] = `"from proxy1"`;
184184

185+
exports[`proxy option should handles external websocket upgrade with webSocketServerType: sockjs Should receive response: console messages 1`] = `
186+
Array [
187+
"Failed to load resource: the server responded with a status of 426 (Upgrade Required)",
188+
]
189+
`;
190+
191+
exports[`proxy option should handles external websocket upgrade with webSocketServerType: sockjs Should receive response: page errors 1`] = `Array []`;
192+
193+
exports[`proxy option should handles external websocket upgrade with webSocketServerType: sockjs Should receive response: response status 1`] = `426`;
194+
195+
exports[`proxy option should handles external websocket upgrade with webSocketServerType: sockjs Should receive response: response text 1`] = `"Upgrade Required"`;
196+
197+
exports[`proxy option should handles external websocket upgrade with webSocketServerType: ws Should receive response: console messages 1`] = `
198+
Array [
199+
"Failed to load resource: the server responded with a status of 426 (Upgrade Required)",
200+
]
201+
`;
202+
203+
exports[`proxy option should handles external websocket upgrade with webSocketServerType: ws Should receive response: page errors 1`] = `Array []`;
204+
205+
exports[`proxy option should handles external websocket upgrade with webSocketServerType: ws Should receive response: response status 1`] = `426`;
206+
207+
exports[`proxy option should handles external websocket upgrade with webSocketServerType: ws Should receive response: response text 1`] = `"Upgrade Required"`;
208+
185209
exports[`proxy option should sharing a proxy option respects proxy1 option: console messages 1`] = `Array []`;
186210

187211
exports[`proxy option should sharing a proxy option respects proxy1 option: page errors 1`] = `Array []`;

test/e2e/proxy.test.js

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
const path = require("path");
44
const express = require("express");
55
// const bodyParser = require("body-parser");
6-
// const WebSocket = require("ws");
6+
const WebSocket = require("ws");
77
const webpack = require("webpack");
88
const runBrowser = require("../helpers/run-browser");
99
const Server = require("../../lib/Server");
1010
const config = require("../fixtures/proxy-config/webpack.config");
11-
const [port1, port2, port3] = require("../ports-map")["proxy-option"];
11+
const [port1, port2, port3, port4] = require("../ports-map")["proxy-option"];
1212

13-
// const WebSocketServer = WebSocket.Server;
13+
const WebSocketServer = WebSocket.Server;
1414
const staticDirectory = path.resolve(__dirname, "../fixtures/proxy-config");
1515

1616
const proxyOptionPathsAsProperties = {
@@ -966,4 +966,105 @@ describe("proxy option", () => {
966966
expect(pageErrors).toMatchSnapshot("page errors");
967967
});
968968
});
969+
970+
describe("should handles external websocket upgrade", () => {
971+
let compiler;
972+
let server;
973+
let page;
974+
let browser;
975+
let pageErrors;
976+
let consoleMessages;
977+
let ws;
978+
let webSocketServer;
979+
let responseMessage;
980+
981+
const webSocketServerTypes = ["sockjs", "ws"];
982+
983+
webSocketServerTypes.forEach((webSocketServerType) => {
984+
describe(`with webSocketServerType: ${webSocketServerType}`, () => {
985+
beforeEach(async () => {
986+
compiler = webpack(config);
987+
988+
server = new Server(
989+
{
990+
webSocketServer: webSocketServerType,
991+
proxy: [
992+
{
993+
context: "/",
994+
target: `http://localhost:${port4}`,
995+
ws: true,
996+
},
997+
],
998+
port: port3,
999+
},
1000+
compiler
1001+
);
1002+
1003+
await server.start();
1004+
1005+
webSocketServer = new WebSocketServer({ port: port4 });
1006+
webSocketServer.on("connection", (connection) => {
1007+
connection.on("message", (message) => {
1008+
connection.send(message);
1009+
});
1010+
});
1011+
1012+
ws = new WebSocket(`ws://localhost:${port3}/proxy3/socket`);
1013+
1014+
ws.on("message", (message) => {
1015+
responseMessage = message.toString();
1016+
});
1017+
1018+
ws.on("open", () => {
1019+
ws.send("foo");
1020+
});
1021+
1022+
({ page, browser } = await runBrowser());
1023+
1024+
pageErrors = [];
1025+
consoleMessages = [];
1026+
});
1027+
1028+
afterEach(async () => {
1029+
webSocketServer.close();
1030+
1031+
for (const client of webSocketServer.clients) {
1032+
client.terminate();
1033+
}
1034+
1035+
await browser.close();
1036+
await server.stop();
1037+
});
1038+
1039+
it("Should receive response", async () => {
1040+
page
1041+
.on("console", (message) => {
1042+
consoleMessages.push(message);
1043+
})
1044+
.on("pageerror", (error) => {
1045+
pageErrors.push(error);
1046+
});
1047+
1048+
const response = await page.goto(
1049+
`http://localhost:${port3}/proxy3/socket`,
1050+
{
1051+
waitUntil: "networkidle0",
1052+
}
1053+
);
1054+
1055+
expect(responseMessage).toEqual("foo");
1056+
1057+
expect(response.status()).toMatchSnapshot("response status");
1058+
1059+
expect(await response.text()).toMatchSnapshot("response text");
1060+
1061+
expect(
1062+
consoleMessages.map((message) => message.text())
1063+
).toMatchSnapshot("console messages");
1064+
1065+
expect(pageErrors).toMatchSnapshot("page errors");
1066+
});
1067+
});
1068+
});
1069+
});
9691070
});

0 commit comments

Comments
 (0)