1
1
/// <reference types="mocha" />
2
2
import { ChecksumAlgorithm } from "@aws-sdk/middleware-flexible-checksums" ;
3
3
import { HttpRequest } from "@aws-sdk/protocol-http" ;
4
- import { FinalizeRequestMiddleware } from "@aws-sdk/types" ;
4
+ import { BuildMiddleware } from "@aws-sdk/types" ;
5
5
import chai from "chai" ;
6
6
import chaiAsPromised from "chai-as-promised" ;
7
+ import { Readable } from "stream" ;
7
8
8
9
import { S3 } from "../src/S3" ;
9
10
10
11
chai . use ( chaiAsPromised ) ;
11
12
const { expect } = chai ;
12
13
13
14
describe ( "Flexible Checksums" , ( ) => {
15
+ const testCases = [
16
+ [ "" , ChecksumAlgorithm . CRC32 , "AAAAAA==" ] ,
17
+ [ "abc" , ChecksumAlgorithm . CRC32 , "NSRBwg==" ] ,
18
+ [ "Hello world" , ChecksumAlgorithm . CRC32 , "i9aeUg==" ] ,
19
+
20
+ [ "" , ChecksumAlgorithm . CRC32C , "AAAAAA==" ] ,
21
+ [ "abc" , ChecksumAlgorithm . CRC32C , "Nks/tw==" ] ,
22
+ [ "Hello world" , ChecksumAlgorithm . CRC32C , "crUfeA==" ] ,
23
+
24
+ [ "" , ChecksumAlgorithm . SHA1 , "2jmj7l5rSw0yVb/vlWAYkK/YBwk=" ] ,
25
+ [ "abc" , ChecksumAlgorithm . SHA1 , "qZk+NkcGgWq6PiVxeFDCbJzQ2J0=" ] ,
26
+ [ "Hello world" , ChecksumAlgorithm . SHA1 , "e1AsOh9IyGCa4hLN+2Od7jlnP14=" ] ,
27
+
28
+ [ "" , ChecksumAlgorithm . SHA256 , "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=" ] ,
29
+ [ "abc" , ChecksumAlgorithm . SHA256 , "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=" ] ,
30
+ [ "Hello world" , ChecksumAlgorithm . SHA256 , "ZOyIygCyaOW6GjVnihtTFtIS9PNmskdyMlNKiuyjfzw=" ] ,
31
+ ] ;
32
+
14
33
describe ( "putObject" , ( ) => {
15
- [
16
- [ "" , ChecksumAlgorithm . CRC32 , "AAAAAA==" ] ,
17
- [ "abc" , ChecksumAlgorithm . CRC32 , "NSRBwg==" ] ,
18
- [ "Hello world" , ChecksumAlgorithm . CRC32 , "i9aeUg==" ] ,
19
-
20
- [ "" , ChecksumAlgorithm . CRC32C , "AAAAAA==" ] ,
21
- [ "abc" , ChecksumAlgorithm . CRC32C , "Nks/tw==" ] ,
22
- [ "Hello world" , ChecksumAlgorithm . CRC32C , "crUfeA==" ] ,
23
-
24
- [ "" , ChecksumAlgorithm . SHA1 , "2jmj7l5rSw0yVb/vlWAYkK/YBwk=" ] ,
25
- [ "abc" , ChecksumAlgorithm . SHA1 , "qZk+NkcGgWq6PiVxeFDCbJzQ2J0=" ] ,
26
- [ "Hello world" , ChecksumAlgorithm . SHA1 , "e1AsOh9IyGCa4hLN+2Od7jlnP14=" ] ,
27
-
28
- [ "" , ChecksumAlgorithm . SHA256 , "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=" ] ,
29
- [ "abc" , ChecksumAlgorithm . SHA256 , "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=" ] ,
30
- [ "Hello world" , ChecksumAlgorithm . SHA256 , "ZOyIygCyaOW6GjVnihtTFtIS9PNmskdyMlNKiuyjfzw=" ] ,
31
- ] . forEach ( ( [ body , checksumAlgorithm , checksumValue ] ) => {
34
+ testCases . forEach ( ( [ body , checksumAlgorithm , checksumValue ] ) => {
32
35
const checksumHeader = `x-amz-checksum-${ checksumAlgorithm . toLowerCase ( ) } ` ;
33
36
34
37
it ( `sets ${ checksumHeader } ="${ checksumValue } "" for checksum="${ checksumAlgorithm } "` , async ( ) => {
35
- const checksumValidator : FinalizeRequestMiddleware < any , any > = ( next ) => ( args ) => {
38
+ const requestChecksumValidator : BuildMiddleware < any , any > = ( next ) => async ( args ) => {
36
39
// middleware intercept the request and return it early
37
40
const request = args . request as HttpRequest ;
38
41
const { headers } = request ;
39
42
expect ( headers [ "x-amz-sdk-checksum-algorithm" ] ) . to . equal ( checksumAlgorithm ) ;
40
43
expect ( headers [ checksumHeader ] ) . to . equal ( checksumValue ) ;
41
- return Promise . resolve ( { output : { } as any , response : { } as any } ) ;
44
+ return { output : { } as any , response : { } as any } ;
42
45
} ;
43
46
44
47
const client = new S3 ( { } ) ;
45
- client . middlewareStack . addRelativeTo ( checksumValidator , {
48
+ client . middlewareStack . addRelativeTo ( requestChecksumValidator , {
46
49
relation : "after" ,
47
50
toMiddleware : "flexibleChecksumsMiddleware" ,
48
51
} ) ;
@@ -56,4 +59,48 @@ describe("Flexible Checksums", () => {
56
59
} ) ;
57
60
} ) ;
58
61
} ) ;
62
+
63
+ describe ( "getObject" , async ( ) => {
64
+ testCases . forEach ( ( [ body , checksumAlgorithm , checksumValue ] ) => {
65
+ const checksumHeader = `x-amz-checksum-${ checksumAlgorithm . toLowerCase ( ) } ` ;
66
+
67
+ it ( `validates ${ checksumHeader } ="${ checksumValue } "" set for checksum="${ checksumAlgorithm } "` , async ( ) => {
68
+ const responseBody = new Readable ( ) ;
69
+ responseBody . push ( body ) ;
70
+ responseBody . push ( null ) ;
71
+ const responseChecksumValidator : BuildMiddleware < any , any > = ( next , context ) => async ( args ) => {
72
+ const request = args . request as HttpRequest ;
73
+ return {
74
+ output : {
75
+ $metadata : { attempts : 0 , httpStatusCode : 200 } ,
76
+ request,
77
+ context,
78
+ Body : responseBody ,
79
+ } as any ,
80
+ response : {
81
+ body : responseBody ,
82
+ headers : {
83
+ [ checksumHeader ] : checksumValue ,
84
+ } ,
85
+ } as any ,
86
+ } ;
87
+ } ;
88
+
89
+ const client = new S3 ( { } ) ;
90
+ client . middlewareStack . addRelativeTo ( responseChecksumValidator , {
91
+ relation : "after" ,
92
+ toMiddleware : "flexibleChecksumsMiddleware" ,
93
+ } ) ;
94
+
95
+ const { Body } = await client . getObject ( {
96
+ Bucket : "bucket" ,
97
+ Key : "key" ,
98
+ ChecksumMode : "ENABLED" ,
99
+ } ) ;
100
+ ( Body as Readable ) . on ( "data" , ( chunk ) => {
101
+ expect ( chunk . toString ( ) ) . to . equal ( body ) ;
102
+ } ) ;
103
+ } ) ;
104
+ } ) ;
105
+ } ) ;
59
106
} ) ;
0 commit comments