|
| 1 | +import { S3 } from "@aws-sdk/client-s3"; |
| 2 | +import { NodeHttpHandler } from "@smithy/node-http-handler"; |
| 3 | +import { HttpResponse } from "@smithy/protocol-http"; |
| 4 | +import { describe, expect, test as it } from "vitest"; |
| 5 | + |
| 6 | +describe("middleware-flexible-checksums.retry", () => { |
| 7 | + it("retry reuses the checksum", async () => { |
| 8 | + const maxAttempts = 3; |
| 9 | + class CustomHandler extends NodeHttpHandler { |
| 10 | + async handle() { |
| 11 | + return { |
| 12 | + response: new HttpResponse({ |
| 13 | + statusCode: 500, // Fake Trasient Error |
| 14 | + headers: {}, |
| 15 | + }), |
| 16 | + }; |
| 17 | + } |
| 18 | + } |
| 19 | + const requestHandler = new CustomHandler(); |
| 20 | + const client = new S3({ maxAttempts, requestHandler }); |
| 21 | + |
| 22 | + let flexChecksCalls = 0; |
| 23 | + client.middlewareStack.addRelativeTo( |
| 24 | + (next: any) => async (args: any) => { |
| 25 | + flexChecksCalls++; |
| 26 | + return next(args); |
| 27 | + }, |
| 28 | + { |
| 29 | + toMiddleware: "flexibleChecksumsMiddleware", |
| 30 | + relation: "after", |
| 31 | + } |
| 32 | + ); |
| 33 | + |
| 34 | + let retryMiddlewareCalls = 0; |
| 35 | + client.middlewareStack.addRelativeTo( |
| 36 | + (next: any) => async (args: any) => { |
| 37 | + retryMiddlewareCalls++; |
| 38 | + return next(args); |
| 39 | + }, |
| 40 | + { |
| 41 | + toMiddleware: "retryMiddleware", |
| 42 | + relation: "after", |
| 43 | + } |
| 44 | + ); |
| 45 | + |
| 46 | + await client |
| 47 | + .putObject({ |
| 48 | + Bucket: "b", |
| 49 | + Key: "k", |
| 50 | + Body: "hello", |
| 51 | + }) |
| 52 | + .catch(() => { |
| 53 | + // Expected, since we're faking transient error which is retried. |
| 54 | + }); |
| 55 | + |
| 56 | + // Validate that flexibleChecksumsMiddleware is called once. |
| 57 | + expect(flexChecksCalls).toEqual(1); |
| 58 | + // Validate that retryMiddleware is called maxAttempts times. |
| 59 | + expect(retryMiddlewareCalls).toEqual(maxAttempts); |
| 60 | + }); |
| 61 | +}); |
0 commit comments