From fc8bed95251f27a24c1441307c44782f3836edd6 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Wed, 15 Dec 2021 07:23:28 +0530 Subject: [PATCH] fix: allow to pass options for custom server (#4110) --- lib/options.json | 9 ++- .../server.test.js.snap.webpack4 | 17 ++++ .../server.test.js.snap.webpack5 | 17 ++++ test/e2e/server.test.js | 77 +++++++++++++++++++ test/validate-options.test.js | 4 + 5 files changed, 123 insertions(+), 1 deletion(-) diff --git a/lib/options.json b/lib/options.json index 1746006455..cca24f9b8f 100644 --- a/lib/options.json +++ b/lib/options.json @@ -667,7 +667,14 @@ "type": "object", "properties": { "type": { - "$ref": "#/definitions/ServerType" + "anyOf": [ + { + "$ref": "#/definitions/ServerType" + }, + { + "$ref": "#/definitions/ServerString" + } + ] }, "options": { "$ref": "#/definitions/ServerOptions" diff --git a/test/e2e/__snapshots__/server.test.js.snap.webpack4 b/test/e2e/__snapshots__/server.test.js.snap.webpack4 index d2cd84c60c..d94f5a3f99 100644 --- a/test/e2e/__snapshots__/server.test.js.snap.webpack4 +++ b/test/e2e/__snapshots__/server.test.js.snap.webpack4 @@ -511,6 +511,23 @@ exports[`server option as object cacert, pfx, key and cert are buffer should han " `; +exports[`server option as object custom server with options should handle GET request to index route (/): console messages 1`] = `Array []`; + +exports[`server option as object custom server with options should handle GET request to index route (/): http options 1`] = ` +Object { + "maxHeaderSize": 16384, +} +`; + +exports[`server option as object custom server with options should handle GET request to index route (/): page errors 1`] = `Array []`; + +exports[`server option as object custom server with options should handle GET request to index route (/): response status 1`] = `200`; + +exports[`server option as object custom server with options should handle GET request to index route (/): response text 1`] = ` +"Heyo. +" +`; + exports[`server option as object options should be prioritized over http2 options should handle GET request to index route (/): console messages 1`] = `Array []`; exports[`server option as object options should be prioritized over http2 options should handle GET request to index route (/): https options 1`] = ` diff --git a/test/e2e/__snapshots__/server.test.js.snap.webpack5 b/test/e2e/__snapshots__/server.test.js.snap.webpack5 index d2cd84c60c..d94f5a3f99 100644 --- a/test/e2e/__snapshots__/server.test.js.snap.webpack5 +++ b/test/e2e/__snapshots__/server.test.js.snap.webpack5 @@ -511,6 +511,23 @@ exports[`server option as object cacert, pfx, key and cert are buffer should han " `; +exports[`server option as object custom server with options should handle GET request to index route (/): console messages 1`] = `Array []`; + +exports[`server option as object custom server with options should handle GET request to index route (/): http options 1`] = ` +Object { + "maxHeaderSize": 16384, +} +`; + +exports[`server option as object custom server with options should handle GET request to index route (/): page errors 1`] = `Array []`; + +exports[`server option as object custom server with options should handle GET request to index route (/): response status 1`] = `200`; + +exports[`server option as object custom server with options should handle GET request to index route (/): response text 1`] = ` +"Heyo. +" +`; + exports[`server option as object options should be prioritized over http2 options should handle GET request to index route (/): console messages 1`] = `Array []`; exports[`server option as object options should be prioritized over http2 options should handle GET request to index route (/): https options 1`] = ` diff --git a/test/e2e/server.test.js b/test/e2e/server.test.js index 95a86d4bc2..7b77e8d629 100644 --- a/test/e2e/server.test.js +++ b/test/e2e/server.test.js @@ -10,6 +10,7 @@ const Server = require("../../lib/Server"); const config = require("../fixtures/static-config/webpack.config"); const runBrowser = require("../helpers/run-browser"); const { skipTestOnWindows } = require("../helpers/conditional-test"); +const customHTTP = require("../helpers/custom-http"); const normalizeOptions = require("../helpers/normalize-options"); const port = require("../ports-map")["server-option"]; @@ -1643,5 +1644,81 @@ describe("server option", () => { expect(pageErrors).toMatchSnapshot("page errors"); }); }); + + describe("custom server with options", () => { + let compiler; + let server; + let createServerSpy; + let page; + let browser; + let pageErrors; + let consoleMessages; + + beforeEach(async () => { + compiler = webpack(config); + + createServerSpy = jest.spyOn(customHTTP, "createServer"); + + server = new Server( + { + static: { + directory: staticDirectory, + watch: false, + }, + server: { + type: path.join(__dirname, "../helpers/custom-http.js"), + options: { + maxHeaderSize: 16384, + }, + }, + port, + }, + compiler + ); + + await server.start(); + + ({ page, browser } = await runBrowser()); + + pageErrors = []; + consoleMessages = []; + }); + + afterEach(async () => { + createServerSpy.mockRestore(); + + await browser.close(); + await server.stop(); + }); + + it("should handle GET request to index route (/)", async () => { + page + .on("console", (message) => { + consoleMessages.push(message); + }) + .on("pageerror", (error) => { + pageErrors.push(error); + }); + + const response = await page.goto(`http://127.0.0.1:${port}/`, { + waitUntil: "networkidle0", + }); + + const HTTPVersion = await page.evaluate( + () => performance.getEntries()[0].nextHopProtocol + ); + + expect(HTTPVersion).toEqual("http/1.1"); + expect( + normalizeOptions(createServerSpy.mock.calls[0][0]) + ).toMatchSnapshot("http options"); + expect(response.status()).toMatchSnapshot("response status"); + expect(await response.text()).toMatchSnapshot("response text"); + expect( + consoleMessages.map((message) => message.text()) + ).toMatchSnapshot("console messages"); + expect(pageErrors).toMatchSnapshot("page errors"); + }); + }); }); }); diff --git a/test/validate-options.test.js b/test/validate-options.test.js index c14546519c..60880d572c 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -393,6 +393,7 @@ const tests = { "http", "https", "spdy", + "custom-server.js", { type: "http", }, @@ -402,6 +403,9 @@ const tests = { { type: "spdy", }, + { + type: "custom-server.js", + }, { type: "https", options: {