From 2d2325f638820d25738ddbd56afe0d104e37f2e0 Mon Sep 17 00:00:00 2001 From: Matthias Osswald Date: Tue, 20 Oct 2020 17:16:44 +0200 Subject: [PATCH] [FIX] nonReadRequests middleware: Use native response API 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. --- lib/middleware/nonReadRequests.js | 3 ++- test/lib/server/middleware/nonReadRequests.js | 11 ++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/middleware/nonReadRequests.js b/lib/middleware/nonReadRequests.js index 2a1d248c..fac995af 100644 --- a/lib/middleware/nonReadRequests.js +++ b/lib/middleware/nonReadRequests.js @@ -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(); } diff --git a/test/lib/server/middleware/nonReadRequests.js b/test/lib/server/middleware/nonReadRequests.js index 383adedd..c9b6ce96 100644 --- a/test/lib/server/middleware/nonReadRequests.js +++ b/test/lib/server/middleware/nonReadRequests.js @@ -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."); } };