Skip to content

Commit e3d92aa

Browse files
committed
fix: populate createChecksumStream in response.body
1 parent 0b87963 commit e3d92aa

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe(validateChecksumFromResponse.name, () => {
2424
} as unknown as PreviouslyResolved;
2525

2626
const mockBody = {};
27+
const mockBodyStream = { isStream: true };
2728
const mockHeaders = {};
2829
const mockResponse = {
2930
body: mockBody,
@@ -53,6 +54,7 @@ describe(validateChecksumFromResponse.name, () => {
5354
vi.mocked(getChecksumAlgorithmListForResponse).mockImplementation((responseAlgorithms) => responseAlgorithms);
5455
vi.mocked(selectChecksumAlgorithmFunction).mockReturnValue(mockChecksumAlgorithmFn);
5556
vi.mocked(getChecksum).mockResolvedValue(mockChecksum);
57+
vi.mocked(createChecksumStream).mockReturnValue(mockBodyStream);
5658
});
5759

5860
afterEach(() => {
@@ -88,15 +90,26 @@ describe(validateChecksumFromResponse.name, () => {
8890
});
8991

9092
describe("successful validation", () => {
91-
const validateCalls = (isStream: boolean) => {
93+
const validateCalls = (isStream: boolean, checksumAlgoFn: ChecksumAlgorithm) => {
9294
expect(getChecksumAlgorithmListForResponse).toHaveBeenCalledWith(mockResponseAlgorithms);
9395
expect(selectChecksumAlgorithmFunction).toHaveBeenCalledTimes(1);
9496

9597
if (isStream) {
9698
expect(getChecksum).not.toHaveBeenCalled();
9799
expect(createChecksumStream).toHaveBeenCalledTimes(1);
100+
expect(createChecksumStream).toHaveBeenCalledWith({
101+
expectedChecksum: mockChecksum,
102+
checksumSourceLocation: checksumAlgoFn,
103+
checksum: new mockChecksumAlgorithmFn(),
104+
source: mockBody,
105+
base64Encoder: mockConfig.base64Encoder,
106+
});
98107
} else {
99108
expect(getChecksum).toHaveBeenCalledTimes(1);
109+
expect(getChecksum).toHaveBeenCalledWith(mockBody, {
110+
checksumAlgorithmFn: mockChecksumAlgorithmFn,
111+
base64Encoder: mockConfig.base64Encoder,
112+
});
100113
expect(createChecksumStream).not.toHaveBeenCalled();
101114
}
102115
};
@@ -107,7 +120,7 @@ describe(validateChecksumFromResponse.name, () => {
107120
await validateChecksumFromResponse(responseWithChecksum, mockOptions);
108121
expect(getChecksumLocationName).toHaveBeenCalledTimes(1);
109122
expect(getChecksumLocationName).toHaveBeenCalledWith(mockResponseAlgorithms[0]);
110-
validateCalls(isStream);
123+
validateCalls(isStream, mockResponseAlgorithms[0]);
111124
});
112125

113126
it.each([false, true])("when checksum is populated for second algorithm when streaming: %s", async (isStream) => {
@@ -117,14 +130,16 @@ describe(validateChecksumFromResponse.name, () => {
117130
expect(getChecksumLocationName).toHaveBeenCalledTimes(2);
118131
expect(getChecksumLocationName).toHaveBeenNthCalledWith(1, mockResponseAlgorithms[0]);
119132
expect(getChecksumLocationName).toHaveBeenNthCalledWith(2, mockResponseAlgorithms[1]);
120-
validateCalls(isStream);
133+
validateCalls(isStream, mockResponseAlgorithms[1]);
121134
});
122135
});
123136

124137
it("throw error if checksum value is not accurate when not streaming", async () => {
125138
vi.mocked(isStreaming).mockReturnValue(false);
139+
126140
const incorrectChecksum = "incorrectChecksum";
127141
const responseWithChecksum = getMockResponseWithHeader(mockResponseAlgorithms[0], incorrectChecksum);
142+
128143
try {
129144
await validateChecksumFromResponse(responseWithChecksum, mockOptions);
130145
fail("should throw checksum mismatch error");
@@ -134,6 +149,7 @@ describe(validateChecksumFromResponse.name, () => {
134149
` in response header "${mockResponseAlgorithms[0]}".`
135150
);
136151
}
152+
137153
expect(getChecksumAlgorithmListForResponse).toHaveBeenCalledWith(mockResponseAlgorithms);
138154
expect(selectChecksumAlgorithmFunction).toHaveBeenCalledTimes(1);
139155
expect(getChecksumLocationName).toHaveBeenCalledTimes(1);
@@ -143,13 +159,18 @@ describe(validateChecksumFromResponse.name, () => {
143159

144160
it("return if checksum value is not accurate when streaming, as error will be thrown when stream is consumed", async () => {
145161
vi.mocked(isStreaming).mockReturnValue(true);
162+
163+
// This override does not matter for the purpose of unit test, but is kept for completeness.
146164
const incorrectChecksum = "incorrectChecksum";
147165
const responseWithChecksum = getMockResponseWithHeader(mockResponseAlgorithms[0], incorrectChecksum);
166+
148167
await validateChecksumFromResponse(responseWithChecksum, mockOptions);
168+
149169
expect(getChecksumAlgorithmListForResponse).toHaveBeenCalledWith(mockResponseAlgorithms);
150170
expect(selectChecksumAlgorithmFunction).toHaveBeenCalledTimes(1);
151171
expect(getChecksumLocationName).toHaveBeenCalledTimes(1);
152172
expect(getChecksum).not.toHaveBeenCalled();
153173
expect(createChecksumStream).toHaveBeenCalledTimes(1);
174+
expect(responseWithChecksum.body).toBe(mockBodyStream);
154175
});
155176
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const validateChecksumFromResponse = async (
3535
const { base64Encoder } = config;
3636

3737
if (isStreaming(responseBody)) {
38-
createChecksumStream({
38+
response.body = createChecksumStream({
3939
expectedChecksum: checksumFromResponse,
4040
checksumSourceLocation: responseHeader,
4141
checksum: new checksumAlgorithmFn() as Checksum,

0 commit comments

Comments
 (0)