Skip to content

Commit

Permalink
Set Content Encoding Header Correctly (#9741)
Browse files Browse the repository at this point in the history
* set content encoding header correctly

Signed-off-by: Jesse Nelson <[email protected]>
  • Loading branch information
jnels124 authored Nov 12, 2024
1 parent a58dfac commit f1db63b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
});

Expand Down Expand Up @@ -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);
});

Expand All @@ -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);
});
});
Expand Down
5 changes: 2 additions & 3 deletions hedera-mirror-rest/middleware/responseCacheHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

Expand Down

0 comments on commit f1db63b

Please sign in to comment.