From 53dac336947eede70669e0252f1634f66628e7c7 Mon Sep 17 00:00:00 2001 From: Dorian Niemiec Date: Sat, 31 Aug 2024 20:48:55 +0200 Subject: [PATCH] Add static file serving test --- ...aticFileServingAndDirectoryListing.test.js | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tests/middleware/staticFileServingAndDirectoryListing.test.js b/tests/middleware/staticFileServingAndDirectoryListing.test.js index 759e587..e05fe0d 100644 --- a/tests/middleware/staticFileServingAndDirectoryListing.test.js +++ b/tests/middleware/staticFileServingAndDirectoryListing.test.js @@ -35,6 +35,8 @@ describe("Static file serving and directory listings middleware", () => { res.writeHead(statusCode, { "Content-Type": "text/plain" }); res.end(statusCode + " " + http.STATUS_CODES[statusCode]); }; + res.head = ""; + res.foot = ""; logFacilities = { errmessage: jest.fn(), resmessage: jest.fn(), @@ -182,4 +184,61 @@ describe("Static file serving and directory listings middleware", () => { expect(res._getData()).toContain("file1.txt"); expect(res._getData()).toContain("file2.txt"); }); + + test("should serve static file if the path is a file", async () => { + req.headers["accept-encoding"] = undefined; + req.path = "/file.txt"; + req.parsedURL.pathname = "/file.txt"; + req.originalParsedURL.pathname = "/file.txt"; + + fs.stat.mockImplementation((path, cb) => { + if (!path.match(/(?:^|\/)file\.txt$/)) { + cb(null, { isDirectory: () => true, isFile: () => false }); + } else { + cb(null, { + isDirectory: () => false, + isFile: () => true, + size: 9 + }); + } + }); + + let mockEndListener = () => {}; + let mockDataSent = false; + const mockStream = { + on: (event, listener) => { + if (event == "open") { + listener(); + } else if (event == "data") { + if (!mockDataSent) { + listener(Buffer.from("mock data")); + mockDataSent = true; + } + mockEndListener(); + } else if (event == "end") { + mockEndListener = listener; + if (mockDataSent) mockEndListener(); + } + return mockStream; + }, + once: (event, listener) => { + mockStream.on(event, listener); + }, + pipe: (destStream) => { + if (!mockDataSent) { + destStream.end("mock data"); + } + return destStream; + } + }; + + fs.createReadStream.mockImplementation(() => { + return mockStream; + }); + + await middleware(req, res, logFacilities, config, next); + + expect(res.statusCode).toBe(200); + expect(res._getData()).toBe("mock data"); + }) });