Skip to content

Commit 0b87963

Browse files
committed
test: src/validateChecksumFromResponse.spec.ts
1 parent f219a78 commit 0b87963

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import { HttpResponse } from "@smithy/protocol-http";
2+
import { createChecksumStream } from "@smithy/util-stream";
23
import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest";
34

45
import { PreviouslyResolved } from "./configuration";
56
import { ChecksumAlgorithm } from "./constants";
67
import { getChecksum } from "./getChecksum";
78
import { getChecksumAlgorithmListForResponse } from "./getChecksumAlgorithmListForResponse";
89
import { getChecksumLocationName } from "./getChecksumLocationName";
10+
import { isStreaming } from "./isStreaming";
911
import { selectChecksumAlgorithmFunction } from "./selectChecksumAlgorithmFunction";
1012
import { validateChecksumFromResponse } from "./validateChecksumFromResponse";
1113

14+
vi.mock("@smithy/util-stream");
1215
vi.mock("./getChecksum");
1316
vi.mock("./getChecksumLocationName");
1417
vi.mock("./getChecksumAlgorithmListForResponse");
18+
vi.mock("./isStreaming");
1519
vi.mock("./selectChecksumAlgorithmFunction");
1620

1721
describe(validateChecksumFromResponse.name, () => {
1822
const mockConfig = {
19-
streamHasher: vi.fn(),
2023
base64Encoder: vi.fn(),
2124
} as unknown as PreviouslyResolved;
2225

@@ -85,29 +88,41 @@ describe(validateChecksumFromResponse.name, () => {
8588
});
8689

8790
describe("successful validation", () => {
88-
afterEach(() => {
91+
const validateCalls = (isStream: boolean) => {
8992
expect(getChecksumAlgorithmListForResponse).toHaveBeenCalledWith(mockResponseAlgorithms);
9093
expect(selectChecksumAlgorithmFunction).toHaveBeenCalledTimes(1);
91-
expect(getChecksum).toHaveBeenCalledTimes(1);
92-
});
9394

94-
it("when checksum is populated for first algorithm", async () => {
95+
if (isStream) {
96+
expect(getChecksum).not.toHaveBeenCalled();
97+
expect(createChecksumStream).toHaveBeenCalledTimes(1);
98+
} else {
99+
expect(getChecksum).toHaveBeenCalledTimes(1);
100+
expect(createChecksumStream).not.toHaveBeenCalled();
101+
}
102+
};
103+
104+
it.each([false, true])("when checksum is populated for first algorithm when streaming: %s", async (isStream) => {
105+
vi.mocked(isStreaming).mockReturnValue(isStream);
95106
const responseWithChecksum = getMockResponseWithHeader(mockResponseAlgorithms[0], mockChecksum);
96107
await validateChecksumFromResponse(responseWithChecksum, mockOptions);
97108
expect(getChecksumLocationName).toHaveBeenCalledTimes(1);
98109
expect(getChecksumLocationName).toHaveBeenCalledWith(mockResponseAlgorithms[0]);
110+
validateCalls(isStream);
99111
});
100112

101-
it("when checksum is populated for second algorithm", async () => {
113+
it.each([false, true])("when checksum is populated for second algorithm when streaming: %s", async (isStream) => {
114+
vi.mocked(isStreaming).mockReturnValue(isStream);
102115
const responseWithChecksum = getMockResponseWithHeader(mockResponseAlgorithms[1], mockChecksum);
103116
await validateChecksumFromResponse(responseWithChecksum, mockOptions);
104117
expect(getChecksumLocationName).toHaveBeenCalledTimes(2);
105118
expect(getChecksumLocationName).toHaveBeenNthCalledWith(1, mockResponseAlgorithms[0]);
106119
expect(getChecksumLocationName).toHaveBeenNthCalledWith(2, mockResponseAlgorithms[1]);
120+
validateCalls(isStream);
107121
});
108122
});
109123

110-
it("throw error if checksum value is not accurate", async () => {
124+
it("throw error if checksum value is not accurate when not streaming", async () => {
125+
vi.mocked(isStreaming).mockReturnValue(false);
111126
const incorrectChecksum = "incorrectChecksum";
112127
const responseWithChecksum = getMockResponseWithHeader(mockResponseAlgorithms[0], incorrectChecksum);
113128
try {
@@ -123,5 +138,18 @@ describe(validateChecksumFromResponse.name, () => {
123138
expect(selectChecksumAlgorithmFunction).toHaveBeenCalledTimes(1);
124139
expect(getChecksumLocationName).toHaveBeenCalledTimes(1);
125140
expect(getChecksum).toHaveBeenCalledTimes(1);
141+
expect(createChecksumStream).not.toHaveBeenCalled();
142+
});
143+
144+
it("return if checksum value is not accurate when streaming, as error will be thrown when stream is consumed", async () => {
145+
vi.mocked(isStreaming).mockReturnValue(true);
146+
const incorrectChecksum = "incorrectChecksum";
147+
const responseWithChecksum = getMockResponseWithHeader(mockResponseAlgorithms[0], incorrectChecksum);
148+
await validateChecksumFromResponse(responseWithChecksum, mockOptions);
149+
expect(getChecksumAlgorithmListForResponse).toHaveBeenCalledWith(mockResponseAlgorithms);
150+
expect(selectChecksumAlgorithmFunction).toHaveBeenCalledTimes(1);
151+
expect(getChecksumLocationName).toHaveBeenCalledTimes(1);
152+
expect(getChecksum).not.toHaveBeenCalled();
153+
expect(createChecksumStream).toHaveBeenCalledTimes(1);
126154
});
127155
});

0 commit comments

Comments
 (0)