@@ -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 } from "./constants" ;
6+ import { ChecksumAlgorithm , DEFAULT_CHECKSUM_ALGORITHM , RequestChecksumCalculation } from "./constants" ;
77import { flexibleChecksumsMiddleware } from "./flexibleChecksumsMiddleware" ;
88import { getChecksumAlgorithmForRequest } from "./getChecksumAlgorithmForRequest" ;
99import { getChecksumLocationName } from "./getChecksumLocationName" ;
@@ -13,6 +13,7 @@ import { isStreaming } from "./isStreaming";
1313import { selectChecksumAlgorithmFunction } from "./selectChecksumAlgorithmFunction" ;
1414import { stringHasher } from "./stringHasher" ;
1515
16+ vi . mock ( "@aws-sdk/core" ) ;
1617vi . mock ( "@smithy/protocol-http" ) ;
1718vi . mock ( "./getChecksumAlgorithmForRequest" ) ;
1819vi . mock ( "./getChecksumLocationName" ) ;
@@ -28,10 +29,14 @@ describe(flexibleChecksumsMiddleware.name, () => {
2829 const mockChecksum = "mockChecksum" ;
2930 const mockChecksumAlgorithmFunction = vi . fn ( ) ;
3031 const mockChecksumLocationName = "mock-checksum-location-name" ;
32+ const mockRequestAlgorithmMember = "mockRequestAlgorithmMember" ;
33+ const mockRequestAlgorithmMemberHttpHeader = "mock-request-algorithm-member-http-header" ;
3134
3235 const mockInput = { } ;
33- const mockConfig = { } as PreviouslyResolved ;
34- const mockMiddlewareConfig = { requestChecksumRequired : false } ;
36+ const mockConfig = {
37+ requestChecksumCalculation : ( ) => Promise . resolve ( RequestChecksumCalculation . WHEN_REQUIRED ) ,
38+ } as PreviouslyResolved ;
39+ const mockMiddlewareConfig = { input : mockInput , requestChecksumRequired : false } ;
3540
3641 const mockBody = { body : "mockRequestBody" } ;
3742 const mockHeaders = { "content-length" : 100 , "content-encoding" : "gzip" } ;
@@ -41,9 +46,8 @@ describe(flexibleChecksumsMiddleware.name, () => {
4146
4247 beforeEach ( ( ) => {
4348 mockNext . mockResolvedValueOnce ( mockResult ) ;
44- const { isInstance } = HttpRequest ;
45- ( isInstance as unknown as any ) . mockReturnValue ( true ) ;
46- vi . mocked ( getChecksumAlgorithmForRequest ) . mockReturnValue ( ChecksumAlgorithm . MD5 ) ;
49+ vi . mocked ( HttpRequest . isInstance ) . mockReturnValue ( true ) ;
50+ vi . mocked ( getChecksumAlgorithmForRequest ) . mockReturnValue ( ChecksumAlgorithm . CRC32 ) ;
4751 vi . mocked ( getChecksumLocationName ) . mockReturnValue ( mockChecksumLocationName ) ;
4852 vi . mocked ( hasHeader ) . mockReturnValue ( true ) ;
4953 vi . mocked ( hasHeaderWithPrefix ) . mockReturnValue ( false ) ;
@@ -58,8 +62,7 @@ describe(flexibleChecksumsMiddleware.name, () => {
5862
5963 describe ( "skips" , ( ) => {
6064 it ( "if not an instance of HttpRequest" , async ( ) => {
61- const { isInstance } = HttpRequest ;
62- ( isInstance as unknown as any ) . mockReturnValue ( false ) ;
65+ vi . mocked ( HttpRequest . isInstance ) . mockReturnValue ( false ) ;
6366 const handler = flexibleChecksumsMiddleware ( mockConfig , mockMiddlewareConfig ) ( mockNext , { } ) ;
6467 await handler ( mockArgs ) ;
6568 expect ( getChecksumAlgorithmForRequest ) . not . toHaveBeenCalled ( ) ;
@@ -77,7 +80,7 @@ describe(flexibleChecksumsMiddleware.name, () => {
7780 expect ( getChecksumAlgorithmForRequest ) . toHaveBeenCalledTimes ( 1 ) ;
7881 } ) ;
7982
80- it ( "if header is already present" , async ( ) => {
83+ it ( "skip if header is already present" , async ( ) => {
8184 const handler = flexibleChecksumsMiddleware ( mockConfig , mockMiddlewareConfig ) ( mockNext , { } ) ;
8285 vi . mocked ( hasHeaderWithPrefix ) . mockReturnValue ( true ) ;
8386
@@ -94,11 +97,53 @@ describe(flexibleChecksumsMiddleware.name, () => {
9497
9598 describe ( "adds checksum in the request header" , ( ) => {
9699 afterEach ( ( ) => {
100+ expect ( HttpRequest . isInstance ) . toHaveBeenCalledTimes ( 1 ) ;
101+ expect ( hasHeaderWithPrefix ) . toHaveBeenCalledTimes ( 1 ) ;
97102 expect ( getChecksumAlgorithmForRequest ) . toHaveBeenCalledTimes ( 1 ) ;
98103 expect ( getChecksumLocationName ) . toHaveBeenCalledTimes ( 1 ) ;
99104 expect ( selectChecksumAlgorithmFunction ) . toHaveBeenCalledTimes ( 1 ) ;
100105 } ) ;
101106
107+ describe ( "if input.requestAlgorithmMember can be set" , ( ) => {
108+ describe ( "input[requestAlgorithmMember] is not defined and" , ( ) => {
109+ const mockMwConfigWithReqAlgoMember = {
110+ ...mockMiddlewareConfig ,
111+ requestAlgorithmMember : {
112+ name : mockRequestAlgorithmMember ,
113+ httpHeader : mockRequestAlgorithmMemberHttpHeader ,
114+ } ,
115+ } ;
116+
117+ it ( "requestChecksumCalculation is supported" , async ( ) => {
118+ const handler = flexibleChecksumsMiddleware (
119+ {
120+ ...mockConfig ,
121+ requestChecksumCalculation : ( ) => Promise . resolve ( RequestChecksumCalculation . WHEN_SUPPORTED ) ,
122+ } ,
123+ mockMwConfigWithReqAlgoMember
124+ ) ( mockNext , { } ) ;
125+ await handler ( mockArgs ) ;
126+ expect ( mockNext . mock . calls [ 0 ] [ 0 ] . input [ mockRequestAlgorithmMember ] ) . toEqual ( DEFAULT_CHECKSUM_ALGORITHM ) ;
127+ expect ( mockNext . mock . calls [ 0 ] [ 0 ] . request . headers [ mockRequestAlgorithmMemberHttpHeader ] ) . toEqual (
128+ DEFAULT_CHECKSUM_ALGORITHM
129+ ) ;
130+ } ) ;
131+
132+ it ( "requestChecksumRequired is set to true" , async ( ) => {
133+ const handler = flexibleChecksumsMiddleware ( mockConfig , {
134+ ...mockMwConfigWithReqAlgoMember ,
135+ requestChecksumRequired : true ,
136+ } ) ( mockNext , { } ) ;
137+
138+ await handler ( mockArgs ) ;
139+ expect ( mockNext . mock . calls [ 0 ] [ 0 ] . input [ mockRequestAlgorithmMember ] ) . toEqual ( DEFAULT_CHECKSUM_ALGORITHM ) ;
140+ expect ( mockNext . mock . calls [ 0 ] [ 0 ] . request . headers [ mockRequestAlgorithmMemberHttpHeader ] ) . toEqual (
141+ DEFAULT_CHECKSUM_ALGORITHM
142+ ) ;
143+ } ) ;
144+ } ) ;
145+ } ) ;
146+
102147 it ( "for streaming body" , async ( ) => {
103148 vi . mocked ( isStreaming ) . mockReturnValue ( true ) ;
104149 const mockUpdatedBody = { body : "mockUpdatedBody" } ;
0 commit comments