@@ -4,7 +4,7 @@ import "@aws-sdk/crc64-nvme-crt";
44import { ChecksumAlgorithm , S3 } from "@aws-sdk/client-s3" ;
55import { HttpHandler , HttpRequest , HttpResponse } from "@smithy/protocol-http" ;
66import { Readable , Transform } from "stream" ;
7- import { describe , expect , test as it } from "vitest" ;
7+ import { describe , expect , test as it , vi } from "vitest" ;
88
99import { requireRequestsFrom } from "../../../private/aws-util-test/src" ;
1010import { 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