@@ -9,6 +9,15 @@ jest.mock("./EventSigningStream");
9
9
jest . mock ( "@smithy/eventstream-codec" ) ;
10
10
11
11
describe ( EventStreamPayloadHandler . name , ( ) => {
12
+ const collectData = ( stream : Readable ) => {
13
+ const chunks : any = [ ] ;
14
+ return new Promise ( ( resolve , reject ) => {
15
+ stream . on ( "data" , ( chunk ) => chunks . push ( chunk ) ) ;
16
+ stream . on ( "error" , reject ) ;
17
+ stream . on ( "end" , ( ) => resolve ( Buffer . concat ( chunks ) . toString ( "utf8" ) ) ) ;
18
+ } ) ;
19
+ } ;
20
+
12
21
const mockMessageSigner : MessageSigner = {
13
22
sign : jest . fn ( ) ,
14
23
signMessage : jest . fn ( ) ,
@@ -49,7 +58,7 @@ describe(EventStreamPayloadHandler.name, () => {
49
58
utf8Decoder : mockUtf8Decoder ,
50
59
utf8Encoder : mockUtf8encoder ,
51
60
} ) ;
52
- const mockRequest = { body : new Readable ( ) } as HttpRequest ;
61
+ const mockRequest = { body : new PassThrough ( ) } as HttpRequest ;
53
62
54
63
try {
55
64
await handler . handle ( mockNextHandler , {
@@ -126,6 +135,42 @@ describe(EventStreamPayloadHandler.name, () => {
126
135
} ) ;
127
136
} ) ;
128
137
138
+ it ( "should start piping regardless of whether the downstream resolves" , async ( ) => {
139
+ const authorization =
140
+ "AWS4-HMAC-SHA256 Credential=AKID/20200510/us-west-2/foo/aws4_request, SignedHeaders=host, Signature=1234567890" ;
141
+ const originalPayload = new PassThrough ( ) ;
142
+ const mockRequest = {
143
+ body : originalPayload ,
144
+ headers : { authorization } ,
145
+ } as any ;
146
+ const handler = new EventStreamPayloadHandler ( {
147
+ messageSigner : ( ) => Promise . resolve ( mockMessageSigner ) ,
148
+ utf8Decoder : mockUtf8Decoder ,
149
+ utf8Encoder : mockUtf8encoder ,
150
+ } ) ;
151
+
152
+ ( mockNextHandler as any ) . mockImplementationOnce ( async ( args : FinalizeHandlerArguments < any > ) => {
153
+ const handledRequest = args . request as HttpRequest ;
154
+
155
+ originalPayload . end ( "Some Data" ) ;
156
+ const collected = await collectData ( handledRequest . body ) ;
157
+
158
+ // this means the stream is flowing without this downstream middleware
159
+ // having resolved yet.
160
+ expect ( collected ) . toEqual ( "Some Data" ) ;
161
+
162
+ return Promise . resolve ( { output : { handledRequest } } ) ;
163
+ } ) ;
164
+
165
+ const {
166
+ output : { handledRequest } ,
167
+ } = await handler . handle ( mockNextHandler , {
168
+ request : mockRequest ,
169
+ input : { } ,
170
+ } ) ;
171
+ expect ( handledRequest . body ) . not . toBe ( originalPayload ) ;
172
+ } ) ;
173
+
129
174
it ( "should start piping to request payload through event signer if downstream middleware returns" , async ( ) => {
130
175
const authorization =
131
176
"AWS4-HMAC-SHA256 Credential=AKID/20200510/us-west-2/foo/aws4_request, SignedHeaders=host, Signature=1234567890" ;
@@ -155,14 +200,6 @@ describe(EventStreamPayloadHandler.name, () => {
155
200
expect ( handledRequest . body ) . not . toBe ( originalPayload ) ;
156
201
// Expect the data from the output payload from eventstream payload handler the same as from the
157
202
// stream supplied to the handler.
158
- const collectData = ( stream : Readable ) => {
159
- const chunks : any = [ ] ;
160
- return new Promise ( ( resolve , reject ) => {
161
- stream . on ( "data" , ( chunk ) => chunks . push ( chunk ) ) ;
162
- stream . on ( "error" , reject ) ;
163
- stream . on ( "end" , ( ) => resolve ( Buffer . concat ( chunks ) . toString ( "utf8" ) ) ) ;
164
- } ) ;
165
- } ;
166
203
originalPayload . end ( "Some Data" ) ;
167
204
const collected = await collectData ( handledRequest . body ) ;
168
205
expect ( collected ) . toEqual ( "Some Data" ) ;
0 commit comments