Skip to content

Commit

Permalink
test: add e2e tests for checkHostHeader method (#4118)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Dec 18, 2021
1 parent d9b490d commit 0ed7d9e
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 55 deletions.
17 changes: 17 additions & 0 deletions test/e2e/__snapshots__/api.test.js.snap.webpack4
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
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
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
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

0 comments on commit 0ed7d9e

Please sign in to comment.