Skip to content

Commit 0b4c2e3

Browse files
committed
test(middleware-flexible-checksums): use requireRequestsFrom
1 parent 6ea0de0 commit 0b4c2e3

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed
Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,47 @@
11
import { S3 } from "@aws-sdk/client-s3";
2+
import { retryMiddleware } from "@smithy/middleware-retry";
23
import { HttpResponse } from "@smithy/protocol-http";
34
import { describe, expect, test as it } from "vitest";
45

6+
import { requireRequestsFrom } from "../../../private/aws-util-test/src";
57
import { flexibleChecksumsMiddleware } from "./flexibleChecksumsMiddleware";
68

79
describe("middleware-flexible-checksums.retry", () => {
810
it("retry reuses the checksum", async () => {
911
const maxAttempts = 3;
10-
const client = new S3({
11-
maxAttempts,
12-
requestHandler: {
13-
handle: async () => ({
14-
response: new HttpResponse({
15-
statusCode: 500, // Fake Trasient Error
16-
}),
17-
}),
18-
},
19-
});
12+
const client = new S3({ maxAttempts });
2013

2114
let flexChecksCallCount = 0;
22-
const flexChecksCallCountMiddleware = (next: any) => async (args: any) => {
23-
console.log("after flexChecks");
24-
flexChecksCallCount++;
25-
return await next(args);
26-
};
27-
client.middlewareStack.addRelativeTo(flexChecksCallCountMiddleware, {
28-
name: flexChecksCallCountMiddleware.name,
29-
toMiddleware: flexibleChecksumsMiddleware.name,
30-
relation: "after",
31-
override: true,
32-
});
15+
client.middlewareStack.addRelativeTo(
16+
(next: any) => async (args: any) => {
17+
flexChecksCallCount++;
18+
return next(args);
19+
},
20+
{
21+
toMiddleware: flexibleChecksumsMiddleware.name,
22+
relation: "after",
23+
}
24+
);
3325

34-
client.middlewareStack.identifyOnResolve(true);
26+
let retryMiddlewareCalls = 0;
27+
client.middlewareStack.addRelativeTo(
28+
(next: any) => async (args: any) => {
29+
retryMiddlewareCalls++;
30+
return next(args);
31+
},
32+
{
33+
toMiddleware: retryMiddleware.name,
34+
relation: "after",
35+
}
36+
);
37+
38+
requireRequestsFrom(client)
39+
.toMatch({ method: "PUT" })
40+
.respondWith(
41+
new HttpResponse({
42+
statusCode: 500, // Fake Trasient Error
43+
})
44+
);
3545

3646
await client
3747
.putObject({
@@ -40,15 +50,12 @@ describe("middleware-flexible-checksums.retry", () => {
4050
Body: "hello",
4151
})
4252
.catch((err) => {
43-
console.log({ err, flexChecksCallCount });
4453
// Expected, since we're faking transient error which is retried.
45-
46-
// Validate that flexibleChecksumsMiddleware is called once.
47-
expect(flexChecksCallCount).toEqual(1);
48-
// Validate that retryMiddleware is called maxAttempts times.
49-
expect(err.$metadata.attempts).toEqual(maxAttempts);
5054
});
5155

52-
expect.assertions(2);
56+
// Validate that flexibleChecksumsMiddleware is called once.
57+
expect(flexChecksCallCount).toEqual(1);
58+
// Validate that retryMiddleware is called maxAttempts times.
59+
expect(retryMiddlewareCalls).toEqual(maxAttempts);
5360
});
5461
});

0 commit comments

Comments
 (0)