Skip to content

Commit 15ed586

Browse files
authored
test(middleware-flexible-checksums): add tests for edge cases (#3362)
1 parent a331a3b commit 15ed586

File tree

5 files changed

+76
-20
lines changed

5 files changed

+76
-20
lines changed

packages/middleware-flexible-checksums/src/flexibleChecksumsMiddleware.spec.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { flexibleChecksumsMiddleware } from "./flexibleChecksumsMiddleware";
77
import { getChecksum } from "./getChecksum";
88
import { getChecksumAlgorithmForRequest } from "./getChecksumAlgorithmForRequest";
99
import { getChecksumLocationName } from "./getChecksumLocationName";
10-
import { FlexibleChecksumsMiddlewareConfig } from "./getFlexibleChecksumsPlugin";
1110
import { hasHeader } from "./hasHeader";
1211
import { isStreaming } from "./isStreaming";
1312
import { selectChecksumAlgorithmFunction } from "./selectChecksumAlgorithmFunction";
@@ -31,13 +30,13 @@ describe(flexibleChecksumsMiddleware.name, () => {
3130

3231
const mockInput = {};
3332
const mockConfig = {} as PreviouslyResolved;
34-
const mockMiddlewareConfig = { input: mockInput } as FlexibleChecksumsMiddlewareConfig;
33+
const mockMiddlewareConfig = { input: mockInput, requestChecksumRequired: false };
3534

36-
const mockBody = { body: "mockBody" };
35+
const mockBody = { body: "mockRequestBody" };
3736
const mockHeaders = { "content-length": 100 };
3837
const mockRequest = { body: mockBody, headers: mockHeaders };
3938
const mockArgs = { request: mockRequest } as BuildHandlerArguments<any>;
40-
const mockResult = { response: {} };
39+
const mockResult = { response: { body: "mockResponsebody" } };
4140

4241
beforeEach(() => {
4342
mockNext.mockResolvedValueOnce(mockResult);
@@ -89,9 +88,8 @@ describe(flexibleChecksumsMiddleware.name, () => {
8988
};
9089
(hasHeader as jest.Mock).mockReturnValue(true);
9190
await handler(mockArgsWithChecksumHeader);
92-
expect(getChecksumLocationName).toHaveBeenCalledTimes(1);
93-
expect(selectChecksumAlgorithmFunction).toHaveBeenCalledTimes(1);
94-
expect(hasHeader).toHaveBeenCalledTimes(1);
91+
expect(getChecksumLocationName).toHaveBeenCalledWith(ChecksumAlgorithm.MD5);
92+
expect(selectChecksumAlgorithmFunction).toHaveBeenCalledWith(ChecksumAlgorithm.MD5, mockConfig);
9593
expect(mockNext).toHaveBeenCalledWith(mockArgsWithChecksumHeader);
9694
expect(hasHeader).toHaveBeenCalledWith(mockChecksumLocationName, mockHeadersWithChecksumHeader);
9795
});
@@ -126,10 +124,20 @@ describe(flexibleChecksumsMiddleware.name, () => {
126124
it("for streaming body", async () => {
127125
(isStreaming as jest.Mock).mockReturnValue(true);
128126
const mockUpdatedBody = { body: "mockUpdatedBody" };
127+
128+
const mockBase64Encoder = jest.fn();
129+
const mockStreamHasher = jest.fn();
130+
const mockBodyLengthChecker = jest.fn();
129131
const mockGetAwsChunkedEncodingStream = jest.fn().mockReturnValue(mockUpdatedBody);
130132

131133
const handler = flexibleChecksumsMiddleware(
132-
{ ...mockConfig, getAwsChunkedEncodingStream: mockGetAwsChunkedEncodingStream },
134+
{
135+
...mockConfig,
136+
base64Encoder: mockBase64Encoder,
137+
bodyLengthChecker: mockBodyLengthChecker,
138+
getAwsChunkedEncodingStream: mockGetAwsChunkedEncodingStream,
139+
streamHasher: mockStreamHasher,
140+
},
133141
mockMiddlewareConfig
134142
)(mockNext, {});
135143
await handler(mockArgs);
@@ -150,7 +158,13 @@ describe(flexibleChecksumsMiddleware.name, () => {
150158
body: mockUpdatedBody,
151159
},
152160
});
153-
expect(mockGetAwsChunkedEncodingStream).toHaveBeenCalledTimes(1);
161+
expect(mockGetAwsChunkedEncodingStream).toHaveBeenCalledWith(mockRequest.body, {
162+
base64Encoder: mockBase64Encoder,
163+
bodyLengthChecker: mockBodyLengthChecker,
164+
checksumLocationName: mockChecksumLocationName,
165+
checksumAlgorithmFn: mockChecksumAlgorithmFunction,
166+
streamHasher: mockStreamHasher,
167+
});
154168
});
155169

156170
it("for non-streaming body", async () => {
@@ -172,14 +186,19 @@ describe(flexibleChecksumsMiddleware.name, () => {
172186
it("validates checksum from the response header", async () => {
173187
const mockRequestValidationModeMember = "mockRequestValidationModeMember";
174188
const mockInput = { [mockRequestValidationModeMember]: "ENABLED" };
189+
const mockResponseAlgorithms = ["ALGO1", "ALGO2"];
175190

176191
const handler = flexibleChecksumsMiddleware(mockConfig, {
177192
...mockMiddlewareConfig,
178193
input: mockInput,
179194
requestValidationModeMember: mockRequestValidationModeMember,
195+
responseAlgorithms: mockResponseAlgorithms,
180196
})(mockNext, {});
181197

182198
await handler(mockArgs);
183-
expect(validateChecksumFromResponse).toHaveBeenCalledTimes(1);
199+
expect(validateChecksumFromResponse).toHaveBeenCalledWith(mockResult.response, {
200+
config: mockConfig,
201+
responseAlgorithms: mockResponseAlgorithms,
202+
});
184203
});
185204
});

packages/middleware-flexible-checksums/src/getChecksum.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe(getChecksum.name, () => {
1414

1515
const mockBody = "mockBody";
1616
const mockOutput = "mockOutput";
17+
const mockRawOutput = Buffer.from(mockOutput);
1718

1819
beforeEach(() => {
1920
mockOptions.base64Encoder.mockResolvedValueOnce(mockOutput);
@@ -25,17 +26,19 @@ describe(getChecksum.name, () => {
2526

2627
it("gets checksum from streamHasher if body is streaming", async () => {
2728
(isStreaming as jest.Mock).mockReturnValue(true);
29+
mockOptions.streamHasher.mockResolvedValue(mockRawOutput);
2830
const checksum = await getChecksum(mockBody, mockOptions);
2931
expect(checksum).toEqual(mockOutput);
3032
expect(stringHasher).not.toHaveBeenCalled();
31-
expect(mockOptions.streamHasher).toHaveBeenCalledTimes(1);
33+
expect(mockOptions.streamHasher).toHaveBeenCalledWith(mockOptions.checksumAlgorithmFn, mockBody);
3234
});
3335

3436
it("gets checksum from stringHasher if body is not streaming", async () => {
3537
(isStreaming as jest.Mock).mockReturnValue(false);
38+
(stringHasher as jest.Mock).mockResolvedValue(mockRawOutput);
3639
const checksum = await getChecksum(mockBody, mockOptions);
3740
expect(checksum).toEqual(mockOutput);
38-
expect(stringHasher).toHaveBeenCalledTimes(1);
3941
expect(mockOptions.streamHasher).not.toHaveBeenCalled();
42+
expect(stringHasher).toHaveBeenCalledWith(mockOptions.checksumAlgorithmFn, mockBody);
4043
});
4144
});

packages/middleware-flexible-checksums/src/getChecksumAlgorithmForRequest.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ describe(getChecksumAlgorithmForRequest.name, () => {
1010
expect(getChecksumAlgorithmForRequest({}, { requestChecksumRequired: true })).toEqual(ChecksumAlgorithm.MD5);
1111
});
1212

13-
it("returns undefined if requestChecksumRequired is not set", () => {
14-
expect(getChecksumAlgorithmForRequest({}, { requestChecksumRequired: false })).toBeUndefined();
13+
it.each([false, undefined])("returns undefined if requestChecksumRequired=%s", (requestChecksumRequired) => {
14+
expect(getChecksumAlgorithmForRequest({}, { requestChecksumRequired })).toBeUndefined();
1515
});
1616
});
1717

1818
describe("when requestAlgorithmMember is not set in input", () => {
1919
const mockOptions = { requestAlgorithmMember: mockRequestAlgorithmMember };
20+
2021
it("returns MD5 if requestChecksumRequired is set", () => {
2122
expect(getChecksumAlgorithmForRequest({}, { ...mockOptions, requestChecksumRequired: true })).toEqual(
2223
ChecksumAlgorithm.MD5
2324
);
2425
});
2526

26-
it("returns undefined if requestChecksumRequired is not set", () => {
27-
expect(getChecksumAlgorithmForRequest({}, { ...mockOptions, requestChecksumRequired: false })).toBeUndefined();
27+
it.each([false, undefined])("returns undefined if requestChecksumRequired=%s", (requestChecksumRequired) => {
28+
expect(getChecksumAlgorithmForRequest({}, { ...mockOptions, requestChecksumRequired })).toBeUndefined();
2829
});
2930
});
3031

packages/middleware-flexible-checksums/src/getChecksumAlgorithmListForResponse.spec.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,46 @@ import { getChecksumAlgorithmListForResponse } from "./getChecksumAlgorithmListF
22
import { PRIORITY_ORDER_ALGORITHMS } from "./types";
33

44
describe(getChecksumAlgorithmListForResponse.name, () => {
5+
const unknownAlgorithm = "UNKNOWNALGO";
6+
57
it("returns empty if responseAlgorithms is empty", () => {
68
expect(getChecksumAlgorithmListForResponse([])).toEqual([]);
79
});
810

911
it("returns empty if contents of responseAlgorithms is not in priority order", () => {
10-
expect(getChecksumAlgorithmListForResponse(["UNKNOWNALGO"])).toEqual([]);
12+
expect(getChecksumAlgorithmListForResponse([unknownAlgorithm])).toEqual([]);
13+
});
14+
15+
describe("returns list as per priority order", () => {
16+
it("when all algorithms are passed in reverse order", () => {
17+
expect(getChecksumAlgorithmListForResponse([...PRIORITY_ORDER_ALGORITHMS].reverse())).toEqual(
18+
PRIORITY_ORDER_ALGORITHMS
19+
);
20+
});
21+
22+
it.each([...Array(PRIORITY_ORDER_ALGORITHMS.length).keys()].filter((num) => num !== 0))(
23+
"when subset of algorithms are passed in reverse order starting with element at '%s'",
24+
(start) => {
25+
const responseAlgorithms = PRIORITY_ORDER_ALGORITHMS.slice(start);
26+
expect(getChecksumAlgorithmListForResponse([...responseAlgorithms].reverse())).toEqual(responseAlgorithms);
27+
}
28+
);
29+
30+
it.each([...Array(PRIORITY_ORDER_ALGORITHMS.length).keys()].filter((num) => num !== 0))(
31+
"when subset of algorithms are passed in reverse order ending with element at '%s' from last",
32+
(end) => {
33+
const responseAlgorithms = PRIORITY_ORDER_ALGORITHMS.slice(PRIORITY_ORDER_ALGORITHMS.length - end);
34+
expect(getChecksumAlgorithmListForResponse([...responseAlgorithms].reverse())).toEqual(responseAlgorithms);
35+
}
36+
);
1137
});
1238

13-
it("returns list as per priority order", () => {
14-
const responseAlgorithms = [...PRIORITY_ORDER_ALGORITHMS];
15-
expect(getChecksumAlgorithmListForResponse(responseAlgorithms.reverse())).toEqual(PRIORITY_ORDER_ALGORITHMS);
39+
it("ignores algorithms not present in priority list", () => {
40+
expect(getChecksumAlgorithmListForResponse([unknownAlgorithm, ...PRIORITY_ORDER_ALGORITHMS].reverse())).toEqual(
41+
PRIORITY_ORDER_ALGORITHMS
42+
);
43+
expect(getChecksumAlgorithmListForResponse([...PRIORITY_ORDER_ALGORITHMS, unknownAlgorithm].reverse())).toEqual(
44+
PRIORITY_ORDER_ALGORITHMS
45+
);
1646
});
1747
});

packages/middleware-flexible-checksums/src/validateChecksumFromResponse.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ describe(validateChecksumFromResponse.name, () => {
9292
responseAlgorithms: mockResponseAlgorithms,
9393
});
9494
expect(getChecksumLocationName).toHaveBeenCalledTimes(1);
95+
expect(getChecksumLocationName).toHaveBeenCalledWith(mockResponseAlgorithms[0]);
9596
});
9697

9798
it("when checksum is populated for second algorithm", async () => {
@@ -101,6 +102,8 @@ describe(validateChecksumFromResponse.name, () => {
101102
responseAlgorithms: mockResponseAlgorithms,
102103
});
103104
expect(getChecksumLocationName).toHaveBeenCalledTimes(2);
105+
expect(getChecksumLocationName).toHaveBeenNthCalledWith(1, mockResponseAlgorithms[0]);
106+
expect(getChecksumLocationName).toHaveBeenNthCalledWith(2, mockResponseAlgorithms[1]);
104107
});
105108
});
106109

0 commit comments

Comments
 (0)