diff --git a/hedera-mirror-rest/__tests__/middleware/responseCacheHandler.test.js b/hedera-mirror-rest/__tests__/middleware/responseCacheHandler.test.js index dc7494c8de..a8e0b5ad9e 100644 --- a/hedera-mirror-rest/__tests__/middleware/responseCacheHandler.test.js +++ b/hedera-mirror-rest/__tests__/middleware/responseCacheHandler.test.js @@ -81,13 +81,13 @@ describe('Response cache middleware', () => { getHeaders: jest.fn(), headers: { 'cache-control': `public, max-age=${cacheControlMaxAge}`, - 'content-encoding': 'gzip' }, locals: [], removeHeader: jest.fn(), send: jest.fn(), set: jest.fn(), status: jest.fn(), + setHeader: jest.fn(), }; }); @@ -246,7 +246,8 @@ describe('Response cache middleware', () => { await responseCacheCheckHandler(mockRequest, mockResponse, null); expect(mockResponse.send).toBeCalledWith(cachedBody); - expect(mockResponse.set).toHaveBeenNthCalledWith(1, Object.assign(cachedHeaders, {'content-encoding': 'gzip'})); + expect(mockResponse.set).toHaveBeenNthCalledWith(1, cachedHeaders); + expect(mockResponse.setHeader).toHaveBeenNthCalledWith(1, 'content-encoding', 'gzip'); expect(mockResponse.status).toBeCalledWith(httpStatusCodes.OK.code); }); @@ -264,6 +265,7 @@ describe('Response cache middleware', () => { await responseCacheCheckHandler(mockRequest, mockResponse, null); expect(mockResponse.send).toBeCalledWith(cachedBody); expect(mockResponse.set).toHaveBeenNthCalledWith(1, cachedHeaders); + expect(mockResponse.setHeader).not.toBeCalled(); expect(mockResponse.status).toBeCalledWith(httpStatusCodes.OK.code); }); }); diff --git a/hedera-mirror-rest/middleware/responseCacheHandler.js b/hedera-mirror-rest/middleware/responseCacheHandler.js index 1c570cb15c..87876d20eb 100644 --- a/hedera-mirror-rest/middleware/responseCacheHandler.js +++ b/hedera-mirror-rest/middleware/responseCacheHandler.js @@ -50,8 +50,6 @@ const responseCacheCheckHandler = async (req, res, next) => { res.status(statusCode); if (isHead || clientCached) { - res.set(cachedResponse.headers); - res.status(statusCode); res.end(); } else { if (cachedResponse.compressed) { @@ -60,9 +58,10 @@ const responseCacheCheckHandler = async (req, res, next) => { if (!acceptsGzip) { cachedResponse.body = unzipSync(cachedResponse.body).toString(); } else { - cachedResponse.headers[CONTENT_ENCODING_HEADER] = 'gzip'; + res.setHeader(CONTENT_ENCODING_HEADER, 'gzip'); } } + res.send(cachedResponse.body); }