Skip to content

Commit

Permalink
[FIX] nonReadRequests middleware: Use native response API
Browse files Browse the repository at this point in the history
Using the native "statusCode" property instead of the express method to
set the status code to allow using the middleware with a different server
framework or as a native Node.js server handler.

This also aligns with other existing middleware that set the statusCode.
  • Loading branch information
matz3 committed Oct 21, 2020
1 parent 4dcae7c commit 2d2325f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lib/middleware/nonReadRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function createMiddleware() {
// Handle anything but read operations *before* the serveIndex middleware
// as it will reject them with a 405 (Method not allowed) instead of 404 like our old tooling
if (req.method !== "GET" && req.method !== "HEAD" && req.method !== "OPTIONS") {
res.status(404).end(`Cannot ${req.method} ${req.url}`);
res.statusCode = 404;
res.end(`Cannot ${req.method} ${req.url}`);
} else {
next();
}
Expand Down
11 changes: 4 additions & 7 deletions test/lib/server/middleware/nonReadRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ test("Non read requests results in status 404 and an error message", (t) => {
const methods = ["POST", "PUT", "DELETE"];
methods.forEach(function(method) {
res = {
status: function(status) {
t.deepEqual(status, 404, "Status should be 404");
return {
end: function(message) {
t.deepEqual(message, "Cannot " + method + " /somePath", "Finished with error message.");
}
};
statusCode: 200,
end: function(message) {
t.deepEqual(res.statusCode, 404, "Status should be 404");
t.deepEqual(message, "Cannot " + method + " /somePath", "Finished with error message.");
}
};

Expand Down

0 comments on commit 2d2325f

Please sign in to comment.