Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow to pass options for custom server #4110

Merged
merged 1 commit into from Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/options.json
Expand Up @@ -667,7 +667,14 @@
"type": "object",
"properties": {
"type": {
"$ref": "#/definitions/ServerType"
"anyOf": [
{
"$ref": "#/definitions/ServerType"
},
{
"$ref": "#/definitions/ServerString"
}
]
},
"options": {
"$ref": "#/definitions/ServerOptions"
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/__snapshots__/server.test.js.snap.webpack4
Expand Up @@ -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`] = `
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/__snapshots__/server.test.js.snap.webpack5
Expand Up @@ -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`] = `
Expand Down
77 changes: 77 additions & 0 deletions test/e2e/server.test.js
Expand Up @@ -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"];

Expand Down Expand Up @@ -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");
});
});
});
});
4 changes: 4 additions & 0 deletions test/validate-options.test.js
Expand Up @@ -393,6 +393,7 @@ const tests = {
"http",
"https",
"spdy",
"custom-server.js",
{
type: "http",
},
Expand All @@ -402,6 +403,9 @@ const tests = {
{
type: "spdy",
},
{
type: "custom-server.js",
},
{
type: "https",
options: {
Expand Down