@@ -4,6 +4,7 @@ import { Readable, Transform } from "stream";
4
4
import { describe , expect , test as it } from "vitest" ;
5
5
6
6
import { requireRequestsFrom } from "../../../private/aws-util-test/src" ;
7
+ import { DEFAULT_CHECKSUM_ALGORITHM , RequestChecksumCalculation , ResponseChecksumValidation } from "./constants" ;
7
8
8
9
describe ( "middleware-flexible-checksums" , ( ) => {
9
10
const logger = {
@@ -14,7 +15,7 @@ describe("middleware-flexible-checksums", () => {
14
15
error ( ) { } ,
15
16
} ;
16
17
17
- const testCases : [ string , ChecksumAlgorithm , string ] [ ] = [
18
+ const testCases : [ string , ChecksumAlgorithm | undefined , string ] [ ] = [
18
19
[ "" , ChecksumAlgorithm . CRC32 , "AAAAAA==" ] ,
19
20
[ "abc" , ChecksumAlgorithm . CRC32 , "NSRBwg==" ] ,
20
21
[ "Hello world" , ChecksumAlgorithm . CRC32 , "i9aeUg==" ] ,
@@ -30,118 +31,146 @@ describe("middleware-flexible-checksums", () => {
30
31
[ "" , ChecksumAlgorithm . SHA256 , "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=" ] ,
31
32
[ "abc" , ChecksumAlgorithm . SHA256 , "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=" ] ,
32
33
[ "Hello world" , ChecksumAlgorithm . SHA256 , "ZOyIygCyaOW6GjVnihtTFtIS9PNmskdyMlNKiuyjfzw=" ] ,
34
+
35
+ // Choose default checksum algorithm when explicily not provided.
36
+ [ "" , undefined , "AAAAAA==" ] ,
37
+ [ "abc" , undefined , "NSRBwg==" ] ,
38
+ [ "Hello world" , undefined , "i9aeUg==" ] ,
33
39
] ;
34
40
35
41
describe ( S3 . name , ( ) => {
36
- const client = new S3 ( { region : "us-west-2" , logger } ) ;
37
-
38
42
describe ( "putObject" , ( ) => {
39
- testCases . forEach ( ( [ body , checksumAlgorithm , checksumValue ] ) => {
40
- const checksumHeader = `x-amz-checksum-${ checksumAlgorithm . toLowerCase ( ) } ` ;
41
-
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
- } ,
43
+ describe . each ( [ undefined , RequestChecksumCalculation . WHEN_SUPPORTED , RequestChecksumCalculation . WHEN_REQUIRED ] ) (
44
+ `when requestChecksumCalculation='%s'` ,
45
+ ( requestChecksumCalculation ) => {
46
+ testCases . forEach ( ( [ body , checksumAlgorithm , checksumValue ] ) => {
47
+ const client = new S3 ( { region : "us-west-2" , logger, requestChecksumCalculation } ) ;
48
+ const checksumHeader = `x-amz-checksum-${ ( checksumAlgorithm ?? DEFAULT_CHECKSUM_ALGORITHM ) . toLowerCase ( ) } ` ;
49
+
50
+ it ( `tests ${ checksumHeader } ="${ checksumValue } "" for checksum="${ checksumAlgorithm } "` , async ( ) => {
51
+ requireRequestsFrom ( client ) . toMatch ( {
52
+ method : "PUT" ,
53
+ hostname : "s3.us-west-2.amazonaws.com" ,
54
+ protocol : "https:" ,
55
+ path : "/b/k" ,
56
+ headers : {
57
+ "content-type" : "application/octet-stream" ,
58
+ ...( body . length
59
+ ? {
60
+ "content-length" : body . length . toString ( ) ,
61
+ Expect : "100-continue" ,
62
+ }
63
+ : { } ) ,
64
+ ...( requestChecksumCalculation === RequestChecksumCalculation . WHEN_REQUIRED &&
65
+ checksumAlgorithm === undefined
66
+ ? { }
67
+ : {
68
+ "x-amz-sdk-checksum-algorithm" : checksumAlgorithm ,
69
+ [ checksumHeader ] : checksumValue ,
70
+ } ) ,
71
+ host : "s3.us-west-2.amazonaws.com" ,
72
+ "x-amz-user-agent" : / ./ ,
73
+ "user-agent" : / ./ ,
74
+ "amz-sdk-invocation-id" : / ./ ,
75
+ "amz-sdk-request" : / ./ ,
76
+ "x-amz-date" : / ./ ,
77
+ "x-amz-content-sha256" : / ./ ,
78
+ authorization : / ./ ,
79
+ } ,
80
+ query : {
81
+ "x-id" : "PutObject" ,
82
+ } ,
83
+ } ) ;
84
+
85
+ await client . putObject ( {
86
+ Bucket : "b" ,
87
+ Key : "k" ,
88
+ Body : body ,
89
+ ChecksumAlgorithm : checksumAlgorithm as ChecksumAlgorithm ,
90
+ } ) ;
91
+
92
+ expect . hasAssertions ( ) ;
93
+ } ) ;
70
94
} ) ;
71
-
72
- await client . putObject ( {
73
- Bucket : "b" ,
74
- Key : "k" ,
75
- Body : body ,
76
- ChecksumAlgorithm : checksumAlgorithm ,
77
- } ) ;
78
-
79
- expect . hasAssertions ( ) ;
80
- } ) ;
81
- } ) ;
95
+ }
96
+ ) ;
82
97
} ) ;
83
98
84
99
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
- } ) ;
132
-
133
- const response = await client . getObject ( {
134
- Bucket : "b" ,
135
- Key : "k" ,
136
- ChecksumMode : "ENABLED" ,
100
+ describe . each ( [ undefined , ResponseChecksumValidation . WHEN_SUPPORTED , ResponseChecksumValidation . WHEN_REQUIRED ] ) (
101
+ `when responseChecksumValidation='%s'` ,
102
+ ( responseChecksumValidation ) => {
103
+ testCases . forEach ( ( [ body , checksumAlgorithm , checksumValue ] ) => {
104
+ const checksumHeader = `x-amz-checksum-${ ( checksumAlgorithm ?? DEFAULT_CHECKSUM_ALGORITHM ) . toLowerCase ( ) } ` ;
105
+
106
+ it ( `validates ${ checksumHeader } ="${ checksumValue } "" for checksum="${ checksumAlgorithm } "` , async ( ) => {
107
+ const client = new S3 ( {
108
+ region : "us-west-2" ,
109
+ logger,
110
+ requestHandler : new ( class implements HttpHandler {
111
+ async handle ( request : HttpRequest ) : Promise < any > {
112
+ expect ( request ) . toMatchObject ( {
113
+ method : "GET" ,
114
+ hostname : "s3.us-west-2.amazonaws.com" ,
115
+ protocol : "https:" ,
116
+ path : "/b/k" ,
117
+ headers : {
118
+ ...( responseChecksumValidation === ResponseChecksumValidation . WHEN_REQUIRED &&
119
+ ! checksumAlgorithm
120
+ ? { }
121
+ : {
122
+ "x-amz-checksum-mode" : "ENABLED" ,
123
+ } ) ,
124
+ host : "s3.us-west-2.amazonaws.com" ,
125
+ "x-amz-user-agent" : / ./ ,
126
+ "user-agent" : / ./ ,
127
+ "amz-sdk-invocation-id" : / ./ ,
128
+ "amz-sdk-request" : / ./ ,
129
+ "x-amz-date" : / ./ ,
130
+ "x-amz-content-sha256" : / ./ ,
131
+ authorization : / ./ ,
132
+ } ,
133
+ query : {
134
+ "x-id" : "GetObject" ,
135
+ } ,
136
+ } ) ;
137
+ return {
138
+ response : new HttpResponse ( {
139
+ statusCode : 200 ,
140
+ headers : {
141
+ "content-type" : "application/octet-stream" ,
142
+ "content-length" : body . length . toString ( ) ,
143
+ [ checksumHeader ] : checksumValue ,
144
+ } ,
145
+ body : Readable . from ( [ body ] ) ,
146
+ } ) ,
147
+ } ;
148
+ }
149
+ updateHttpClientConfig ( key : never , value : never ) : void { }
150
+ httpHandlerConfigs ( ) {
151
+ return { } ;
152
+ }
153
+ } ) ( ) ,
154
+ responseChecksumValidation,
155
+ } ) ;
156
+
157
+ const response = await client . getObject ( {
158
+ Bucket : "b" ,
159
+ Key : "k" ,
160
+ // Do not pass ChecksumMode if algorithm is not explicitly defined. It'll be set by SDK.
161
+ ChecksumMode : checksumAlgorithm ? "ENABLED" : undefined ,
162
+ } ) ;
163
+
164
+ await expect ( response . Body ?. transformToString ( ) ) . resolves . toEqual ( body ) ;
165
+ } ) ;
137
166
} ) ;
138
-
139
- await expect ( response . Body ?. transformToString ( ) ) . resolves . toEqual ( body ) ;
140
- } ) ;
141
- } ) ;
167
+ }
168
+ ) ;
142
169
} ) ;
143
170
144
171
it ( "should not set binary file content length" , async ( ) => {
172
+ const client = new S3 ( { region : "us-west-2" , logger } ) ;
173
+
145
174
requireRequestsFrom ( client ) . toMatch ( {
146
175
method : "PUT" ,
147
176
hostname : "s3.us-west-2.amazonaws.com" ,
@@ -182,6 +211,8 @@ describe("middleware-flexible-checksums", () => {
182
211
[ "CRC32C" , "V" ] ,
183
212
] . forEach ( ( [ algo , id ] ) => {
184
213
it ( `should feature-detect checksum ${ algo } =${ id } ` , async ( ) => {
214
+ const client = new S3 ( { region : "us-west-2" , logger } ) ;
215
+
185
216
requireRequestsFrom ( client ) . toMatch ( {
186
217
headers : {
187
218
"user-agent" : new RegExp ( `(.*?) m\/(.*?)${ id } (.*?)$` ) ,
0 commit comments