@@ -34,27 +34,71 @@ describe("Flexible Checksums", () => {
34
34
testCases . forEach ( ( [ body , checksumAlgorithm , checksumValue ] ) => {
35
35
const checksumHeader = `x-amz-checksum-${ checksumAlgorithm . toLowerCase ( ) } ` ;
36
36
37
- it ( `sets ${ checksumHeader } ="${ checksumValue } "" for checksum="${ checksumAlgorithm } "` , async ( ) => {
38
- const requestChecksumValidator : BuildMiddleware < any , any > = ( next ) => async ( args ) => {
39
- // middleware intercept the request and return it early
40
- const request = args . request as HttpRequest ;
41
- const { headers } = request ;
42
- expect ( headers [ "x-amz-sdk-checksum-algorithm" ] ) . to . equal ( checksumAlgorithm ) ;
43
- expect ( headers [ checksumHeader ] ) . to . equal ( checksumValue ) ;
44
- return { output : { } as any , response : { } as any } ;
37
+ describe ( `sets ${ checksumHeader } ="${ checksumValue } "" for checksum="${ checksumAlgorithm } "` , ( ) => {
38
+ const getBodyAsReadableStream = ( content : string ) => {
39
+ const readableStream = new Readable ( ) ;
40
+ const separator = " " ;
41
+ const wordsAsChunks = content . split ( separator ) ;
42
+ wordsAsChunks . forEach ( ( word , index ) => {
43
+ readableStream . push ( word ) ;
44
+ if ( index !== wordsAsChunks . length - 1 ) {
45
+ readableStream . push ( separator ) ;
46
+ }
47
+ } ) ;
48
+ readableStream . push ( null ) ;
49
+ return readableStream ;
45
50
} ;
46
51
47
- const client = new S3 ( { } ) ;
48
- client . middlewareStack . addRelativeTo ( requestChecksumValidator , {
49
- relation : "after" ,
50
- toMiddleware : "flexibleChecksumsMiddleware" ,
52
+ it ( `when body is sent as a request` , async ( ) => {
53
+ const requestChecksumValidator : BuildMiddleware < any , any > = ( next ) => async ( args ) => {
54
+ // middleware intercept the request and return it early
55
+ const request = args . request as HttpRequest ;
56
+ const { headers } = request ;
57
+ expect ( headers [ "x-amz-sdk-checksum-algorithm" ] ) . to . equal ( checksumAlgorithm ) ;
58
+ expect ( headers [ checksumHeader ] ) . to . equal ( checksumValue ) ;
59
+ return { output : { } as any , response : { } as any } ;
60
+ } ;
61
+
62
+ const client = new S3 ( { } ) ;
63
+ client . middlewareStack . addRelativeTo ( requestChecksumValidator , {
64
+ relation : "after" ,
65
+ toMiddleware : "flexibleChecksumsMiddleware" ,
66
+ } ) ;
67
+
68
+ return await client . putObject ( {
69
+ Bucket : "bucket" ,
70
+ Key : "key" ,
71
+ Body : body ,
72
+ ChecksumAlgorithm : checksumAlgorithm ,
73
+ } ) ;
51
74
} ) ;
52
75
53
- return await client . putObject ( {
54
- Bucket : "bucket" ,
55
- Key : "key" ,
56
- Body : body ,
57
- ChecksumAlgorithm : checksumAlgorithm ,
76
+ it ( `when body is sent as a stream` , async ( ) => {
77
+ const requestChecksumValidator : BuildMiddleware < any , any > = ( next ) => async ( args ) => {
78
+ // middleware intercept the request and return it early
79
+ const request = args . request as HttpRequest ;
80
+ const { headers } = request ;
81
+ expect ( headers [ "content-length" ] ) . to . be . undefined ;
82
+ expect ( headers [ "content-encoding" ] ) . to . equal ( "aws-chunked" ) ;
83
+ expect ( headers [ "transfer-encoding" ] ) . to . equal ( "chunked" ) ;
84
+ expect ( headers [ "x-amz-content-sha256" ] ) . to . equal ( "STREAMING-UNSIGNED-PAYLOAD-TRAILER" ) ;
85
+ expect ( headers [ "x-amz-trailer" ] ) . to . equal ( checksumHeader ) ;
86
+ return { output : { } as any , response : { } as any } ;
87
+ } ;
88
+
89
+ const client = new S3 ( { } ) ;
90
+ client . middlewareStack . addRelativeTo ( requestChecksumValidator , {
91
+ relation : "after" ,
92
+ toMiddleware : "flexibleChecksumsMiddleware" ,
93
+ } ) ;
94
+
95
+ const bodyStream = getBodyAsReadableStream ( body ) ;
96
+ return await client . putObject ( {
97
+ Bucket : "bucket" ,
98
+ Key : "key" ,
99
+ Body : bodyStream ,
100
+ ChecksumAlgorithm : checksumAlgorithm ,
101
+ } ) ;
58
102
} ) ;
59
103
} ) ;
60
104
} ) ;
0 commit comments