|
| 1 | +import { Logger } from "@aws-sdk/types"; |
| 2 | + |
| 3 | +import { deserializerMiddleware } from "./deserializerMiddleware"; |
| 4 | + |
| 5 | +describe("deserializerMiddleware", () => { |
| 6 | + const mockNext = jest.fn(); |
| 7 | + const mockDeserializer = jest.fn(); |
| 8 | + |
| 9 | + const mockOptions = { |
| 10 | + endpoint: () => |
| 11 | + Promise.resolve({ |
| 12 | + protocol: "protocol", |
| 13 | + hostname: "hostname", |
| 14 | + path: "path", |
| 15 | + }), |
| 16 | + }; |
| 17 | + |
| 18 | + const mockArgs = { |
| 19 | + input: { |
| 20 | + inputKey: "inputValue", |
| 21 | + }, |
| 22 | + request: { |
| 23 | + method: "GET", |
| 24 | + headers: {}, |
| 25 | + }, |
| 26 | + }; |
| 27 | + |
| 28 | + // const mockRequest = { |
| 29 | + // method: "GET", |
| 30 | + // headers: {}, |
| 31 | + // }; |
| 32 | + |
| 33 | + const mockOutput = { |
| 34 | + $metadata: { |
| 35 | + statusCode: 200, |
| 36 | + requestId: "requestId", |
| 37 | + }, |
| 38 | + outputKey: "outputValue", |
| 39 | + }; |
| 40 | + |
| 41 | + const mockNextResponse = { |
| 42 | + response: { |
| 43 | + statusCode: 200, |
| 44 | + headers: {}, |
| 45 | + }, |
| 46 | + }; |
| 47 | + |
| 48 | + const mockResponse = { |
| 49 | + response: mockNextResponse.response, |
| 50 | + output: mockOutput, |
| 51 | + }; |
| 52 | + |
| 53 | + beforeEach(() => { |
| 54 | + mockNext.mockResolvedValueOnce(mockNextResponse); |
| 55 | + mockDeserializer.mockResolvedValueOnce(mockOutput); |
| 56 | + }); |
| 57 | + |
| 58 | + afterEach(() => { |
| 59 | + expect(mockNext).toHaveBeenCalledTimes(1); |
| 60 | + expect(mockNext).toHaveBeenCalledWith(mockArgs); |
| 61 | + expect(mockDeserializer).toHaveBeenCalledTimes(1); |
| 62 | + expect(mockDeserializer).toHaveBeenCalledWith(mockNextResponse.response, mockOptions); |
| 63 | + jest.clearAllMocks(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("returns without logging if context.logger is not defined", async () => { |
| 67 | + const response = await deserializerMiddleware(mockOptions, mockDeserializer)(mockNext, {})(mockArgs); |
| 68 | + expect(response).toStrictEqual(mockResponse); |
| 69 | + }); |
| 70 | + |
| 71 | + it("returns without logging if context.logger doesn't have debug/info function", async () => { |
| 72 | + const logger = {} as Logger; |
| 73 | + const response = await deserializerMiddleware(mockOptions, mockDeserializer)(mockNext, { logger })(mockArgs); |
| 74 | + expect(response).toStrictEqual(mockResponse); |
| 75 | + }); |
| 76 | + |
| 77 | + it("logs output if context.logger has info function", async () => { |
| 78 | + const logger = ({ info: jest.fn() } as unknown) as Logger; |
| 79 | + |
| 80 | + const outputFilterSensitiveLog = jest.fn().mockImplementationOnce((output) => output); |
| 81 | + const response = await deserializerMiddleware(mockOptions, mockDeserializer)(mockNext, { |
| 82 | + logger, |
| 83 | + outputFilterSensitiveLog: outputFilterSensitiveLog, |
| 84 | + })(mockArgs); |
| 85 | + |
| 86 | + const { $metadata, ...outputWithoutMetadata } = mockOutput; |
| 87 | + expect(response).toStrictEqual(mockResponse); |
| 88 | + expect(outputFilterSensitiveLog).toHaveBeenCalledTimes(1); |
| 89 | + expect(outputFilterSensitiveLog).toHaveBeenCalledWith(outputWithoutMetadata); |
| 90 | + expect(logger.info).toHaveBeenCalledTimes(1); |
| 91 | + expect(logger.info).toHaveBeenCalledWith({ output: outputWithoutMetadata }); |
| 92 | + }); |
| 93 | + |
| 94 | + it("logs response if context.logger has debug function", async () => { |
| 95 | + const logger = ({ debug: jest.fn() } as unknown) as Logger; |
| 96 | + |
| 97 | + const response = await deserializerMiddleware(mockOptions, mockDeserializer)(mockNext, { logger })(mockArgs); |
| 98 | + |
| 99 | + expect(response).toStrictEqual(mockResponse); |
| 100 | + expect(logger.debug).toHaveBeenCalledTimes(1); |
| 101 | + expect(logger.debug).toHaveBeenCalledWith({ httpResponse: mockNextResponse.response }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments