forked from aws/aws-sdk-js-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validateChecksumFromResponse.spec.ts
130 lines (115 loc) · 5.5 KB
/
validateChecksumFromResponse.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { HttpResponse } from "@aws-sdk/protocol-http";
import { PreviouslyResolved } from "./configuration";
import { ChecksumAlgorithm } from "./constants";
import { getChecksum } from "./getChecksum";
import { getChecksumAlgorithmListForResponse } from "./getChecksumAlgorithmListForResponse";
import { getChecksumLocationName } from "./getChecksumLocationName";
import { selectChecksumAlgorithmFunction } from "./selectChecksumAlgorithmFunction";
import { validateChecksumFromResponse } from "./validateChecksumFromResponse";
jest.mock("./getChecksum");
jest.mock("./getChecksumLocationName");
jest.mock("./getChecksumAlgorithmListForResponse");
jest.mock("./selectChecksumAlgorithmFunction");
describe(validateChecksumFromResponse.name, () => {
const mockConfig = {
streamHasher: jest.fn(),
base64Encoder: jest.fn(),
} as unknown as PreviouslyResolved;
const mockBody = {};
const mockHeaders = {};
const mockResponse = {
body: mockBody,
headers: mockHeaders,
} as HttpResponse;
const mockChecksum = "mockChecksum";
const mockResponseAlgorithms = [ChecksumAlgorithm.CRC32, ChecksumAlgorithm.CRC32C];
const mockChecksumAlgorithmFn = jest.fn();
const getMockResponseWithHeader = (headerKey: string, headerValue: string) => ({
...mockResponse,
headers: {
...mockHeaders,
[headerKey]: headerValue,
},
});
beforeEach(() => {
(getChecksumLocationName as jest.Mock).mockImplementation((algorithm) => algorithm);
(getChecksumAlgorithmListForResponse as jest.Mock).mockImplementation((responseAlgorithms) => responseAlgorithms);
(selectChecksumAlgorithmFunction as jest.Mock).mockReturnValue(mockChecksumAlgorithmFn);
(getChecksum as jest.Mock).mockResolvedValue(mockChecksum);
});
afterEach(() => {
expect(getChecksumAlgorithmListForResponse).toHaveBeenCalledTimes(1);
jest.clearAllMocks();
});
it("skip validation if response algorithms is empty", async () => {
const emptyAlgorithmsList = [];
await validateChecksumFromResponse(mockResponse, { config: mockConfig, responseAlgorithms: emptyAlgorithmsList });
expect(getChecksumAlgorithmListForResponse).toHaveBeenCalledWith(emptyAlgorithmsList);
expect(getChecksumLocationName).not.toHaveBeenCalled();
});
it("skip validation if updated algorithm list from response is empty", async () => {
(getChecksumAlgorithmListForResponse as jest.Mock).mockImplementation(() => []);
await validateChecksumFromResponse(mockResponse, {
config: mockConfig,
responseAlgorithms: mockResponseAlgorithms,
});
expect(getChecksumAlgorithmListForResponse).toHaveBeenCalledWith(mockResponseAlgorithms);
expect(getChecksumLocationName).not.toHaveBeenCalled();
});
it("skip validation if checksum is not present in header", async () => {
await validateChecksumFromResponse(mockResponse, {
config: mockConfig,
responseAlgorithms: mockResponseAlgorithms,
});
expect(getChecksumAlgorithmListForResponse).toHaveBeenCalledWith(mockResponseAlgorithms);
expect(getChecksumLocationName).toHaveBeenCalledTimes(mockResponseAlgorithms.length);
expect(selectChecksumAlgorithmFunction).not.toHaveBeenCalled();
expect(getChecksum).not.toHaveBeenCalled();
});
describe("successful validation for accurate checksum value", () => {
afterEach(() => {
expect(getChecksumAlgorithmListForResponse).toHaveBeenCalledWith(mockResponseAlgorithms);
expect(selectChecksumAlgorithmFunction).toHaveBeenCalledTimes(1);
expect(getChecksum).toHaveBeenCalledTimes(1);
});
it("when checksum is populated for first algorithm", async () => {
const responseWithChecksum = getMockResponseWithHeader(mockResponseAlgorithms[0], mockChecksum);
await validateChecksumFromResponse(responseWithChecksum, {
config: mockConfig,
responseAlgorithms: mockResponseAlgorithms,
});
expect(getChecksumLocationName).toHaveBeenCalledTimes(1);
expect(getChecksumLocationName).toHaveBeenCalledWith(mockResponseAlgorithms[0]);
});
it("when checksum is populated for second algorithm", async () => {
const responseWithChecksum = getMockResponseWithHeader(mockResponseAlgorithms[1], mockChecksum);
await validateChecksumFromResponse(responseWithChecksum, {
config: mockConfig,
responseAlgorithms: mockResponseAlgorithms,
});
expect(getChecksumLocationName).toHaveBeenCalledTimes(2);
expect(getChecksumLocationName).toHaveBeenNthCalledWith(1, mockResponseAlgorithms[0]);
expect(getChecksumLocationName).toHaveBeenNthCalledWith(2, mockResponseAlgorithms[1]);
});
});
it("throw error if checksum value is not accurate", async () => {
const incorrectChecksum = "incorrectChecksum";
const responseWithChecksum = getMockResponseWithHeader(mockResponseAlgorithms[0], incorrectChecksum);
try {
await validateChecksumFromResponse(responseWithChecksum, {
config: mockConfig,
responseAlgorithms: mockResponseAlgorithms,
});
fail("should throw checksum mismatch error");
} catch (error) {
expect(error.message).toMatch(
`Checksum mismatch: expected "${mockChecksum}" but received "${incorrectChecksum}"` +
` in response header "${mockResponseAlgorithms[0]}".`
);
}
expect(getChecksumAlgorithmListForResponse).toHaveBeenCalledWith(mockResponseAlgorithms);
expect(selectChecksumAlgorithmFunction).toHaveBeenCalledTimes(1);
expect(getChecksumLocationName).toHaveBeenCalledTimes(1);
expect(getChecksum).toHaveBeenCalledTimes(1);
});
});