-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move logging of input/output and req/res to serde (#1526)
* chore: move logging of input/request to serde * chore: move logging of output/response to serde * test: update loggerMiddleware tests * test: add tests for serializerMiddleware * test: add tests for deserializerMiddleware * test: prefix mock to loggerMiddleware tests
- Loading branch information
Showing
7 changed files
with
277 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const base = require("../../jest.config.base.js"); | ||
|
||
module.exports = { | ||
...base, | ||
}; |
98 changes: 98 additions & 0 deletions
98
packages/middleware-serde/src/deserializerMiddleware.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Logger } from "@aws-sdk/types"; | ||
|
||
import { deserializerMiddleware } from "./deserializerMiddleware"; | ||
|
||
describe("deserializerMiddleware", () => { | ||
const mockNext = jest.fn(); | ||
const mockDeserializer = jest.fn(); | ||
|
||
const mockOptions = { | ||
endpoint: () => | ||
Promise.resolve({ | ||
protocol: "protocol", | ||
hostname: "hostname", | ||
path: "path", | ||
}), | ||
}; | ||
|
||
const mockArgs = { | ||
input: { | ||
inputKey: "inputValue", | ||
}, | ||
request: { | ||
method: "GET", | ||
headers: {}, | ||
}, | ||
}; | ||
|
||
const mockOutput = { | ||
$metadata: { | ||
statusCode: 200, | ||
requestId: "requestId", | ||
}, | ||
outputKey: "outputValue", | ||
}; | ||
|
||
const mockNextResponse = { | ||
response: { | ||
statusCode: 200, | ||
headers: {}, | ||
}, | ||
}; | ||
|
||
const mockResponse = { | ||
response: mockNextResponse.response, | ||
output: mockOutput, | ||
}; | ||
|
||
beforeEach(() => { | ||
mockNext.mockResolvedValueOnce(mockNextResponse); | ||
mockDeserializer.mockResolvedValueOnce(mockOutput); | ||
}); | ||
|
||
afterEach(() => { | ||
expect(mockNext).toHaveBeenCalledTimes(1); | ||
expect(mockNext).toHaveBeenCalledWith(mockArgs); | ||
expect(mockDeserializer).toHaveBeenCalledTimes(1); | ||
expect(mockDeserializer).toHaveBeenCalledWith(mockNextResponse.response, mockOptions); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("returns without logging if context.logger is not defined", async () => { | ||
const response = await deserializerMiddleware(mockOptions, mockDeserializer)(mockNext, {})(mockArgs); | ||
expect(response).toStrictEqual(mockResponse); | ||
}); | ||
|
||
it("returns without logging if context.logger doesn't have debug/info function", async () => { | ||
const logger = {} as Logger; | ||
const response = await deserializerMiddleware(mockOptions, mockDeserializer)(mockNext, { logger })(mockArgs); | ||
expect(response).toStrictEqual(mockResponse); | ||
}); | ||
|
||
it("logs output if context.logger has info function", async () => { | ||
const logger = ({ info: jest.fn() } as unknown) as Logger; | ||
|
||
const outputFilterSensitiveLog = jest.fn().mockImplementationOnce((output) => output); | ||
const response = await deserializerMiddleware(mockOptions, mockDeserializer)(mockNext, { | ||
logger, | ||
outputFilterSensitiveLog: outputFilterSensitiveLog, | ||
})(mockArgs); | ||
|
||
const { $metadata, ...outputWithoutMetadata } = mockOutput; | ||
expect(response).toStrictEqual(mockResponse); | ||
expect(outputFilterSensitiveLog).toHaveBeenCalledTimes(1); | ||
expect(outputFilterSensitiveLog).toHaveBeenCalledWith(outputWithoutMetadata); | ||
expect(logger.info).toHaveBeenCalledTimes(1); | ||
expect(logger.info).toHaveBeenCalledWith({ output: outputWithoutMetadata }); | ||
}); | ||
|
||
it("logs response if context.logger has debug function", async () => { | ||
const logger = ({ debug: jest.fn() } as unknown) as Logger; | ||
|
||
const response = await deserializerMiddleware(mockOptions, mockDeserializer)(mockNext, { logger })(mockArgs); | ||
|
||
expect(response).toStrictEqual(mockResponse); | ||
expect(logger.debug).toHaveBeenCalledTimes(1); | ||
expect(logger.debug).toHaveBeenCalledWith({ httpResponse: mockNextResponse.response }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
packages/middleware-serde/src/serializerMiddleware.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Logger } from "@aws-sdk/types"; | ||
|
||
import { serializerMiddleware } from "./serializerMiddleware"; | ||
|
||
describe("serializerMiddleware", () => { | ||
const mockNext = jest.fn(); | ||
const mockSerializer = jest.fn(); | ||
|
||
const mockOptions = { | ||
endpoint: () => | ||
Promise.resolve({ | ||
protocol: "protocol", | ||
hostname: "hostname", | ||
path: "path", | ||
}), | ||
}; | ||
|
||
const mockArgs = { | ||
input: { | ||
inputKey: "inputValue", | ||
}, | ||
}; | ||
|
||
const mockRequest = { | ||
method: "GET", | ||
headers: {}, | ||
}; | ||
|
||
const mockResponse = { | ||
statusCode: 200, | ||
headers: {}, | ||
}; | ||
|
||
const mockOutput = { | ||
$metadata: { | ||
statusCode: 200, | ||
requestId: "requestId", | ||
}, | ||
outputKey: "outputValue", | ||
}; | ||
|
||
const mockReturn = { | ||
response: mockResponse, | ||
output: mockOutput, | ||
}; | ||
|
||
beforeEach(() => { | ||
mockNext.mockResolvedValueOnce(mockReturn); | ||
mockSerializer.mockResolvedValueOnce(mockRequest); | ||
}); | ||
|
||
afterEach(() => { | ||
expect(mockSerializer).toHaveBeenCalledTimes(1); | ||
expect(mockSerializer).toHaveBeenCalledWith(mockArgs.input, mockOptions); | ||
expect(mockNext).toHaveBeenCalledTimes(1); | ||
expect(mockNext).toHaveBeenCalledWith({ ...mockArgs, request: mockRequest }); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("returns without logging if context.logger is not defined", async () => { | ||
const response = await serializerMiddleware(mockOptions, mockSerializer)(mockNext, {})(mockArgs); | ||
expect(response).toStrictEqual(mockReturn); | ||
}); | ||
|
||
it("returns without logging if context.logger doesn't have debug/info function", async () => { | ||
const logger = {} as Logger; | ||
const response = await serializerMiddleware(mockOptions, mockSerializer)(mockNext, { logger })(mockArgs); | ||
expect(response).toStrictEqual(mockReturn); | ||
}); | ||
|
||
it("logs input if context.logger has info function", async () => { | ||
const logger = ({ info: jest.fn() } as unknown) as Logger; | ||
|
||
const inputFilterSensitiveLog = jest.fn().mockImplementationOnce((input) => input); | ||
const response = await serializerMiddleware(mockOptions, mockSerializer)(mockNext, { | ||
logger, | ||
inputFilterSensitiveLog, | ||
})(mockArgs); | ||
|
||
expect(response).toStrictEqual(mockReturn); | ||
expect(inputFilterSensitiveLog).toHaveBeenCalledTimes(1); | ||
expect(inputFilterSensitiveLog).toHaveBeenCalledWith(mockArgs.input); | ||
expect(logger.info).toHaveBeenCalledTimes(1); | ||
expect(logger.info).toHaveBeenCalledWith({ input: mockArgs.input }); | ||
}); | ||
|
||
it("logs request if context.logger has debug function", async () => { | ||
const logger = ({ debug: jest.fn() } as unknown) as Logger; | ||
|
||
const response = await serializerMiddleware(mockOptions, mockSerializer)(mockNext, { logger })(mockArgs); | ||
|
||
expect(response).toStrictEqual(mockReturn); | ||
expect(logger.debug).toHaveBeenCalledTimes(1); | ||
expect(logger.debug).toHaveBeenCalledWith({ httpRequest: mockRequest }); | ||
}); | ||
}); |
Oops, something went wrong.