@@ -3,7 +3,7 @@ import { BuildHandlerArguments } from "@smithy/types";
33import { afterEach , beforeEach , describe , expect , test as it , vi } from "vitest" ;
44
55import { PreviouslyResolved } from "./configuration" ;
6- import { ChecksumAlgorithm , RequestChecksumCalculation } from "./constants" ;
6+ import { ChecksumAlgorithm , DEFAULT_CHECKSUM_ALGORITHM , RequestChecksumCalculation } from "./constants" ;
77import { flexibleChecksumsMiddleware } from "./flexibleChecksumsMiddleware" ;
88import { getChecksumAlgorithmForRequest } from "./getChecksumAlgorithmForRequest" ;
99import { getChecksumLocationName } from "./getChecksumLocationName" ;
@@ -34,7 +34,7 @@ describe(flexibleChecksumsMiddleware.name, () => {
3434 const mockConfig = {
3535 requestChecksumCalculation : ( ) => Promise . resolve ( RequestChecksumCalculation . WHEN_REQUIRED ) ,
3636 } as PreviouslyResolved ;
37- const mockMiddlewareConfig = { input : mockInput , requestChecksumRequired : false } ;
37+ const mockMiddlewareConfig = { requestChecksumRequired : false } ;
3838
3939 const mockBody = { body : "mockRequestBody" } ;
4040 const mockHeaders = { "content-length" : 100 , "content-encoding" : "gzip" } ;
@@ -78,17 +78,66 @@ describe(flexibleChecksumsMiddleware.name, () => {
7878 expect ( getChecksumAlgorithmForRequest ) . toHaveBeenCalledTimes ( 1 ) ;
7979 } ) ;
8080
81- it ( "skip if header is already present" , async ( ) => {
82- const handler = flexibleChecksumsMiddleware ( mockConfig , mockMiddlewareConfig ) ( mockNext , { } ) ;
83- vi . mocked ( hasHeaderWithPrefix ) . mockReturnValue ( true ) ;
84-
85- await handler ( mockArgs ) ;
86-
87- expect ( hasHeaderWithPrefix ) . toHaveBeenCalledTimes ( 1 ) ;
88- expect ( getChecksumLocationName ) . not . toHaveBeenCalled ( ) ;
89- expect ( selectChecksumAlgorithmFunction ) . not . toHaveBeenCalled ( ) ;
90- expect ( hasHeader ) . not . toHaveBeenCalled ( ) ;
91- expect ( mockNext ) . toHaveBeenCalledWith ( mockArgs ) ;
81+ describe ( "skip if header is already present" , async ( ) => {
82+ beforeEach ( ( ) => {
83+ vi . mocked ( hasHeaderWithPrefix ) . mockReturnValue ( true ) ;
84+ } ) ;
85+
86+ afterEach ( ( ) => {
87+ expect ( hasHeaderWithPrefix ) . toHaveBeenCalledTimes ( 1 ) ;
88+ expect ( getChecksumLocationName ) . not . toHaveBeenCalled ( ) ;
89+ expect ( selectChecksumAlgorithmFunction ) . not . toHaveBeenCalled ( ) ;
90+ expect ( hasHeader ) . not . toHaveBeenCalled ( ) ;
91+ } ) ;
92+
93+ it ( "with no changes input" , async ( ) => {
94+ const handler = flexibleChecksumsMiddleware ( mockConfig , mockMiddlewareConfig ) ( mockNext , { } ) ;
95+
96+ await handler ( mockArgs ) ;
97+ expect ( mockNext ) . toHaveBeenCalledWith ( mockArgs ) ;
98+ } ) ;
99+
100+ describe ( "handles input[requestAlgorithmMember]" , ( ) => {
101+ it ( "removes if respective checksum header is not present" , async ( ) => {
102+ const mockRequestAlgorithmMember = "mockRequestAlgorithmMember" ;
103+ const handler = flexibleChecksumsMiddleware ( mockConfig , {
104+ ...mockMiddlewareConfig ,
105+ requestAlgorithmMember : mockRequestAlgorithmMember ,
106+ } ) ( mockNext , { } ) ;
107+
108+ const mockArgsWithInput = {
109+ ...mockArgs ,
110+ input : { [ mockRequestAlgorithmMember ] : DEFAULT_CHECKSUM_ALGORITHM } ,
111+ } ;
112+ await handler ( mockArgsWithInput ) ;
113+ expect ( mockNext ) . toHaveBeenCalledWith ( mockArgs ) ;
114+ expect ( mockNext . mock . calls [ 0 ] [ 0 ] . input [ mockRequestAlgorithmMember ] ) . toBeUndefined ( ) ;
115+ } ) ;
116+
117+ // This means user set the checksum algorithm, as well as the actual checksum.
118+ it ( "retains if respective checksum header is present" , async ( ) => {
119+ const mockRequestAlgorithmMember = "mockRequestAlgorithmMember" ;
120+ const handler = flexibleChecksumsMiddleware ( mockConfig , {
121+ ...mockMiddlewareConfig ,
122+ requestAlgorithmMember : mockRequestAlgorithmMember ,
123+ } ) ( mockNext , { } ) ;
124+
125+ const mockArgsWithInputAndRequest = {
126+ ...mockArgs ,
127+ input : { [ mockRequestAlgorithmMember ] : DEFAULT_CHECKSUM_ALGORITHM } ,
128+ request : {
129+ ...mockRequest ,
130+ headers : {
131+ ...mockHeaders ,
132+ [ `x-amz-checksum-${ DEFAULT_CHECKSUM_ALGORITHM . toLowerCase ( ) } ` ] : mockChecksum ,
133+ } ,
134+ } ,
135+ } ;
136+ await handler ( mockArgsWithInputAndRequest ) ;
137+ expect ( mockNext ) . toHaveBeenCalledWith ( mockArgsWithInputAndRequest ) ;
138+ expect ( mockNext . mock . calls [ 0 ] [ 0 ] . input [ mockRequestAlgorithmMember ] ) . toBe ( DEFAULT_CHECKSUM_ALGORITHM ) ;
139+ } ) ;
140+ } ) ;
92141 } ) ;
93142 } ) ;
94143 } ) ;
0 commit comments