Skip to content

Commit ec1fba2

Browse files
Merge branch 'master' into feat/setup-middlewares-option
2 parents 79bfee2 + 0ed7d9e commit ec1fba2

File tree

4 files changed

+121
-55
lines changed

4 files changed

+121
-55
lines changed

Diff for: test/e2e/__snapshots__/api.test.js.snap.webpack4

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ exports[`API Invalidate callback should use the provided \`callback\` function:
2626

2727
exports[`API Invalidate callback should use the provided \`callback\` function: response status 1`] = `200`;
2828

29+
exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: console messages 1`] = `
30+
Array [
31+
"[HMR] Waiting for update signal from WDS...",
32+
"Hey.",
33+
"WebSocket connection to 'ws://test.host:8158/ws' failed: Error in connection establishment: net::ERR_NAME_NOT_RESOLVED",
34+
"[webpack-dev-server] JSHandle@object",
35+
"[webpack-dev-server] Disconnected!",
36+
"[webpack-dev-server] Trying to reconnect...",
37+
]
38+
`;
39+
40+
exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: page errors 1`] = `Array []`;
41+
42+
exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: response status 1`] = `200`;
43+
44+
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"`;
45+
2946
exports[`API Server.getFreePort should retry finding the port for up to defaultPortRetry times (number): console messages 1`] = `
3047
Array [
3148
"[HMR] Waiting for update signal from WDS...",

Diff for: test/e2e/__snapshots__/api.test.js.snap.webpack5

+17
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ exports[`API Invalidate callback should use the provided \`callback\` function:
2626

2727
exports[`API Invalidate callback should use the provided \`callback\` function: response status 1`] = `200`;
2828

29+
exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: console messages 1`] = `
30+
Array [
31+
"[HMR] Waiting for update signal from WDS...",
32+
"Hey.",
33+
"WebSocket connection to 'ws://test.host:8158/ws' failed: Error in connection establishment: net::ERR_NAME_NOT_RESOLVED",
34+
"[webpack-dev-server] JSHandle@object",
35+
"[webpack-dev-server] Disconnected!",
36+
"[webpack-dev-server] Trying to reconnect...",
37+
]
38+
`;
39+
40+
exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: page errors 1`] = `Array []`;
41+
42+
exports[`API Server.checkHostHeader should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object: response status 1`] = `200`;
43+
44+
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"`;
45+
2946
exports[`API Server.getFreePort should retry finding the port for up to defaultPortRetry times (number): console messages 1`] = `
3047
Array [
3148
"[HMR] Waiting for update signal from WDS...",

Diff for: test/e2e/api.test.js

+87
Original file line numberDiff line numberDiff line change
@@ -769,4 +769,91 @@ describe("API", () => {
769769
}
770770
});
771771
});
772+
773+
describe("Server.checkHostHeader", () => {
774+
it("should allow access for every requests using an IP", () => {
775+
const options = {};
776+
777+
const tests = [
778+
"192.168.1.123",
779+
"192.168.1.2:8080",
780+
"[::1]",
781+
"[::1]:8080",
782+
"[ad42::1de2:54c2:c2fa:1234]",
783+
"[ad42::1de2:54c2:c2fa:1234]:8080",
784+
];
785+
786+
const compiler = webpack(config);
787+
const server = new Server(options, compiler);
788+
789+
tests.forEach((test) => {
790+
const headers = { host: test };
791+
792+
if (!server.checkHeader(headers, "host")) {
793+
throw new Error("Validation didn't pass");
794+
}
795+
});
796+
});
797+
798+
it('should allow URLs with scheme for checking origin when the "option.client.webSocketURL" is object', async () => {
799+
const options = {
800+
port,
801+
client: {
802+
webSocketURL: {
803+
hostname: "test.host",
804+
},
805+
},
806+
webSocketServer: "ws",
807+
};
808+
const headers = {
809+
origin: "https://test.host",
810+
};
811+
812+
const compiler = webpack(config);
813+
const server = new Server(options, compiler);
814+
815+
await server.start();
816+
817+
const { page, browser } = await runBrowser();
818+
819+
const pageErrors = [];
820+
const consoleMessages = [];
821+
822+
page
823+
.on("console", (message) => {
824+
consoleMessages.push(message);
825+
})
826+
.on("pageerror", (error) => {
827+
pageErrors.push(error);
828+
});
829+
830+
const webSocketRequests = [];
831+
const client = page._client;
832+
833+
client.on("Network.webSocketCreated", (test) => {
834+
webSocketRequests.push(test);
835+
});
836+
837+
const response = await page.goto(`http://127.0.0.1:${port}/main`, {
838+
waitUntil: "networkidle0",
839+
});
840+
841+
if (!server.checkHeader(headers, "origin")) {
842+
throw new Error("Validation didn't fail");
843+
}
844+
845+
expect(webSocketRequests[0].url).toMatchSnapshot("web socket URL");
846+
847+
expect(response.status()).toMatchSnapshot("response status");
848+
849+
expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
850+
"console messages"
851+
);
852+
853+
expect(pageErrors).toMatchSnapshot("page errors");
854+
855+
await browser.close();
856+
await server.stop();
857+
});
858+
});
772859
});

Diff for: test/server/Server.test.js

-55
Original file line numberDiff line numberDiff line change
@@ -162,61 +162,6 @@ describe("Server", () => {
162162
});
163163
});
164164

165-
describe("checkHostHeader", () => {
166-
let compiler;
167-
let server;
168-
169-
beforeEach(() => {
170-
compiler = webpack(config);
171-
});
172-
173-
afterEach(async () => {
174-
await server.stop();
175-
});
176-
177-
it("should allow access for every requests using an IP", () => {
178-
const options = {};
179-
180-
const tests = [
181-
"192.168.1.123",
182-
"192.168.1.2:8080",
183-
"[::1]",
184-
"[::1]:8080",
185-
"[ad42::1de2:54c2:c2fa:1234]",
186-
"[ad42::1de2:54c2:c2fa:1234]:8080",
187-
];
188-
189-
server = new Server(options, compiler);
190-
191-
tests.forEach((test) => {
192-
const headers = { host: test };
193-
194-
if (!server.checkHeader(headers, "host")) {
195-
throw new Error("Validation didn't pass");
196-
}
197-
});
198-
});
199-
200-
it('should allow urls with scheme for checking origin when the "option.client.webSocketURL" is object', () => {
201-
const options = {
202-
client: {
203-
webSocketURL: {
204-
hostname: "test.host",
205-
},
206-
},
207-
};
208-
const headers = {
209-
origin: "https://test.host",
210-
};
211-
212-
server = new Server(options, compiler);
213-
214-
if (!server.checkHeader(headers, "origin")) {
215-
throw new Error("Validation didn't fail");
216-
}
217-
});
218-
});
219-
220165
describe("WEBPACK_SERVE environment variable", () => {
221166
const OLD_ENV = process.env;
222167

0 commit comments

Comments
 (0)