1- import { ChecksumAlgorithm } from "@aws-sdk/middleware-flexible-checksums" ;
1+ import { ChecksumAlgorithm , DEFAULT_CHECKSUM_ALGORITHM } from "@aws-sdk/middleware-flexible-checksums" ;
22import { HttpRequest } from "@smithy/protocol-http" ;
33import { BuildMiddleware } from "@smithy/types" ;
44import { Readable } from "stream" ;
@@ -23,11 +23,14 @@ describe("Flexible Checksums", () => {
2323 [ "" , ChecksumAlgorithm . SHA256 , "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=" ] ,
2424 [ "abc" , ChecksumAlgorithm . SHA256 , "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=" ] ,
2525 [ "Hello world" , ChecksumAlgorithm . SHA256 , "ZOyIygCyaOW6GjVnihtTFtIS9PNmskdyMlNKiuyjfzw=" ] ,
26+
27+ // Choose default checksum algorithm when explicily not provided.
28+ [ "Hello world" , undefined , "i9aeUg==" ] ,
2629 ] ;
2730
2831 describe ( "putObject" , ( ) => {
2932 testCases . forEach ( ( [ body , checksumAlgorithm , checksumValue ] ) => {
30- const checksumHeader = `x-amz-checksum-${ checksumAlgorithm . toLowerCase ( ) } ` ;
33+ const checksumHeader = `x-amz-checksum-${ ( checksumAlgorithm ?? DEFAULT_CHECKSUM_ALGORITHM ) . toLowerCase ( ) } ` ;
3134
3235 describe ( `sets ${ checksumHeader } ="${ checksumValue } "" for checksum="${ checksumAlgorithm } "` , ( ) => {
3336 const getBodyAsReadableStream = ( content : string ) => {
@@ -49,7 +52,7 @@ describe("Flexible Checksums", () => {
4952 // middleware intercept the request and return it early
5053 const request = args . request as HttpRequest ;
5154 const { headers } = request ;
52- expect ( headers [ "x-amz-sdk-checksum-algorithm" ] ) . to . equal ( checksumAlgorithm ) ;
55+ expect ( headers [ "x-amz-sdk-checksum-algorithm" ] ) . to . equal ( checksumAlgorithm ?? DEFAULT_CHECKSUM_ALGORITHM ) ;
5356 expect ( headers [ checksumHeader ] ) . to . equal ( checksumValue ) ;
5457 return { output : { } as any , response : { } as any } ;
5558 } ;
@@ -120,13 +123,14 @@ describe("Flexible Checksums", () => {
120123
121124 describe ( "getObject" , async ( ) => {
122125 testCases . forEach ( ( [ body , checksumAlgorithm , checksumValue ] ) => {
123- const checksumHeader = `x-amz-checksum-${ checksumAlgorithm . toLowerCase ( ) } ` ;
126+ const checksumHeader = `x-amz-checksum-${ ( checksumAlgorithm ?? DEFAULT_CHECKSUM_ALGORITHM ) . toLowerCase ( ) } ` ;
124127
125128 it ( `validates ${ checksumHeader } ="${ checksumValue } "" set for checksum="${ checksumAlgorithm } "` , async ( ) => {
126129 const responseBody = new Readable ( ) ;
127130 responseBody . push ( body ) ;
128131 responseBody . push ( null ) ;
129132 const responseChecksumValidator : BuildMiddleware < any , any > = ( next , context ) => async ( args ) => {
133+ expect ( args . input . ChecksumMode ) . to . equal ( "ENABLED" ) ;
130134 const request = args . request as HttpRequest ;
131135 return {
132136 output : {
@@ -159,7 +163,8 @@ describe("Flexible Checksums", () => {
159163 const { Body } = await client . getObject ( {
160164 Bucket : "bucket" ,
161165 Key : "key" ,
162- ChecksumMode : "ENABLED" ,
166+ // Do not pass ChecksumMode if algorithm is not explicitly defined. It'll be set by SDK.
167+ ChecksumMode : checksumAlgorithm ? "ENABLED" : undefined ,
163168 } ) ;
164169 ( Body as Readable ) . on ( "data" , ( chunk ) => {
165170 expect ( chunk . toString ( ) ) . to . equal ( body ) ;
0 commit comments