-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.test.js
More file actions
107 lines (89 loc) · 3.25 KB
/
Copy pathutils.test.js
File metadata and controls
107 lines (89 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* eslint-disable no-undef */
const { redactApiKey, getObfuscatedIP } = require("./utils");
describe("redactApiKey", () => {
it("should redact the API key", () => {
expect(redactApiKey("1234567890")).toBe("1234******");
expect(redactApiKey("123")).toBe("123");
});
it("should return 'N/A' if the API key is missing", () => {
expect(redactApiKey(null)).toBe("N/A");
expect(redactApiKey(undefined)).toBe("N/A");
expect(redactApiKey("")).toBe("N/A");
});
});
describe("getObfuscatedIP", () => {
it("should obfuscate a valid IPv4 address from x-forwarded-for header", () => {
const req = {
headers: {
"x-forwarded-for": "192.168.1.123",
},
connection: { remoteAddress: "10.0.0.1" },
};
const result = getObfuscatedIP(req);
expect(result).toBe("192.168.1.*");
});
it("should obfuscate a valid IPv4 address from remoteAddress if x-forwarded-for is not present", () => {
const req = {
headers: {},
connection: { remoteAddress: "10.0.0.1" },
};
const result = getObfuscatedIP(req);
expect(result).toBe("10.0.0.*");
});
it("should obfuscate a valid IPv6 address from x-forwarded-for header", () => {
const req = {
headers: {
"x-forwarded-for": "2001:0db8:85a3:0000:0000:8a2e:0370:7334",
},
connection: { remoteAddress: "fe80::1" },
};
const result = getObfuscatedIP(req);
expect(result).toBe("2001:0db8:85a3:0000:0000:8a2e:0370:*");
});
it("should obfuscate a valid IPv6 address from remoteAddress if x-forwarded-for is not present", () => {
const req = {
headers: {},
connection: { remoteAddress: "fe80::1" },
};
const result = getObfuscatedIP(req);
expect(result).toBe("fe80::*");
});
it('should return "Unknown" if no IP is found in headers or connection', () => {
const req = {
headers: {},
connection: {},
};
const result = getObfuscatedIP(req);
expect(result).toBe("Unknown");
});
it("should handle multiple x-forwarded-for IPs by taking the first one for IPv4", () => {
const req = {
headers: {
"x-forwarded-for": "192.168.1.123, 10.0.0.1",
},
connection: { remoteAddress: "172.16.0.1" },
};
const result = getObfuscatedIP(req);
expect(result).toBe("192.168.1.*");
});
it("should handle multiple x-forwarded-for IPs by taking the first one for IPv6", () => {
const req = {
headers: {
"x-forwarded-for": "2001:0db8:85a3::7334, fe80::1",
},
connection: { remoteAddress: "fe80::2" },
};
const result = getObfuscatedIP(req);
expect(result).toBe("2001:0db8:85a3::*");
});
it('should return "Unknown" if an invalid x-forwarded-for header is present', () => {
const req = {
headers: {
"x-forwarded-for": "",
},
connection: {},
};
const result = getObfuscatedIP(req);
expect(result).toBe("Unknown");
});
});