|
| 1 | +import { beforeEach, describe, expect, it, jest } from "@jest/globals"; |
| 2 | +import axios from "axios"; |
| 3 | + |
| 4 | +// Automock axios |
| 5 | +jest.mock("axios"); |
| 6 | +const mockedAxios = axios as jest.Mocked<typeof axios>; |
| 7 | + |
| 8 | +import { BaseIterableClient } from "../../src/client/base.js"; |
| 9 | +// Import the real logger to spy on it |
| 10 | +import { logger } from "../../src/logger.js"; |
| 11 | + |
| 12 | +describe("Debug Logging Sanitization", () => { |
| 13 | + let mockClientInstance: any; |
| 14 | + let requestInterceptor: any; |
| 15 | + let responseInterceptorError: any; |
| 16 | + |
| 17 | + let debugSpy: any; |
| 18 | + let errorSpy: any; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + |
| 23 | + // Spy on logger methods |
| 24 | + // We use mockImplementation to silence the console output during tests |
| 25 | + debugSpy = jest.spyOn(logger, "debug").mockImplementation(() => logger); |
| 26 | + errorSpy = jest.spyOn(logger, "error").mockImplementation(() => logger); |
| 27 | + |
| 28 | + requestInterceptor = undefined; |
| 29 | + responseInterceptorError = undefined; |
| 30 | + |
| 31 | + mockClientInstance = { |
| 32 | + interceptors: { |
| 33 | + request: { |
| 34 | + use: jest.fn((callback) => { |
| 35 | + requestInterceptor = callback; |
| 36 | + return 0; |
| 37 | + }), |
| 38 | + }, |
| 39 | + response: { |
| 40 | + use: jest.fn((success, error) => { |
| 41 | + responseInterceptorError = error; |
| 42 | + return 0; |
| 43 | + }), |
| 44 | + }, |
| 45 | + }, |
| 46 | + get: jest.fn(), |
| 47 | + defaults: { headers: {} }, |
| 48 | + }; |
| 49 | + |
| 50 | + if (jest.isMockFunction(mockedAxios.create)) { |
| 51 | + mockedAxios.create.mockReturnValue(mockClientInstance); |
| 52 | + } else { |
| 53 | + (mockedAxios as any).create = jest.fn().mockReturnValue(mockClientInstance); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + it("should call axios.create and register interceptors", () => { |
| 58 | + new BaseIterableClient({ |
| 59 | + apiKey: "test-api-key", |
| 60 | + debug: true, |
| 61 | + }); |
| 62 | + |
| 63 | + expect(mockedAxios.create).toHaveBeenCalled(); |
| 64 | + expect(mockClientInstance.interceptors.request.use).toHaveBeenCalled(); |
| 65 | + expect(requestInterceptor).toBeDefined(); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should redact sensitive headers in debug logs", () => { |
| 69 | + new BaseIterableClient({ |
| 70 | + apiKey: "test-api-key", |
| 71 | + debug: true, |
| 72 | + }); |
| 73 | + |
| 74 | + if (!requestInterceptor) throw new Error("Request interceptor missing"); |
| 75 | + |
| 76 | + const requestConfig = { |
| 77 | + method: "get", |
| 78 | + url: "/test", |
| 79 | + headers: { |
| 80 | + Authorization: "Bearer secret-token", |
| 81 | + "Cookie": "session=secret", |
| 82 | + "X-Custom": "safe", |
| 83 | + "Api-Key": "real-api-key", |
| 84 | + }, |
| 85 | + }; |
| 86 | + |
| 87 | + requestInterceptor(requestConfig); |
| 88 | + |
| 89 | + expect(debugSpy).toHaveBeenCalledWith( |
| 90 | + "API request", |
| 91 | + expect.objectContaining({ |
| 92 | + headers: expect.objectContaining({ |
| 93 | + "Api-Key": "[REDACTED]", |
| 94 | + Authorization: "[REDACTED]", |
| 95 | + Cookie: "[REDACTED]", |
| 96 | + "X-Custom": "safe", |
| 97 | + }), |
| 98 | + }) |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should NOT log error response data by default (debugVerbose=false)", async () => { |
| 103 | + new BaseIterableClient({ |
| 104 | + apiKey: "test-api-key", |
| 105 | + debug: true, |
| 106 | + debugVerbose: false, |
| 107 | + }); |
| 108 | + |
| 109 | + if (!responseInterceptorError) throw new Error("Response interceptor missing"); |
| 110 | + |
| 111 | + const sensitiveError = { message: "User [email protected] not found" }; |
| 112 | + const errorResponse = { |
| 113 | + response: { |
| 114 | + status: 404, |
| 115 | + config: { url: "/error" }, |
| 116 | + data: sensitiveError, |
| 117 | + }, |
| 118 | + }; |
| 119 | + |
| 120 | + try { |
| 121 | + await responseInterceptorError(errorResponse); |
| 122 | + } catch { |
| 123 | + // Expected |
| 124 | + } |
| 125 | + |
| 126 | + expect(errorSpy).toHaveBeenCalledWith( |
| 127 | + "API error", |
| 128 | + expect.objectContaining({ |
| 129 | + status: 404, |
| 130 | + }) |
| 131 | + ); |
| 132 | + |
| 133 | + const errorLog = errorSpy.mock.calls.find( |
| 134 | + (call: any) => call[0] === "API error" |
| 135 | + ); |
| 136 | + const errorData = errorLog?.[1] as any; |
| 137 | + |
| 138 | + expect(errorData.data).toBeUndefined(); |
| 139 | + }); |
| 140 | + |
| 141 | + it("should log error response data when debugVerbose is true", async () => { |
| 142 | + new BaseIterableClient({ |
| 143 | + apiKey: "test-api-key", |
| 144 | + debug: true, |
| 145 | + debugVerbose: true, |
| 146 | + }); |
| 147 | + |
| 148 | + if (!responseInterceptorError) throw new Error("Response interceptor missing"); |
| 149 | + |
| 150 | + const errorBody = { error: "details" }; |
| 151 | + const errorResponse = { |
| 152 | + response: { |
| 153 | + status: 400, |
| 154 | + config: { url: "/error" }, |
| 155 | + data: errorBody, |
| 156 | + }, |
| 157 | + }; |
| 158 | + |
| 159 | + try { |
| 160 | + await responseInterceptorError(errorResponse); |
| 161 | + } catch { |
| 162 | + // Expected |
| 163 | + } |
| 164 | + |
| 165 | + expect(errorSpy).toHaveBeenCalledWith( |
| 166 | + "API error", |
| 167 | + expect.objectContaining({ |
| 168 | + status: 400, |
| 169 | + data: errorBody, |
| 170 | + }) |
| 171 | + ); |
| 172 | + }); |
| 173 | +}); |
0 commit comments