1
1
import { ChecksumConstructor } from "@smithy/types" ;
2
+ import * as crypto from "crypto" ;
2
3
3
4
import { ssecMiddleware } from "./" ;
4
5
5
6
describe ( "ssecMiddleware" , ( ) => {
6
7
const next = jest . fn ( ) ;
7
8
const decoder = jest . fn ( ) . mockResolvedValue ( new Uint8Array ( 0 ) ) ;
8
- const encoder = jest . fn ( ) . mockReturnValue ( "base64" ) ;
9
+ const encoder1 = jest . fn ( ) ;
10
+ const encoder2 = jest . fn ( ) ;
9
11
const mockHashUpdate = jest . fn ( ) ;
10
12
const mockHashReset = jest . fn ( ) ;
11
13
const mockHashDigest = jest . fn ( ) . mockReturnValue ( new Uint8Array ( 0 ) ) ;
@@ -17,13 +19,53 @@ describe("ssecMiddleware", () => {
17
19
beforeEach ( ( ) => {
18
20
next . mockClear ( ) ;
19
21
decoder . mockClear ( ) ;
20
- encoder . mockClear ( ) ;
22
+ encoder1 . mockClear ( ) ;
23
+ encoder2 . mockClear ( ) ;
21
24
mockHashUpdate . mockClear ( ) ;
22
25
mockHashDigest . mockClear ( ) ;
23
26
mockHashReset . mockClear ( ) ;
24
27
} ) ;
25
28
26
29
it ( "should base64 encode input keys and set respective MD5 inputs" , async ( ) => {
30
+ encoder1 . mockReturnValue ( "/+JF8FMG8UVMWSaNz0s6Wg==" ) ;
31
+ const key = "TestKey123" ;
32
+ const binaryRepresentationOfKey = Buffer . from ( key ) ;
33
+ const base64Key = binaryRepresentationOfKey . toString ( "base64" ) ;
34
+ const md5Hash = crypto . createHash ( "md5" ) . update ( binaryRepresentationOfKey ) . digest ( ) ;
35
+ const base64Md5Hash = Buffer . from ( md5Hash ) . toString ( "base64" ) ;
36
+
37
+ const args = {
38
+ input : {
39
+ SSECustomerKey : base64Key ,
40
+ CopySourceSSECustomerKey : base64Key ,
41
+ } ,
42
+ } ;
43
+
44
+ const handler = ssecMiddleware ( {
45
+ base64Encoder : encoder1 ,
46
+ utf8Decoder : decoder ,
47
+ md5 : MockHash ,
48
+ } ) ( next , { } as any ) ;
49
+
50
+ await handler ( args ) ;
51
+
52
+ expect ( next . mock . calls . length ) . toBe ( 1 ) ;
53
+ expect ( next ) . toHaveBeenCalledWith ( {
54
+ input : {
55
+ SSECustomerKey : base64Key ,
56
+ SSECustomerKeyMD5 : base64Md5Hash ,
57
+ CopySourceSSECustomerKey : base64Key ,
58
+ CopySourceSSECustomerKeyMD5 : base64Md5Hash ,
59
+ } ,
60
+ } ) ;
61
+ expect ( decoder . mock . calls . length ) . toBe ( 0 ) ;
62
+ expect ( encoder1 . mock . calls . length ) . toBe ( 2 ) ;
63
+ expect ( mockHashUpdate . mock . calls . length ) . toBe ( 2 ) ;
64
+ expect ( mockHashDigest . mock . calls . length ) . toBe ( 2 ) ;
65
+ encoder1 . mockClear ( ) ;
66
+ } ) ;
67
+ it ( "should base64 encode input keys and set respective MD5 inputs" , async ( ) => {
68
+ encoder2 . mockReturnValue ( "base64" ) ;
27
69
const args = {
28
70
input : {
29
71
SSECustomerKey : "foo" ,
@@ -32,7 +74,7 @@ describe("ssecMiddleware", () => {
32
74
} ;
33
75
34
76
const handler = ssecMiddleware ( {
35
- base64Encoder : encoder ,
77
+ base64Encoder : encoder2 ,
36
78
utf8Decoder : decoder ,
37
79
md5 : MockHash ,
38
80
} ) ( next , { } as any ) ;
@@ -49,8 +91,9 @@ describe("ssecMiddleware", () => {
49
91
} ,
50
92
} ) ;
51
93
expect ( decoder . mock . calls . length ) . toBe ( 2 ) ;
52
- expect ( encoder . mock . calls . length ) . toBe ( 4 ) ;
94
+ expect ( encoder2 . mock . calls . length ) . toBe ( 4 ) ;
53
95
expect ( mockHashUpdate . mock . calls . length ) . toBe ( 2 ) ;
54
96
expect ( mockHashDigest . mock . calls . length ) . toBe ( 2 ) ;
97
+ encoder2 . mockClear ( ) ;
55
98
} ) ;
56
99
} ) ;
0 commit comments