2024-08-30 22:11:16 +02:00
|
|
|
const middleware = require("../../src/middleware/rewriteURL.js");
|
|
|
|
const createRegex = require("../../src/utils/createRegex.js");
|
|
|
|
const sanitizeURL = require("../../src/utils/urlSanitizer.js");
|
|
|
|
const parseURL = require("../../src/utils/urlParser.js");
|
|
|
|
|
|
|
|
jest.mock("fs");
|
|
|
|
jest.mock("../../src/utils/urlSanitizer.js");
|
|
|
|
jest.mock("../../src/utils/urlParser.js");
|
|
|
|
jest.mock("../../src/utils/createRegex.js");
|
|
|
|
|
|
|
|
describe("rewriteURL middleware", () => {
|
|
|
|
let req, res, logFacilities, config, next;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
req = {
|
|
|
|
parsedURL: {
|
|
|
|
pathname: "/test",
|
|
|
|
search: "",
|
2024-09-01 21:54:42 +02:00
|
|
|
hash: ""
|
2024-08-30 22:11:16 +02:00
|
|
|
},
|
|
|
|
url: "/test",
|
|
|
|
headers: {
|
2024-09-01 21:54:42 +02:00
|
|
|
host: "test.com"
|
2024-08-30 22:11:16 +02:00
|
|
|
},
|
|
|
|
socket: {
|
|
|
|
encrypted: false,
|
2024-09-01 21:54:42 +02:00
|
|
|
localAddress: "127.0.0.1"
|
|
|
|
}
|
2024-08-30 22:11:16 +02:00
|
|
|
};
|
|
|
|
res = {
|
2024-09-01 21:54:42 +02:00
|
|
|
error: jest.fn()
|
2024-08-30 22:11:16 +02:00
|
|
|
};
|
|
|
|
logFacilities = {
|
|
|
|
resmessage: jest.fn(),
|
2024-09-01 21:54:42 +02:00
|
|
|
errmessage: jest.fn()
|
2024-08-30 22:11:16 +02:00
|
|
|
};
|
|
|
|
config = {
|
|
|
|
rewriteMap: [],
|
|
|
|
domain: "test.com",
|
2024-09-01 21:54:42 +02:00
|
|
|
allowDoubleSlashes: false
|
2024-08-30 22:11:16 +02:00
|
|
|
};
|
|
|
|
next = jest.fn();
|
|
|
|
|
|
|
|
// Make mocks call actual functions
|
|
|
|
createRegex.mockImplementation((...params) =>
|
2024-09-01 21:54:42 +02:00
|
|
|
jest.requireActual("../../src/utils/createRegex.js")(...params)
|
2024-08-30 22:11:16 +02:00
|
|
|
);
|
|
|
|
parseURL.mockImplementation((...params) =>
|
2024-09-01 21:54:42 +02:00
|
|
|
jest.requireActual("../../src/utils/urlParser.js")(...params)
|
2024-08-30 22:11:16 +02:00
|
|
|
);
|
|
|
|
sanitizeURL.mockImplementation((...params) =>
|
2024-09-01 21:54:42 +02:00
|
|
|
jest.requireActual("../../src/utils/urlSanitizer.js")(...params)
|
2024-08-30 22:11:16 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should call next if URL is not rewritten", () => {
|
|
|
|
middleware(req, res, logFacilities, config, next);
|
|
|
|
expect(next).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should return 400 if URL decoding fails", () => {
|
|
|
|
req.parsedURL.pathname = "%";
|
|
|
|
middleware(req, res, logFacilities, config, next);
|
|
|
|
expect(res.error).toHaveBeenCalledWith(400);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should return 500 if rewriteURL callback returns an error", () => {
|
|
|
|
config.rewriteMap = [
|
|
|
|
{
|
|
|
|
host: "test.com",
|
|
|
|
definingRegex: "/.*/",
|
|
|
|
replacements: [
|
|
|
|
{
|
|
|
|
regex: "/.*/",
|
2024-09-01 21:54:42 +02:00
|
|
|
replacement: "error"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2024-08-30 22:11:16 +02:00
|
|
|
];
|
|
|
|
createRegex.mockImplementation(() => {
|
|
|
|
throw new Error("Test error");
|
|
|
|
});
|
|
|
|
middleware(req, res, logFacilities, config, next);
|
|
|
|
expect(res.error).toHaveBeenCalledWith(500, expect.any(Error));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should return 400 if parsedURL is invalid", () => {
|
|
|
|
config.rewriteMap = [
|
|
|
|
{
|
|
|
|
host: "test.com",
|
|
|
|
definingRegex: "/.*/",
|
|
|
|
replacements: [
|
|
|
|
{
|
|
|
|
regex: "/.*/",
|
2024-09-01 21:54:42 +02:00
|
|
|
replacement: "/new"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2024-08-30 22:11:16 +02:00
|
|
|
];
|
|
|
|
parseURL.mockImplementation(() => {
|
|
|
|
throw new Error("Test error");
|
|
|
|
});
|
|
|
|
middleware(req, res, logFacilities, config, next);
|
|
|
|
expect(res.error).toHaveBeenCalledWith(400, expect.any(Error));
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should return 403 if URL is sanitized", () => {
|
|
|
|
config.rewriteMap = [
|
|
|
|
{
|
|
|
|
host: "test.com",
|
|
|
|
definingRegex: "/.*/",
|
|
|
|
replacements: [
|
|
|
|
{
|
|
|
|
regex: "/.*/",
|
2024-09-01 21:54:42 +02:00
|
|
|
replacement: "/new"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2024-08-30 22:11:16 +02:00
|
|
|
];
|
|
|
|
sanitizeURL.mockReturnValue("/sanitized");
|
|
|
|
middleware(req, res, logFacilities, config, next);
|
|
|
|
expect(res.error).toHaveBeenCalledWith(403);
|
|
|
|
expect(logFacilities.errmessage).toHaveBeenCalledWith("Content blocked.");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("should call next if URL is rewritten successfully", () => {
|
|
|
|
config.rewriteMap = [
|
|
|
|
{
|
|
|
|
host: "test.com",
|
|
|
|
definingRegex: "/.*/",
|
|
|
|
replacements: [
|
|
|
|
{
|
|
|
|
regex: "/.*/",
|
2024-09-01 21:54:42 +02:00
|
|
|
replacement: "/new"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2024-08-30 22:11:16 +02:00
|
|
|
];
|
|
|
|
middleware(req, res, logFacilities, config, next);
|
|
|
|
expect(next).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|