1
0
Fork 0
forked from svrjs/svrjs
This repository has been archived on 2024-11-10. You can view files and clone it, but cannot push or open issues or pull requests.
svrjs/tests/middleware/responseHeaders.test.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

const middleware = require("../../src/middleware/responseHeaders.js");
describe("Response header setting middleware", () => {
let req, res, next, config, logFacilities;
beforeEach(() => {
req = { isProxy: false };
res = { setHeader: jest.fn() };
next = jest.fn();
config = {
getCustomHeaders: jest.fn(() => ({ "X-Custom-Header": "custom-value" })),
};
logFacilities = {};
});
test("should set custom headers if req.isProxy is false", () => {
middleware(req, res, logFacilities, config, next);
expect(res.setHeader).toHaveBeenCalledWith(
"X-Custom-Header",
"custom-value",
);
expect(next).toHaveBeenCalled();
});
test("should not set custom headers if req.isProxy is true", () => {
req.isProxy = true;
middleware(req, res, logFacilities, config, next);
expect(res.setHeader).not.toHaveBeenCalled();
expect(next).toHaveBeenCalled();
});
test("should call next even if an error occurs while setting headers", () => {
res.setHeader.mockImplementation(() => {
throw new Error("test error");
});
middleware(req, res, logFacilities, config, next);
expect(next).toHaveBeenCalled();
});
test("should have proxySafe property set to true", () => {
expect(middleware.proxySafe).toBe(true);
});
});