1
1
import { ChecksumAlgorithm , S3 } from "@aws-sdk/client-s3" ;
2
- import { Transform } from "stream" ;
2
+ import { HttpHandler , HttpRequest , HttpResponse } from "@smithy/protocol-http" ;
3
+ import { Readable , Transform } from "stream" ;
3
4
import { describe , expect , test as it } from "vitest" ;
4
5
5
6
import { requireRequestsFrom } from "../../../private/aws-util-test/src" ;
@@ -13,87 +14,134 @@ describe("middleware-flexible-checksums", () => {
13
14
error ( ) { } ,
14
15
} ;
15
16
17
+ const testCases : [ string , ChecksumAlgorithm , string ] [ ] = [
18
+ [ "" , ChecksumAlgorithm . CRC32 , "AAAAAA==" ] ,
19
+ [ "abc" , ChecksumAlgorithm . CRC32 , "NSRBwg==" ] ,
20
+ [ "Hello world" , ChecksumAlgorithm . CRC32 , "i9aeUg==" ] ,
21
+
22
+ [ "" , ChecksumAlgorithm . CRC32C , "AAAAAA==" ] ,
23
+ [ "abc" , ChecksumAlgorithm . CRC32C , "Nks/tw==" ] ,
24
+ [ "Hello world" , ChecksumAlgorithm . CRC32C , "crUfeA==" ] ,
25
+
26
+ [ "" , ChecksumAlgorithm . SHA1 , "2jmj7l5rSw0yVb/vlWAYkK/YBwk=" ] ,
27
+ [ "abc" , ChecksumAlgorithm . SHA1 , "qZk+NkcGgWq6PiVxeFDCbJzQ2J0=" ] ,
28
+ [ "Hello world" , ChecksumAlgorithm . SHA1 , "e1AsOh9IyGCa4hLN+2Od7jlnP14=" ] ,
29
+
30
+ [ "" , ChecksumAlgorithm . SHA256 , "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=" ] ,
31
+ [ "abc" , ChecksumAlgorithm . SHA256 , "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=" ] ,
32
+ [ "Hello world" , ChecksumAlgorithm . SHA256 , "ZOyIygCyaOW6GjVnihtTFtIS9PNmskdyMlNKiuyjfzw=" ] ,
33
+ ] ;
34
+
16
35
describe ( S3 . name , ( ) => {
17
- it ( "should set flexible checksums (SHA256)" , async ( ) => {
18
- const client = new S3 ( { region : "us-west-2" , logger } ) ;
36
+ const client = new S3 ( { region : "us-west-2" , logger } ) ;
19
37
20
- requireRequestsFrom ( client ) . toMatch ( {
21
- method : "PUT" ,
22
- hostname : "s3.us-west-2.amazonaws.com" ,
23
- protocol : "https:" ,
24
- path : "/b/k" ,
25
- headers : {
26
- "content-type" : "application/octet-stream" ,
27
- "x-amz-sdk-checksum-algorithm" : "SHA256" ,
28
- "content-length" : "4" ,
29
- Expect : "100-continue" ,
30
- "x-amz-checksum-sha256" : "iNQmb9TmM40TuEX88olXnSCciXgjuSF9o+Fhk28DFYk=" ,
31
- host : "s3.us-west-2.amazonaws.com" ,
32
- "x-amz-user-agent" : / ./ ,
33
- "user-agent" : / ./ ,
34
- "amz-sdk-invocation-id" : / ./ ,
35
- "amz-sdk-request" : / ./ ,
36
- "x-amz-date" : / ./ ,
37
- "x-amz-content-sha256" : "88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589" ,
38
- authorization : / ./ ,
39
- } ,
40
- query : {
41
- "x-id" : "PutObject" ,
42
- } ,
43
- } ) ;
38
+ describe ( "putObject" , ( ) => {
39
+ testCases . forEach ( ( [ body , checksumAlgorithm , checksumValue ] ) => {
40
+ const checksumHeader = `x-amz-checksum-${ checksumAlgorithm . toLowerCase ( ) } ` ;
44
41
45
- await client . putObject ( {
46
- Bucket : "b" ,
47
- Key : "k" ,
48
- Body : Buffer . from ( "abcd" ) ,
49
- ChecksumAlgorithm : "SHA256" ,
50
- } ) ;
42
+ it ( `sets ${ checksumHeader } ="${ checksumValue } "" for checksum="${ checksumAlgorithm } "` , async ( ) => {
43
+ requireRequestsFrom ( client ) . toMatch ( {
44
+ method : "PUT" ,
45
+ hostname : "s3.us-west-2.amazonaws.com" ,
46
+ protocol : "https:" ,
47
+ path : "/b/k" ,
48
+ headers : {
49
+ "content-type" : "application/octet-stream" ,
50
+ ...( body . length
51
+ ? {
52
+ "content-length" : body . length . toString ( ) ,
53
+ Expect : "100-continue" ,
54
+ }
55
+ : { } ) ,
56
+ "x-amz-sdk-checksum-algorithm" : checksumAlgorithm ,
57
+ [ checksumHeader ] : checksumValue ,
58
+ host : "s3.us-west-2.amazonaws.com" ,
59
+ "x-amz-user-agent" : / ./ ,
60
+ "user-agent" : / ./ ,
61
+ "amz-sdk-invocation-id" : / ./ ,
62
+ "amz-sdk-request" : / ./ ,
63
+ "x-amz-date" : / ./ ,
64
+ "x-amz-content-sha256" : / ./ ,
65
+ authorization : / ./ ,
66
+ } ,
67
+ query : {
68
+ "x-id" : "PutObject" ,
69
+ } ,
70
+ } ) ;
51
71
52
- expect . hasAssertions ( ) ;
72
+ await client . putObject ( {
73
+ Bucket : "b" ,
74
+ Key : "k" ,
75
+ Body : body ,
76
+ ChecksumAlgorithm : checksumAlgorithm ,
77
+ } ) ;
78
+
79
+ expect . hasAssertions ( ) ;
80
+ } ) ;
81
+ } ) ;
53
82
} ) ;
54
83
55
- it ( "should set flexible checksums (SHA1)" , async ( ) => {
56
- const client = new S3 ( { region : "us-west-2" , logger } ) ;
84
+ describe ( "getObject" , ( ) => {
85
+ testCases . forEach ( ( [ body , checksumAlgorithm , checksumValue ] ) => {
86
+ const checksumHeader = `x-amz-checksum-${ checksumAlgorithm . toLowerCase ( ) } ` ;
87
+
88
+ it ( `validates ${ checksumHeader } ="${ checksumValue } "" set for checksum="${ checksumAlgorithm } "` , async ( ) => {
89
+ const client = new S3 ( {
90
+ region : "us-west-2" ,
91
+ logger,
92
+ requestHandler : new ( class implements HttpHandler {
93
+ async handle ( request : HttpRequest ) : Promise < any > {
94
+ expect ( request ) . toMatchObject ( {
95
+ method : "GET" ,
96
+ hostname : "s3.us-west-2.amazonaws.com" ,
97
+ protocol : "https:" ,
98
+ path : "/b/k" ,
99
+ headers : {
100
+ "x-amz-checksum-mode" : "ENABLED" ,
101
+ host : "s3.us-west-2.amazonaws.com" ,
102
+ "x-amz-user-agent" : / ./ ,
103
+ "user-agent" : / ./ ,
104
+ "amz-sdk-invocation-id" : / ./ ,
105
+ "amz-sdk-request" : / ./ ,
106
+ "x-amz-date" : / ./ ,
107
+ "x-amz-content-sha256" : / ./ ,
108
+ authorization : / ./ ,
109
+ } ,
110
+ query : {
111
+ "x-id" : "GetObject" ,
112
+ } ,
113
+ } ) ;
114
+ return {
115
+ response : new HttpResponse ( {
116
+ statusCode : 200 ,
117
+ headers : {
118
+ "content-type" : "application/octet-stream" ,
119
+ "content-length" : body . length . toString ( ) ,
120
+ [ checksumHeader ] : checksumValue ,
121
+ } ,
122
+ body : Readable . from ( [ body ] ) ,
123
+ } ) ,
124
+ } ;
125
+ }
126
+ updateHttpClientConfig ( key : never , value : never ) : void { }
127
+ httpHandlerConfigs ( ) {
128
+ return { } ;
129
+ }
130
+ } ) ( ) ,
131
+ } ) ;
57
132
58
- requireRequestsFrom ( client ) . toMatch ( {
59
- method : "PUT" ,
60
- hostname : "s3.us-west-2.amazonaws.com" ,
61
- protocol : "https:" ,
62
- path : "/b/k" ,
63
- headers : {
64
- "content-type" : "application/octet-stream" ,
65
- "x-amz-sdk-checksum-algorithm" : "SHA1" ,
66
- "content-length" : "4" ,
67
- Expect : "100-continue" ,
68
- "x-amz-checksum-sha1" : "gf6L/odXbD7LIkJvjleEc4KRes8=" ,
69
- host : "s3.us-west-2.amazonaws.com" ,
70
- "x-amz-user-agent" : / ./ ,
71
- "user-agent" : / ./ ,
72
- "amz-sdk-invocation-id" : / ./ ,
73
- "amz-sdk-request" : / ./ ,
74
- "x-amz-date" : / ./ ,
75
- authorization : / ./ ,
76
- // this is here even if algo is SHA1
77
- "x-amz-content-sha256" : "88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589" ,
78
- } ,
79
- query : {
80
- "x-id" : "PutObject" ,
81
- } ,
82
- } ) ;
133
+ const response = await client . getObject ( {
134
+ Bucket : "b" ,
135
+ Key : "k" ,
136
+ ChecksumMode : "ENABLED" ,
137
+ } ) ;
83
138
84
- await client . putObject ( {
85
- Bucket : "b" ,
86
- Key : "k" ,
87
- Body : Buffer . from ( "abcd" ) ,
88
- ChecksumAlgorithm : "SHA1" ,
139
+ await expect ( response . Body ?. transformToString ( ) ) . resolves . toEqual ( body ) ;
140
+ } ) ;
89
141
} ) ;
90
-
91
- expect . hasAssertions ( ) ;
92
142
} ) ;
93
143
94
144
it ( "should not set binary file content length" , async ( ) => {
95
- const client = new S3 ( { region : "us-west-2" , logger } ) ;
96
-
97
145
requireRequestsFrom ( client ) . toMatch ( {
98
146
method : "PUT" ,
99
147
hostname : "s3.us-west-2.amazonaws.com" ,
@@ -134,8 +182,6 @@ describe("middleware-flexible-checksums", () => {
134
182
[ "CRC32C" , "V" ] ,
135
183
] . forEach ( ( [ algo , id ] ) => {
136
184
it ( `should feature-detect checksum ${ algo } =${ id } ` , async ( ) => {
137
- const client = new S3 ( { region : "us-west-2" , logger } ) ;
138
-
139
185
requireRequestsFrom ( client ) . toMatch ( {
140
186
headers : {
141
187
"user-agent" : new RegExp ( `(.*?) m\/(.*?)${ id } (.*?)$` ) ,
0 commit comments