-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsanitization.test.ts
More file actions
173 lines (145 loc) · 4.31 KB
/
sanitization.test.ts
File metadata and controls
173 lines (145 loc) · 4.31 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import axios from "axios";
// Automock axios
jest.mock("axios");
const mockedAxios = axios as jest.Mocked<typeof axios>;
import { BaseIterableClient } from "../../src/client/base.js";
// Import the real logger to spy on it
import { logger } from "../../src/logger.js";
describe("Debug Logging Sanitization", () => {
let mockClientInstance: any;
let requestInterceptor: any;
let responseInterceptorError: any;
let debugSpy: any;
let errorSpy: any;
beforeEach(() => {
jest.clearAllMocks();
// Spy on logger methods
// We use mockImplementation to silence the console output during tests
debugSpy = jest.spyOn(logger, "debug").mockImplementation(() => logger);
errorSpy = jest.spyOn(logger, "error").mockImplementation(() => logger);
requestInterceptor = undefined;
responseInterceptorError = undefined;
mockClientInstance = {
interceptors: {
request: {
use: jest.fn((callback) => {
requestInterceptor = callback;
return 0;
}),
},
response: {
use: jest.fn((success, error) => {
responseInterceptorError = error;
return 0;
}),
},
},
get: jest.fn(),
defaults: { headers: {} },
};
if (jest.isMockFunction(mockedAxios.create)) {
mockedAxios.create.mockReturnValue(mockClientInstance);
} else {
(mockedAxios as any).create = jest.fn().mockReturnValue(mockClientInstance);
}
});
it("should call axios.create and register interceptors", () => {
new BaseIterableClient({
apiKey: "test-api-key",
debug: true,
});
expect(mockedAxios.create).toHaveBeenCalled();
expect(mockClientInstance.interceptors.request.use).toHaveBeenCalled();
expect(requestInterceptor).toBeDefined();
});
it("should redact sensitive headers in debug logs", () => {
new BaseIterableClient({
apiKey: "test-api-key",
debug: true,
});
if (!requestInterceptor) throw new Error("Request interceptor missing");
const requestConfig = {
method: "get",
url: "/test",
headers: {
Authorization: "Bearer secret-token",
"Cookie": "session=secret",
"X-Custom": "safe",
"Api-Key": "real-api-key",
},
};
requestInterceptor(requestConfig);
expect(debugSpy).toHaveBeenCalledWith(
"API request",
expect.objectContaining({
headers: expect.objectContaining({
"Api-Key": "[REDACTED]",
Authorization: "[REDACTED]",
Cookie: "[REDACTED]",
"X-Custom": "safe",
}),
})
);
});
it("should NOT log error response data by default (debugVerbose=false)", async () => {
new BaseIterableClient({
apiKey: "test-api-key",
debug: true,
debugVerbose: false,
});
if (!responseInterceptorError) throw new Error("Response interceptor missing");
const sensitiveError = { message: "User email@example.com not found" };
const errorResponse = {
response: {
status: 404,
config: { url: "/error" },
data: sensitiveError,
},
};
try {
await responseInterceptorError(errorResponse);
} catch {
// Expected
}
expect(errorSpy).toHaveBeenCalledWith(
"API error",
expect.objectContaining({
status: 404,
})
);
const errorLog = errorSpy.mock.calls.find(
(call: any) => call[0] === "API error"
);
const errorData = errorLog?.[1] as any;
expect(errorData.data).toBeUndefined();
});
it("should log error response data when debugVerbose is true", async () => {
new BaseIterableClient({
apiKey: "test-api-key",
debug: true,
debugVerbose: true,
});
if (!responseInterceptorError) throw new Error("Response interceptor missing");
const errorBody = { error: "details" };
const errorResponse = {
response: {
status: 400,
config: { url: "/error" },
data: errorBody,
},
};
try {
await responseInterceptorError(errorResponse);
} catch {
// Expected
}
expect(errorSpy).toHaveBeenCalledWith(
"API error",
expect.objectContaining({
status: 400,
data: errorBody,
})
);
});
});