@@ -260,6 +260,134 @@ describe('RESTDataSource', () => {
260260 await dataSource . getFoo ( '1' ) ;
261261 } ) ;
262262
263+ it ( 'contains only one Content-Type header when Content-Type already exists is application/json' , async ( ) => {
264+ const requestOptions = {
265+ headers : {
266+ 'Content-Type' : 'application/json' ,
267+ } ,
268+ body : { foo : 'bar' } ,
269+ } ;
270+ const dataSource = new ( class extends RESTDataSource {
271+ override baseURL = 'https://api.example.com' ;
272+
273+ postFoo ( ) {
274+ return this . post ( 'foo' , requestOptions ) ;
275+ }
276+ } ) ( ) ;
277+
278+ const spyOnHttpFetch = jest . spyOn ( dataSource [ 'httpCache' ] , 'fetch' ) ;
279+
280+ nock ( apiUrl )
281+ . post ( '/foo' )
282+ . reply ( 200 , { foo : 'bar' } , { 'Content-Type' : 'application/json' } ) ;
283+
284+ const data = await dataSource . postFoo ( ) ;
285+ expect ( spyOnHttpFetch . mock . calls [ 0 ] [ 1 ] ) . toEqual ( {
286+ headers : { 'content-type' : 'application/json' } ,
287+ body : '{"foo":"bar"}' ,
288+ method : 'POST' ,
289+ params : new URLSearchParams ( ) ,
290+ } ) ;
291+ expect ( data ) . toEqual ( { foo : 'bar' } ) ;
292+ } ) ;
293+
294+ it ( 'converts uppercase-containing headers to lowercase' , async ( ) => {
295+ const requestOptions = {
296+ headers : {
297+ 'Content-Type' : 'application/json' ,
298+ 'Test-Header' : 'foobar' ,
299+ 'ANOTHER-TEST-HEADER' : 'test2' ,
300+ } ,
301+ body : { foo : 'bar' } ,
302+ } ;
303+ const dataSource = new ( class extends RESTDataSource {
304+ override baseURL = 'https://api.example.com' ;
305+
306+ postFoo ( ) {
307+ return this . post ( 'foo' , requestOptions ) ;
308+ }
309+ } ) ( ) ;
310+
311+ const spyOnHttpFetch = jest . spyOn ( dataSource [ 'httpCache' ] , 'fetch' ) ;
312+
313+ nock ( apiUrl )
314+ . post ( '/foo' )
315+ . reply ( 200 , { foo : 'bar' } , { 'Content-Type' : 'application/json' } ) ;
316+
317+ const data = await dataSource . postFoo ( ) ;
318+ expect ( spyOnHttpFetch . mock . calls [ 0 ] [ 1 ] ) . toEqual ( {
319+ headers : {
320+ 'content-type' : 'application/json' ,
321+ 'test-header' : 'foobar' ,
322+ 'another-test-header' : 'test2' ,
323+ } ,
324+ body : '{"foo":"bar"}' ,
325+ method : 'POST' ,
326+ params : new URLSearchParams ( ) ,
327+ } ) ;
328+ expect ( data ) . toEqual ( { foo : 'bar' } ) ;
329+ } ) ;
330+
331+ it ( 'adds an `application/json` content-type header when none is present' , async ( ) => {
332+ const requestOptions = {
333+ body : { foo : 'bar' } ,
334+ } ;
335+ const dataSource = new ( class extends RESTDataSource {
336+ override baseURL = 'https://api.example.com' ;
337+
338+ postFoo ( ) {
339+ return this . post ( 'foo' , requestOptions ) ;
340+ }
341+ } ) ( ) ;
342+
343+ const spyOnHttpFetch = jest . spyOn ( dataSource [ 'httpCache' ] , 'fetch' ) ;
344+
345+ nock ( apiUrl )
346+ . post ( '/foo' )
347+ . reply ( 200 , { foo : 'bar' } , { 'Content-Type' : 'application/json' } ) ;
348+
349+ const data = await dataSource . postFoo ( ) ;
350+ expect ( spyOnHttpFetch . mock . calls [ 0 ] [ 1 ] ) . toEqual ( {
351+ headers : {
352+ 'content-type' : 'application/json' ,
353+ } ,
354+ body : '{"foo":"bar"}' ,
355+ method : 'POST' ,
356+ params : new URLSearchParams ( ) ,
357+ } ) ;
358+ expect ( data ) . toEqual ( { foo : 'bar' } ) ;
359+ } ) ;
360+
361+ it ( 'adds an `application/json` content header when no headers are passed in' , async ( ) => {
362+ const requestOptions = {
363+ body : { foo : 'bar' } ,
364+ } ;
365+ const dataSource = new ( class extends RESTDataSource {
366+ override baseURL = 'https://api.example.com' ;
367+
368+ postFoo ( ) {
369+ return this . post ( 'foo' , requestOptions ) ;
370+ }
371+ } ) ( ) ;
372+
373+ const spyOnHttpFetch = jest . spyOn ( dataSource [ 'httpCache' ] , 'fetch' ) ;
374+
375+ nock ( apiUrl )
376+ . post ( '/foo' )
377+ . reply ( 200 , { foo : 'bar' } , { 'Content-Type' : 'application/json' } ) ;
378+
379+ const data = await dataSource . postFoo ( ) ;
380+ expect ( spyOnHttpFetch . mock . calls [ 0 ] [ 1 ] ) . toEqual ( {
381+ headers : {
382+ 'content-type' : 'application/json' ,
383+ } ,
384+ body : '{"foo":"bar"}' ,
385+ method : 'POST' ,
386+ params : new URLSearchParams ( ) ,
387+ } ) ;
388+ expect ( data ) . toEqual ( { foo : 'bar' } ) ;
389+ } ) ;
390+
263391 it ( 'serializes a request body that is an object as JSON' , async ( ) => {
264392 const expectedFoo = { foo : 'bar' } ;
265393 const dataSource = new ( class extends RESTDataSource {
0 commit comments