From d4cccf80fdec81b9d9012f7a4ecece36ed49a96e Mon Sep 17 00:00:00 2001 From: Peter Somogyvari Date: Tue, 6 Feb 2024 14:49:17 -0800 Subject: [PATCH] squash! - try 13 - revert of test case for cross-check Signed-off-by: Peter Somogyvari --- .../handle-rest-endpoint-exception.test.ts | 118 ++++++++---------- 1 file changed, 54 insertions(+), 64 deletions(-) diff --git a/packages/cactus-core/src/test/typescript/unit/handle-rest-endpoint-exception.test.ts b/packages/cactus-core/src/test/typescript/unit/handle-rest-endpoint-exception.test.ts index d9ed2e3f6cf..35fd6218d52 100644 --- a/packages/cactus-core/src/test/typescript/unit/handle-rest-endpoint-exception.test.ts +++ b/packages/cactus-core/src/test/typescript/unit/handle-rest-endpoint-exception.test.ts @@ -10,17 +10,10 @@ import { } from "../../../main/typescript/public-api"; // replace with the correct path to your module import { LoggerProvider } from "@hyperledger/cactus-common"; -import { - identifierByCodes, - INTERNAL_SERVER_ERROR, -} from "http-errors-enhanced-cjs"; -// Since we are testing error handling, lots of false positive error logs appear -// that are hard to untangle/confusing when reading the test log output. -// The SILENT level suppresses these. const log = LoggerProvider.getOrCreate({ label: "handle-rest-endpoint-exception.test.ts", - level: "SILENT", + level: "DEBUG", }); describe("handleRestEndpointException", () => { @@ -28,80 +21,63 @@ describe("handleRestEndpointException", () => { jest.clearAllMocks(); }); - it("should handle HttpError with statusCode gte 500", async () => { + it("should handle HttpError with statusCode >= 500", async () => { const mockResponse = createResponse(); - const rootCauseErrorMsg = "Root Cause Exception that should cause gte 500"; - const rootError = new Error(rootCauseErrorMsg); - - const reThrowErrorMsg = - "Message of the Re-thrown Exception that should have some context for debugging on top of the information already available in the rootCauseErrorMsg."; - - const ctx: IHandleRestEndpointExceptionOptions = { - errorMsg: reThrowErrorMsg, + const mockOptions: IHandleRestEndpointExceptionOptions = { + errorMsg: "Test error message", log: jest.mocked(log), // Provide a mock logger if needed - error: rootError, // Provide appropriate error objects for testing + error: new Error("Test error"), // Provide appropriate error objects for testing res: mockResponse, }; - const spyLog = jest.spyOn(ctx.log, "error"); + const mockHttpError = createHttpError(500, "Test HTTP error", { + expose: true, + }); + + const errorAsSanitizedJson = safeStringifyException(mockHttpError); + const spyLogDebug = jest.spyOn(mockOptions.log, "debug"); const spyStatus = jest.spyOn(mockResponse, "status"); const spyJson = jest.spyOn(mockResponse, "json"); - await handleRestEndpointException(ctx); + await handleRestEndpointException({ ...mockOptions, error: mockHttpError }); expect(spyStatus).toHaveBeenCalledWith(500); - expect(spyLog).toHaveBeenCalledWith( - ctx.errorMsg, - safeStringifyException(rootError), - ); - - expect(spyJson).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - identifierByCodes[INTERNAL_SERVER_ERROR], - ), - error: expect.stringContaining(reThrowErrorMsg), - }), + expect(spyLogDebug).toHaveBeenCalledWith( + mockOptions.errorMsg, + errorAsSanitizedJson, ); - expect(spyJson).toHaveBeenCalledWith( - expect.objectContaining({ - message: expect.stringMatching( - identifierByCodes[INTERNAL_SERVER_ERROR], - ), - error: expect.stringContaining(rootCauseErrorMsg), - }), - ); + expect(spyJson).toHaveBeenCalledWith({ + message: "InternalServerError", + error: errorAsSanitizedJson, + }); }); it("should handle HttpError with statusCode < 500", async () => { const mockResponse = createResponse(); - const rootErrorStr = "Test HTTP 404 error"; - const mockHttpError = createHttpError(404, rootErrorStr, { - expose: true, - }); - - const ctx: IHandleRestEndpointExceptionOptions = { + const mockOptions: IHandleRestEndpointExceptionOptions = { errorMsg: "Test error message", log: jest.mocked(log), // Provide a mock logger if needed - error: mockHttpError, // Provide appropriate error objects for testing + error: new Error("Test error"), // Provide appropriate error objects for testing res: mockResponse, }; + const mockHttpError = createHttpError(404, "Test HTTP error", { + expose: true, + }); + const errorAsSanitizedJson = safeStringifyException(mockHttpError); - const spyLogError = jest.spyOn(ctx.log, "error"); + const spyLogError = jest.spyOn(mockOptions.log, "error"); const spyStatus = jest.spyOn(mockResponse, "status"); const spyJson = jest.spyOn(mockResponse, "json"); - - await handleRestEndpointException(ctx); + await handleRestEndpointException({ ...mockOptions, error: mockHttpError }); expect(spyStatus).toHaveBeenCalledWith(404); - expect(spyLogError).toHaveBeenCalledWith( - ctx.errorMsg, + mockOptions.errorMsg, errorAsSanitizedJson, ); @@ -113,9 +89,10 @@ describe("handleRestEndpointException", () => { it("should handle non-HttpError", async () => { const mockResponse = createResponse(); + const mockError = new Error("An unexpected exception. Ha!"); - const ctx: IHandleRestEndpointExceptionOptions = { + const mockOptions: IHandleRestEndpointExceptionOptions = { errorMsg: "Test error message", log: jest.mocked(log), // Provide a mock logger if needed error: mockError, @@ -123,14 +100,19 @@ describe("handleRestEndpointException", () => { }; const mockErrorJson = safeStringifyException(mockError); - const spyLoggerFn = jest.spyOn(ctx.log, "error"); + const spyLoggerFn = jest.spyOn(mockOptions.log, "error"); const spyStatus = jest.spyOn(mockResponse, "status"); const spyJson = jest.spyOn(mockResponse, "json"); - await handleRestEndpointException(ctx); + await handleRestEndpointException({ ...mockOptions, error: mockError }); expect(spyStatus).toHaveBeenCalledWith(500); - expect(spyLoggerFn).toHaveBeenCalledWith(ctx.errorMsg, mockErrorJson); + + expect(spyLoggerFn).toHaveBeenCalledWith( + mockOptions.errorMsg, + mockErrorJson, + ); + expect(spyJson).toHaveBeenCalledOnce(); const mostRecentCall = spyJson.mock.lastCall; @@ -149,7 +131,7 @@ describe("handleRestEndpointException", () => { const dummyXssPayload = ``; const mockError = new Error(dummyXssPayload); - const ctx: IHandleRestEndpointExceptionOptions = { + const mockOptions: IHandleRestEndpointExceptionOptions = { errorMsg: "Test error message", log: jest.mocked(log), // Provide a mock logger if needed error: mockError, @@ -157,15 +139,18 @@ describe("handleRestEndpointException", () => { }; const mockErrorJson = safeStringifyException(mockError); - const spyLoggerFn = jest.spyOn(ctx.log, "error"); + const spyLoggerFn = jest.spyOn(mockOptions.log, "error"); const spyStatus = jest.spyOn(mockResponse, "status"); const spyJson = jest.spyOn(mockResponse, "json"); - await handleRestEndpointException(ctx); + await handleRestEndpointException({ ...mockOptions, error: mockError }); expect(spyStatus).toHaveBeenCalledWith(500); - expect(spyLoggerFn).toHaveBeenCalledWith(ctx.errorMsg, mockErrorJson); + expect(spyLoggerFn).toHaveBeenCalledWith( + mockOptions.errorMsg, + mockErrorJson, + ); expect(spyJson).toHaveBeenCalledOnce(); @@ -188,7 +173,7 @@ describe("handleRestEndpointException", () => { const dummyXssPayload = ``; const mockError = dummyXssPayload; - const ctx: IHandleRestEndpointExceptionOptions = { + const mockOptions: IHandleRestEndpointExceptionOptions = { errorMsg: "Test error message", log: jest.mocked(log), // Provide a mock logger if needed error: mockError, @@ -196,14 +181,19 @@ describe("handleRestEndpointException", () => { }; const mockErrorJson = safeStringifyException(mockError); - const spyLoggerFn = jest.spyOn(ctx.log, "error"); + const spyLoggerFn = jest.spyOn(mockOptions.log, "error"); const spyStatus = jest.spyOn(mockResponse, "status"); const spyJson = jest.spyOn(mockResponse, "json"); - await handleRestEndpointException(ctx); + await handleRestEndpointException({ ...mockOptions, error: mockError }); expect(spyStatus).toHaveBeenCalledWith(500); - expect(spyLoggerFn).toHaveBeenCalledWith(ctx.errorMsg, mockErrorJson); + + expect(spyLoggerFn).toHaveBeenCalledWith( + mockOptions.errorMsg, + mockErrorJson, + ); + expect(spyJson).toHaveBeenCalledOnce(); const mostRecentCall = spyJson.mock.lastCall;