diff --git a/test/e2e/__snapshots__/api.test.js.snap.webpack4 b/test/e2e/__snapshots__/api.test.js.snap.webpack4 index 33b57c065b..d53956a485 100644 --- a/test/e2e/__snapshots__/api.test.js.snap.webpack4 +++ b/test/e2e/__snapshots__/api.test.js.snap.webpack4 @@ -26,6 +26,23 @@ exports[`API Invalidate callback should use the provided \`callback\` function: exports[`API Invalidate callback should use the provided \`callback\` function: response status 1`] = `200`; +exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "WebSocket connection to 'ws://test.host:8158/ws' failed: Error in connection establishment: net::ERR_NAME_NOT_RESOLVED", + "[webpack-dev-server] JSHandle@object", + "[webpack-dev-server] Disconnected!", + "[webpack-dev-server] Trying to reconnect...", +] +`; + +exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: page errors 1`] = `Array []`; + +exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: response status 1`] = `200`; + +exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: web socket URL 1`] = `"ws://test.host:8158/ws"`; + exports[`API Server.getFreePort should retry finding the port for up to defaultPortRetry times (number): console messages 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", diff --git a/test/e2e/__snapshots__/api.test.js.snap.webpack5 b/test/e2e/__snapshots__/api.test.js.snap.webpack5 index 33b57c065b..d53956a485 100644 --- a/test/e2e/__snapshots__/api.test.js.snap.webpack5 +++ b/test/e2e/__snapshots__/api.test.js.snap.webpack5 @@ -26,6 +26,23 @@ exports[`API Invalidate callback should use the provided \`callback\` function: exports[`API Invalidate callback should use the provided \`callback\` function: response status 1`] = `200`; +exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: console messages 1`] = ` +Array [ + "[HMR] Waiting for update signal from WDS...", + "Hey.", + "WebSocket connection to 'ws://test.host:8158/ws' failed: Error in connection establishment: net::ERR_NAME_NOT_RESOLVED", + "[webpack-dev-server] JSHandle@object", + "[webpack-dev-server] Disconnected!", + "[webpack-dev-server] Trying to reconnect...", +] +`; + +exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: page errors 1`] = `Array []`; + +exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: response status 1`] = `200`; + +exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: web socket URL 1`] = `"ws://test.host:8158/ws"`; + exports[`API Server.getFreePort should retry finding the port for up to defaultPortRetry times (number): console messages 1`] = ` Array [ "[HMR] Waiting for update signal from WDS...", diff --git a/test/e2e/api.test.js b/test/e2e/api.test.js index e653a70ba9..740b1a3d7f 100644 --- a/test/e2e/api.test.js +++ b/test/e2e/api.test.js @@ -769,4 +769,91 @@ describe("API", () => { } }); }); + + describe("Server.checkHostHeader", () => { + it("should allow access for every requests using an IP", () => { + const options = {}; + + const tests = [ + "192.168.1.123", + "192.168.1.2:8080", + "[::1]", + "[::1]:8080", + "[ad42::1de2:54c2:c2fa:1234]", + "[ad42::1de2:54c2:c2fa:1234]:8080", + ]; + + const compiler = webpack(config); + const server = new Server(options, compiler); + + tests.forEach((test) => { + const headers = { host: test }; + + if (!server.checkHeader(headers, "host")) { + throw new Error("Validation didn't pass"); + } + }); + }); + + it('should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object', async () => { + const options = { + port, + client: { + webSocketURL: { + hostname: "test.host", + }, + }, + webSocketServer: "ws", + }; + const headers = { + origin: "https://test.host", + }; + + const compiler = webpack(config); + const server = new Server(options, compiler); + + await server.start(); + + const { page, browser } = await runBrowser(); + + const pageErrors = []; + const consoleMessages = []; + + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const webSocketRequests = []; + const client = page._client; + + client.on("Network.webSocketCreated", (test) => { + webSocketRequests.push(test); + }); + + const response = await page.goto(`http://127.0.0.1:${port}/main`, { + waitUntil: "networkidle0", + }); + + if (!server.checkHeader(headers, "origin")) { + throw new Error("Validation didn't fail"); + } + + expect(webSocketRequests[0].url).toMatchSnapshot("web socket URL"); + + expect(response.status()).toMatchSnapshot("response status"); + + expect(consoleMessages.map((message) => message.text())).toMatchSnapshot( + "console messages" + ); + + expect(pageErrors).toMatchSnapshot("page errors"); + + await browser.close(); + await server.stop(); + }); + }); }); diff --git a/test/server/Server.test.js b/test/server/Server.test.js index bccf10a6c0..10b46c4d0a 100644 --- a/test/server/Server.test.js +++ b/test/server/Server.test.js @@ -162,61 +162,6 @@ describe("Server", () => { }); }); - describe("checkHostHeader", () => { - let compiler; - let server; - - beforeEach(() => { - compiler = webpack(config); - }); - - afterEach(async () => { - await server.stop(); - }); - - it("should allow access for every requests using an IP", () => { - const options = {}; - - const tests = [ - "192.168.1.123", - "192.168.1.2:8080", - "[::1]", - "[::1]:8080", - "[ad42::1de2:54c2:c2fa:1234]", - "[ad42::1de2:54c2:c2fa:1234]:8080", - ]; - - server = new Server(options, compiler); - - tests.forEach((test) => { - const headers = { host: test }; - - if (!server.checkHeader(headers, "host")) { - throw new Error("Validation didn't pass"); - } - }); - }); - - it('should allow urls with scheme for checking origin when the "option.client.webSocketURL" is object', () => { - const options = { - client: { - webSocketURL: { - hostname: "test.host", - }, - }, - }; - const headers = { - origin: "https://test.host", - }; - - server = new Server(options, compiler); - - if (!server.checkHeader(headers, "origin")) { - throw new Error("Validation didn't fail"); - } - }); - }); - describe("WEBPACK_SERVE environment variable", () => { const OLD_ENV = process.env;