@@ -169,12 +169,13 @@ describe('BatchRequestClient', () => {
169169 }
170170 } ) ;
171171
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 ( ) => {
173173 const stub = sinon . stub ( httpClient , 'send' ) . resolves (
174174 createMultipartResponse ( [ responseObject ] ) ) ;
175175 stubs . push ( stub ) ;
176176 const requests : SubRequest [ ] = [
177177 { url : 'https://example.com' , body : { foo : 1 } } ,
178+ { url : 'https://example.com' , body : { foo : 2 } } ,
178179 ] ;
179180 const commonHeaders = { 'X-Custom-Header' : 'value' } ;
180181 const batch = new BatchRequestClient ( httpClient , batchUrl , commonHeaders ) ;
@@ -185,9 +186,39 @@ describe('BatchRequestClient', () => {
185186 expect ( stub ) . to . have . been . calledOnce ;
186187 const args : HttpRequestConfig = stub . getCall ( 0 ) . args [ 0 ] ;
187188 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+ } ) ;
188196 } ) ;
189197
190198 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 ( ) => {
191222 const stub = sinon . stub ( httpClient , 'send' ) . resolves (
192223 createMultipartResponse ( [ responseObject ] ) ) ;
193224 stubs . push ( stub ) ;
0 commit comments