File tree Expand file tree Collapse file tree 2 files changed +53
-4
lines changed
packages/middleware-sdk-transcribe-streaming/src Expand file tree Collapse file tree 2 files changed +53
-4
lines changed Original file line number Diff line number Diff line change
1
+ import { SignatureV4 } from "./signer" ;
2
+ import { HttpRequest } from "@aws-sdk/protocol-http" ;
3
+ describe ( "transcribe streaming" , ( ) => {
4
+ describe ( "WebSocket request signer" , ( ) => {
5
+ it ( "should invoke base SigV4 signer correctly" , async ( ) => {
6
+ expect . assertions ( 4 ) ;
7
+ const mockBaseSigner = {
8
+ presign : jest . fn ( )
9
+ } ;
10
+ const signer = new SignatureV4 ( { signer : mockBaseSigner as any } ) ;
11
+ const toSign = new HttpRequest ( {
12
+ headers : {
13
+ "x-amz-foo" : "foo" ,
14
+ bar : "bar" ,
15
+ "amz-sdk-invocation-id" : "123" ,
16
+ "amz-sdk-request" : "attempt=1" ,
17
+ host : "aws.amazon.com"
18
+ } ,
19
+ body : "hello world" ,
20
+ query : {
21
+ prop1 : "A" ,
22
+ prop2 : "B"
23
+ }
24
+ } ) ;
25
+ const signed = await signer . sign ( toSign ) ;
26
+ expect ( toSign ) . toMatchObject ( signed ) ;
27
+ expect ( mockBaseSigner . presign ) . toBeCalled ( ) ;
28
+ // The request's body should not be presigned
29
+ expect ( mockBaseSigner . presign . mock . calls [ 0 ] [ 0 ] . body ) . toEqual ( "" ) ;
30
+ expect ( mockBaseSigner . presign . mock . calls [ 0 ] [ 1 ] ! . unsignableHeaders )
31
+ . toBeDefined ;
32
+ const unsignableHeaders = mockBaseSigner . presign . mock . calls [ 0 ] [ 1 ] !
33
+ . unsignableHeaders ;
34
+ expect ( unsignableHeaders ) . toEqual (
35
+ new Set ( Object . keys ( toSign . headers ) . filter ( a => a !== "host" ) )
36
+ ) ;
37
+ } ) ;
38
+ } ) ;
39
+ } ) ;
Original file line number Diff line number Diff line change @@ -27,10 +27,20 @@ export class SignatureV4 implements RequestSigner, RequestPresigner {
27
27
) : Promise < IHttpRequest > {
28
28
if ( HttpRequest . isInstance ( toSign ) ) {
29
29
// Presign the endpoint url with empty body, otherwise
30
- // the payload hash would be UNSINGED_PAYLOAD
31
- const signedRequest = await this . signer . presign ( { ...toSign , body : "" } , {
32
- expiresIn : 5 * 60 // presigned url must be expired within 5 mins
33
- } as any ) ;
30
+ // the payload hash would be UNSINGED-PAYLOAD
31
+ const signedRequest = await this . signer . presign (
32
+ { ...toSign , body : "" } ,
33
+ {
34
+ // presigned url must be expired within 5 mins.
35
+ expiresIn : 5 * 60 ,
36
+ // Not to sign headers. Transcribe-streaming WebSocket
37
+ // request omits headers except for required 'host' header. If we sign
38
+ // the other headers, the signature could be mismatch.
39
+ unsignableHeaders : new Set (
40
+ Object . keys ( toSign . headers ) . filter ( header => header !== "host" )
41
+ )
42
+ }
43
+ ) ;
34
44
return {
35
45
...signedRequest ,
36
46
body : toSign . body
You can’t perform that action at this time.
0 commit comments