fix: add support for res.setHeader and res.removeHeader methods

This commit is contained in:
Dorian Niemiec 2024-12-21 22:40:43 +01:00
parent ec2c148681
commit fb19abc550
2 changed files with 24 additions and 12 deletions

View file

@ -68,6 +68,8 @@ module.exports = function (req, res, logFacilities, config, next) {
} }
// Capture the response // Capture the response
const originalSetHeader = res.setHeader.bind(res);
const originalRemoveHeader = res.removeHeader.bind(res);
const originalWriteHead = res.writeHead.bind(res); const originalWriteHead = res.writeHead.bind(res);
const originalWrite = res.write.bind(res); const originalWrite = res.write.bind(res);
const originalEnd = res.end.bind(res); const originalEnd = res.end.bind(res);
@ -77,6 +79,16 @@ module.exports = function (req, res, logFacilities, config, next) {
let maximumCachedResponseSizeExceeded = false; let maximumCachedResponseSizeExceeded = false;
let piping = false; let piping = false;
res.setHeader = function (name, value) {
writtenHeaders[name.toLowerCase()] = value;
originalSetHeader(name, value);
};
res.removeHeader = function (name) {
delete writtenHeaders[name.toLowerCase()];
originalRemoveHeader(name);
};
res.writeHead = function (statusCode, statusCodeDescription, headers) { res.writeHead = function (statusCode, statusCodeDescription, headers) {
const properHeaders = headers ? headers : statusCodeDescription; const properHeaders = headers ? headers : statusCodeDescription;
if (typeof properHeaders === "object" && properHeaders !== null) { if (typeof properHeaders === "object" && properHeaders !== null) {
@ -85,7 +97,7 @@ module.exports = function (req, res, logFacilities, config, next) {
}); });
} }
writtenStatusCode = statusCode; writtenStatusCode = statusCode;
res.setHeader("X-SVRJS-Cache", "MISS"); originalSetHeader("X-SVRJS-Cache", "MISS");
if (headers || typeof statusCodeDescription !== "object") { if (headers || typeof statusCodeDescription !== "object") {
originalWriteHead( originalWriteHead(
writtenStatusCode, writtenStatusCode,

View file

@ -14,12 +14,9 @@ jest.mock("../src/utils/cacheControlUtils.js", () => ({
})); }));
describe("SVR.JS Cache mod", () => { describe("SVR.JS Cache mod", () => {
let req, res, logFacilities, config, next, resWriteHead, resEnd; let req, res, logFacilities, config, next;
beforeEach(() => { beforeEach(() => {
resWriteHead = jest.fn();
resEnd = jest.fn();
req = { req = {
method: "GET", method: "GET",
headers: {}, headers: {},
@ -29,9 +26,9 @@ describe("SVR.JS Cache mod", () => {
res = { res = {
headers: {}, headers: {},
writeHead: resWriteHead, writeHead: jest.fn(),
write: jest.fn(), write: jest.fn(),
end: resEnd, end: jest.fn(),
setHeader: jest.fn(), setHeader: jest.fn(),
getHeaderNames: jest.fn(() => []), getHeaderNames: jest.fn(() => []),
getHeaders: jest.fn(() => ({})), getHeaders: jest.fn(() => ({})),
@ -96,6 +93,11 @@ describe("SVR.JS Cache mod", () => {
// Reset mocks for the second invocation // Reset mocks for the second invocation
jest.clearAllMocks(); jest.clearAllMocks();
next.mockReset(); next.mockReset();
res.setHeader = jest.fn();
res.removeHeader = jest.fn();
res.writeHead = jest.fn();
res.write = jest.fn();
res.end = jest.fn();
// Second request: retrieve from cache // Second request: retrieve from cache
parseCacheControl.mockReturnValue({}); parseCacheControl.mockReturnValue({});
@ -108,14 +110,12 @@ describe("SVR.JS Cache mod", () => {
"The response is cached." "The response is cached."
); );
expect(res.setHeader).toHaveBeenCalledWith("X-SVRJS-Cache", "HIT"); expect(res.setHeader).toHaveBeenCalledWith("X-SVRJS-Cache", "HIT");
expect(resWriteHead).toHaveBeenCalledWith(200, { expect(res.writeHead).toHaveBeenCalledWith(200, {
"cache-control": "max-age=300", "cache-control": "max-age=300",
"content-type": "application/json" "content-type": "application/json"
}); });
expect(resEnd).toHaveBeenCalledWith( expect(res.end).toHaveBeenCalledWith(
Buffer.from("cached response body", "latin1"), Buffer.from("cached response body", "latin1")
undefined,
undefined
); );
expect(next).not.toHaveBeenCalled(); // No middleware should be called expect(next).not.toHaveBeenCalled(); // No middleware should be called
}); });