11import { S3 } from "@aws-sdk/client-s3" ;
2+ import { retryMiddleware } from "@smithy/middleware-retry" ;
23import { HttpResponse } from "@smithy/protocol-http" ;
34import { describe , expect , test as it } from "vitest" ;
45
6+ import { requireRequestsFrom } from "../../../private/aws-util-test/src" ;
57import { flexibleChecksumsMiddleware } from "./flexibleChecksumsMiddleware" ;
68
79describe ( "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