Skip to content

Commit d58fd1d

Browse files
committed
test(middleware-flexible-checksums): retry doesn't recompute the checksum
1 parent 08a2486 commit d58fd1d

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "@aws-sdk/crc64-nvme-crt";
44
import { ChecksumAlgorithm, S3 } from "@aws-sdk/client-s3";
55
import { HttpHandler, HttpRequest, HttpResponse } from "@smithy/protocol-http";
66
import { Readable, Transform } from "stream";
7-
import { describe, expect, test as it } from "vitest";
7+
import { describe, expect, test as it, vi } from "vitest";
88

99
import { requireRequestsFrom } from "../../../private/aws-util-test/src";
1010
import { DEFAULT_CHECKSUM_ALGORITHM, RequestChecksumCalculation, ResponseChecksumValidation } from "./constants";
@@ -101,6 +101,59 @@ describe("middleware-flexible-checksums", () => {
101101
});
102102
}
103103
);
104+
105+
it("retry doesn't recompute the checksum", async () => {
106+
const maxAttempts = 3;
107+
const client = new S3({ maxAttempts });
108+
109+
const mockFlexChecksCallsFn = vi.fn();
110+
client.middlewareStack.addRelativeTo(
111+
(next: any) => async (args: any) => {
112+
mockFlexChecksCallsFn();
113+
return next(args);
114+
},
115+
{
116+
toMiddleware: "flexibleChecksumsMiddleware",
117+
relation: "after",
118+
}
119+
);
120+
121+
const mockRetryMiddlewareCallsFn = vi.fn();
122+
client.middlewareStack.addRelativeTo(
123+
(next: any) => async (args: any) => {
124+
mockRetryMiddlewareCallsFn();
125+
return next(args);
126+
},
127+
{
128+
toMiddleware: "retryMiddleware",
129+
relation: "after",
130+
}
131+
);
132+
133+
requireRequestsFrom(client)
134+
.toMatch({ method: "PUT" })
135+
.respondWith(
136+
new HttpResponse({
137+
statusCode: 500, // Fake Trasient Error
138+
headers: {},
139+
})
140+
);
141+
142+
await client
143+
.putObject({
144+
Bucket: "b",
145+
Key: "k",
146+
Body: "hello",
147+
})
148+
.catch((err) => {
149+
// Expected, since we're faking transient error which is retried.
150+
});
151+
152+
// Validate that flexibleChecksumsMiddleware is called once.
153+
expect(mockFlexChecksCallsFn).toHaveBeenCalledTimes(1);
154+
// Validate that retryMiddleware is called maxAttempts times.
155+
expect(mockRetryMiddlewareCallsFn).toHaveBeenCalledTimes(maxAttempts);
156+
});
104157
});
105158

106159
describe("getObject", () => {

0 commit comments

Comments
 (0)