|
3 | 3 | const path = require("path");
|
4 | 4 | const express = require("express");
|
5 | 5 | // const bodyParser = require("body-parser");
|
6 |
| -// const WebSocket = require("ws"); |
| 6 | +const WebSocket = require("ws"); |
7 | 7 | const webpack = require("webpack");
|
8 | 8 | const runBrowser = require("../helpers/run-browser");
|
9 | 9 | const Server = require("../../lib/Server");
|
10 | 10 | 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"]; |
12 | 12 |
|
13 |
| -// const WebSocketServer = WebSocket.Server; |
| 13 | +const WebSocketServer = WebSocket.Server; |
14 | 14 | const staticDirectory = path.resolve(__dirname, "../fixtures/proxy-config");
|
15 | 15 |
|
16 | 16 | const proxyOptionPathsAsProperties = {
|
@@ -966,4 +966,105 @@ describe("proxy option", () => {
|
966 | 966 | expect(pageErrors).toMatchSnapshot("page errors");
|
967 | 967 | });
|
968 | 968 | });
|
| 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 | + }); |
969 | 1070 | });
|
0 commit comments