Skip to content

Commit

Permalink
test: add cases for webSocketURL with server option (#4346)
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 authored Mar 19, 2022
1 parent 84b4774 commit 95e26fe
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 0 deletions.
44 changes: 44 additions & 0 deletions test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack4
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,50 @@ Array [

exports[`web socket server URL should work with "https" option ("ws"): page errors 1`] = `Array []`;

exports[`web socket server URL should work with "server: 'https'" option ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work with "server: 'https'" option ("sockjs"): page errors 1`] = `Array []`;

exports[`web socket server URL should work with "server: 'https'" option ("ws"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work with "server: 'https'" option ("ws"): page errors 1`] = `Array []`;

exports[`web socket server URL should work with "server: 'spdy'" option ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work with "server: 'spdy'" option ("sockjs"): page errors 1`] = `Array []`;

exports[`web socket server URL should work with "server: 'spdy'" option ("ws"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work with "server: 'spdy'" option ("ws"): page errors 1`] = `Array []`;

exports[`web socket server URL should work with default "/ws" value of the "client.webSocketURL.pathname" option ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down
44 changes: 44 additions & 0 deletions test/e2e/__snapshots__/web-socket-server-url.test.js.snap.webpack5
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,50 @@ Array [

exports[`web socket server URL should work with "https" option ("ws"): page errors 1`] = `Array []`;

exports[`web socket server URL should work with "server: 'https'" option ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work with "server: 'https'" option ("sockjs"): page errors 1`] = `Array []`;

exports[`web socket server URL should work with "server: 'https'" option ("ws"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work with "server: 'https'" option ("ws"): page errors 1`] = `Array []`;

exports[`web socket server URL should work with "server: 'spdy'" option ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work with "server: 'spdy'" option ("sockjs"): page errors 1`] = `Array []`;

exports[`web socket server URL should work with "server: 'spdy'" option ("ws"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
"Hey.",
"[webpack-dev-server] Hot Module Replacement enabled.",
"[webpack-dev-server] Live Reloading enabled.",
]
`;

exports[`web socket server URL should work with "server: 'spdy'" option ("ws"): page errors 1`] = `Array []`;

exports[`web socket server URL should work with default "/ws" value of the "client.webSocketURL.pathname" option ("sockjs"): console messages 1`] = `
Array [
"[HMR] Waiting for update signal from WDS...",
Expand Down
128 changes: 128 additions & 0 deletions test/e2e/web-socket-server-url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,134 @@ describe("web socket server URL", () => {
await server.stop();
});

it(`should work with "server: 'https'" option ("${webSocketServer}")`, async () => {
const hostname = "127.0.0.1";
const compiler = webpack(config);
const devServerOptions = {
webSocketServer,
port: port1,
server: "https",
};
const server = new Server(devServerOptions, 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 = [];

if (webSocketServer === "ws") {
const client = page._client;

client.on("Network.webSocketCreated", (test) => {
webSocketRequests.push(test);
});
} else {
page.on("request", (request) => {
if (/\/ws\//.test(request.url())) {
webSocketRequests.push({ url: request.url() });
}
});
}

await page.goto(`https://${hostname}:${port1}/`, {
waitUntil: "networkidle0",
});

const webSocketRequest = webSocketRequests[0];

if (webSocketServer === "ws") {
expect(webSocketRequest.url).toContain(`wss://${hostname}:${port1}/ws`);
} else {
expect(webSocketRequest.url).toContain(
`https://${hostname}:${port1}/ws`
);
}

expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);
expect(pageErrors).toMatchSnapshot("page errors");

await browser.close();
await server.stop();
});

it(`should work with "server: 'spdy'" option ("${webSocketServer}")`, async () => {
const hostname = "127.0.0.1";
const compiler = webpack(config);
const devServerOptions = {
webSocketServer,
port: port1,
server: "spdy",
};
const server = new Server(devServerOptions, 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 = [];

if (webSocketServer === "ws") {
const client = page._client;

client.on("Network.webSocketCreated", (test) => {
webSocketRequests.push(test);
});
} else {
page.on("request", (request) => {
if (/\/ws\//.test(request.url())) {
webSocketRequests.push({ url: request.url() });
}
});
}

await page.goto(`https://${hostname}:${port1}/`, {
waitUntil: "networkidle0",
});

const webSocketRequest = webSocketRequests[0];

if (webSocketServer === "ws") {
expect(webSocketRequest.url).toContain(`wss://${hostname}:${port1}/ws`);
} else {
expect(webSocketRequest.url).toContain(
`https://${hostname}:${port1}/ws`
);
}

expect(consoleMessages.map((message) => message.text())).toMatchSnapshot(
"console messages"
);
expect(pageErrors).toMatchSnapshot("page errors");

await browser.close();
await server.stop();
});

it(`should work when "port" option is "auto" ("${webSocketServer}")`, async () => {
process.env.WEBPACK_DEV_SERVER_BASE_PORT = 50000;

Expand Down

0 comments on commit 95e26fe

Please sign in to comment.