Skip to content

Commit

Permalink
test: fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Apr 17, 2024
1 parent 66b3584 commit 1449a49
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 52 deletions.
5 changes: 2 additions & 3 deletions lib/Server.js
Expand Up @@ -1814,9 +1814,8 @@ class Server {
return next();
}

// TODO add this to test
res.statusCode = 403;
res.send("Invalid Host header");
res.end("Invalid Host header");
},
);
}
Expand Down Expand Up @@ -1924,7 +1923,7 @@ class Server {

/** @type {import("webpack-dev-middleware").API<Request, Response>}*/
(middleware).waitUntilValid((stats) => {
res.setHeader("Content-Type", "text/html");
res.setHeader("Content-Type", "text/html; charset=utf-8");

// HEAD requests should not return body content
if (req.method === "HEAD") {
Expand Down
53 changes: 26 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -119,7 +119,7 @@
"memfs": "^4.6.0",
"npm-run-all": "^4.1.5",
"prettier": "^3.2.4",
"puppeteer": "^22.1.0",
"puppeteer": "^22.6.5",
"readable-stream": "^4.5.2",
"require-from-string": "^2.0.2",
"sockjs-client": "^1.6.1",
Expand Down
16 changes: 12 additions & 4 deletions test/e2e/__snapshots__/allowed-hosts.test.js.snap.webpack5
Expand Up @@ -262,15 +262,23 @@ exports[`allowed hosts should connect web socket client using localhost to web s

exports[`allowed hosts should connect web socket client using localhost to web socket server with the "auto" value ("ws"): page errors 1`] = `[]`;

exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("sockjs"): console messages 1`] = `[]`;
exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("sockjs"): console messages 1`] = `
[
"Failed to load resource: the server responded with a status of 403 (Forbidden)",
]
`;

exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("sockjs"): html 1`] = `"<html><head></head><body>Invalid Host header</body></html>"`;
exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("sockjs"): html 1`] = `"<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">Invalid Host header</pre></body></html>"`;

exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("sockjs"): page errors 1`] = `[]`;

exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("ws"): console messages 1`] = `[]`;
exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("ws"): console messages 1`] = `
[
"Failed to load resource: the server responded with a status of 403 (Forbidden)",
]
`;

exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("ws"): html 1`] = `"<html><head></head><body>Invalid Host header</body></html>"`;
exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("ws"): html 1`] = `"<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">Invalid Host header</pre></body></html>"`;

exports[`allowed hosts should disconnect web client using localhost to web socket server with the "auto" value ("ws"): page errors 1`] = `[]`;

Expand Down
18 changes: 12 additions & 6 deletions test/e2e/on-listening.test.js
Expand Up @@ -26,12 +26,18 @@ describe("onListening option", () => {

onListeningIsRunning = true;

devServer.app.get("/listening/some/path", (_, response) => {
response.send("listening");
});

devServer.app.post("/listening/some/path", (_, response) => {
response.send("listening POST");
devServer.app.use("/listening/some/path", (req, res, next) => {
if (req.method === "GET") {
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end("listening");
return;
} else if (req.method === "POST") {
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end("listening POST");
return;
}

return next();
});
},
port,
Expand Down
30 changes: 19 additions & 11 deletions test/e2e/setup-middlewares.test.js
Expand Up @@ -23,35 +23,43 @@ describe("setupMiddlewares option", () => {
throw new Error("webpack-dev-server is not defined");
}

devServer.app.get("/setup-middleware/some/path", (_, response) => {
response.send("setup-middlewares option GET");
});

devServer.app.post("/setup-middleware/some/path", (_, response) => {
response.send("setup-middlewares option POST");
devServer.app.use("/setup-middleware/some/path", (req, res, next) => {
if (req.method === "GET") {
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end("setup-middlewares option GET");
return;
} else if (req.method === "POST") {
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end("setup-middlewares option POST");
return;
}

return next();
});

middlewares.push({
name: "hello-world-test-two",
middleware: (req, res, next) => {
if (req.path !== "/foo/bar/baz") {
if (req.url !== "/foo/bar/baz") {
next();

return;
}

res.send("Hello World without path!");
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end("Hello World without path!");
},
});
middlewares.push({
name: "hello-world-test-one",
path: "/foo/bar",
middleware: (req, res) => {
res.send("Hello World with path!");
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end("Hello World with path!");
},
});
middlewares.push((req, res) => {
res.send("Hello World as function!");
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end("Hello World as function!");
});

return middlewares;
Expand Down

0 comments on commit 1449a49

Please sign in to comment.