@@ -169,12 +169,13 @@ describe('BatchRequestClient', () => {
169
169
}
170
170
} ) ;
171
171
172
- it ( 'should add common headers to the batch requests' , async ( ) => {
172
+ it ( 'should add common headers to the parent and sub requests in a batch ' , async ( ) => {
173
173
const stub = sinon . stub ( httpClient , 'send' ) . resolves (
174
174
createMultipartResponse ( [ responseObject ] ) ) ;
175
175
stubs . push ( stub ) ;
176
176
const requests : SubRequest [ ] = [
177
177
{ url : 'https://example.com' , body : { foo : 1 } } ,
178
+ { url : 'https://example.com' , body : { foo : 2 } } ,
178
179
] ;
179
180
const commonHeaders = { 'X-Custom-Header' : 'value' } ;
180
181
const batch = new BatchRequestClient ( httpClient , batchUrl , commonHeaders ) ;
@@ -185,9 +186,39 @@ describe('BatchRequestClient', () => {
185
186
expect ( stub ) . to . have . been . calledOnce ;
186
187
const args : HttpRequestConfig = stub . getCall ( 0 ) . args [ 0 ] ;
187
188
expect ( args . headers ) . to . have . property ( 'X-Custom-Header' , 'value' ) ;
189
+
190
+ const parsedRequest = parseHttpRequest ( args . data as Buffer ) ;
191
+ expect ( parsedRequest . multipart . length ) . to . equal ( requests . length ) ;
192
+ parsedRequest . multipart . forEach ( ( sub : { body : Buffer } ) => {
193
+ const parsedSubRequest : { headers : object } = parseHttpRequest ( sub . body . toString ( ) . trim ( ) ) ;
194
+ expect ( parsedSubRequest . headers ) . to . have . property ( 'X-Custom-Header' , 'value' ) ;
195
+ } ) ;
188
196
} ) ;
189
197
190
198
it ( 'should add sub request headers to the payload' , async ( ) => {
199
+ const stub = sinon . stub ( httpClient , 'send' ) . resolves (
200
+ createMultipartResponse ( [ responseObject ] ) ) ;
201
+ stubs . push ( stub ) ;
202
+ const requests : SubRequest [ ] = [
203
+ { url : 'https://example.com' , body : { foo : 1 } , headers : { 'X-Custom-Header' : 'value' } } ,
204
+ { url : 'https://example.com' , body : { foo : 1 } , headers : { 'X-Custom-Header' : 'value' } } ,
205
+ ] ;
206
+ const batch = new BatchRequestClient ( httpClient , batchUrl ) ;
207
+
208
+ const responses : HttpResponse [ ] = await batch . send ( requests ) ;
209
+
210
+ expect ( responses . length ) . to . equal ( 1 ) ;
211
+ expect ( stub ) . to . have . been . calledOnce ;
212
+ const args : HttpRequestConfig = stub . getCall ( 0 ) . args [ 0 ] ;
213
+ const parsedRequest = parseHttpRequest ( args . data as Buffer ) ;
214
+ expect ( parsedRequest . multipart . length ) . to . equal ( requests . length ) ;
215
+ parsedRequest . multipart . forEach ( ( sub : { body : Buffer } ) => {
216
+ const parsedSubRequest : { headers : object } = parseHttpRequest ( sub . body . toString ( ) . trim ( ) ) ;
217
+ expect ( parsedSubRequest . headers ) . to . have . property ( 'X-Custom-Header' , 'value' ) ;
218
+ } ) ;
219
+ } ) ;
220
+
221
+ it ( 'sub request headers should get precedence' , async ( ) => {
191
222
const stub = sinon . stub ( httpClient , 'send' ) . resolves (
192
223
createMultipartResponse ( [ responseObject ] ) ) ;
193
224
stubs . push ( stub ) ;
0 commit comments