Skip to content

test: add e2e tests for checkHostHeader method #4118

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

Merged
merged 1 commit into from
Dec 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions test/e2e/__snapshots__/api.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -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...",
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/__snapshots__/api.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -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...",
Expand Down
87 changes: 87 additions & 0 deletions test/e2e/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
55 changes: 0 additions & 55 deletions test/server/Server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down