16
16
17
17
'use strict' ;
18
18
19
- import * as _ from 'lodash' ;
20
19
import * as chai from 'chai' ;
21
20
import * as nock from 'nock' ;
22
21
import * as sinon from 'sinon' ;
@@ -28,8 +27,9 @@ import * as mocks from '../../resources/mocks';
28
27
29
28
import { FirebaseApp } from '../../../src/firebase-app' ;
30
29
import {
31
- ApiSettings , HttpClient , HttpError , AuthorizedHttpClient , ApiCallbackFunction ,
30
+ ApiSettings , HttpClient , HttpError , AuthorizedHttpClient , ApiCallbackFunction , HttpRequestConfig ,
32
31
} from '../../../src/utils/api-request' ;
32
+ import { deepCopy } from '../../../src/utils/deep-copy' ;
33
33
import { Agent } from 'http' ;
34
34
35
35
chai . should ( ) ;
@@ -95,7 +95,7 @@ describe('HttpClient', () => {
95
95
let transportSpy : sinon . SinonSpy = null ;
96
96
97
97
afterEach ( ( ) => {
98
- _ . forEach ( mockedRequests , ( mockedRequest ) => mockedRequest . done ( ) ) ;
98
+ mockedRequests . forEach ( ( mockedRequest ) => mockedRequest . done ( ) ) ;
99
99
mockedRequests = [ ] ;
100
100
if ( transportSpy ) {
101
101
transportSpy . restore ( ) ;
@@ -202,6 +202,38 @@ describe('HttpClient', () => {
202
202
} ) ;
203
203
} ) ;
204
204
205
+ it ( 'should not mutate the arguments' , ( ) => {
206
+ const reqData = { request : 'data' } ;
207
+ const scope = nock ( 'https://' + mockHost , {
208
+ reqheaders : {
209
+ 'Authorization' : 'Bearer token' ,
210
+ 'Content-Type' : ( header ) => {
211
+ return header . startsWith ( 'application/json' ) ; // auto-inserted
212
+ } ,
213
+ 'My-Custom-Header' : 'CustomValue' ,
214
+ } ,
215
+ } ) . post ( mockPath , reqData )
216
+ . reply ( 200 , { success : true } , {
217
+ 'content-type' : 'application/json' ,
218
+ } ) ;
219
+ mockedRequests . push ( scope ) ;
220
+ const client = new HttpClient ( ) ;
221
+ const request : HttpRequestConfig = {
222
+ method : 'POST' ,
223
+ url : mockUrl ,
224
+ headers : {
225
+ 'authorization' : 'Bearer token' ,
226
+ 'My-Custom-Header' : 'CustomValue' ,
227
+ } ,
228
+ data : reqData ,
229
+ } ;
230
+ const requestCopy = deepCopy ( request ) ;
231
+ return client . send ( request ) . then ( ( resp ) => {
232
+ expect ( resp . status ) . to . equal ( 200 ) ;
233
+ expect ( request ) . to . deep . equal ( requestCopy ) ;
234
+ } ) ;
235
+ } ) ;
236
+
205
237
it ( 'should make a GET request with the provided headers and data' , ( ) => {
206
238
const reqData = { key1 : 'value1' , key2 : 'value2' } ;
207
239
const respData = { success : true } ;
@@ -421,7 +453,7 @@ describe('AuthorizedHttpClient', () => {
421
453
} ) ;
422
454
423
455
afterEach ( ( ) => {
424
- _ . forEach ( mockedRequests , ( mockedRequest ) => mockedRequest . done ( ) ) ;
456
+ mockedRequests . forEach ( ( mockedRequest ) => mockedRequest . done ( ) ) ;
425
457
mockedRequests = [ ] ;
426
458
return mockApp . delete ( ) ;
427
459
} ) ;
@@ -516,9 +548,8 @@ describe('AuthorizedHttpClient', () => {
516
548
const respData = { success : true } ;
517
549
const options = {
518
550
reqheaders : {
519
- 'Authorization' : 'Bearer token' ,
520
551
'Content-Type' : ( header : string ) => {
521
- return header . startsWith ( 'application/json' ) ; // auto-inserted by Axios
552
+ return header . startsWith ( 'application/json' ) ; // auto-inserted
522
553
} ,
523
554
'My-Custom-Header' : 'CustomValue' ,
524
555
} ,
@@ -544,6 +575,39 @@ describe('AuthorizedHttpClient', () => {
544
575
expect ( resp . data ) . to . deep . equal ( respData ) ;
545
576
} ) ;
546
577
} ) ;
578
+
579
+ it ( 'should not mutate the arguments' , ( ) => {
580
+ const reqData = { request : 'data' } ;
581
+ const options = {
582
+ reqheaders : {
583
+ 'Content-Type' : ( header : string ) => {
584
+ return header . startsWith ( 'application/json' ) ; // auto-inserted
585
+ } ,
586
+ 'My-Custom-Header' : 'CustomValue' ,
587
+ } ,
588
+ } ;
589
+ Object . assign ( options . reqheaders , requestHeaders . reqheaders ) ;
590
+ const scope = nock ( 'https://' + mockHost , options )
591
+ . post ( mockPath , reqData )
592
+ . reply ( 200 , { success : true } , {
593
+ 'content-type' : 'application/json' ,
594
+ } ) ;
595
+ mockedRequests . push ( scope ) ;
596
+ const client = new AuthorizedHttpClient ( mockApp ) ;
597
+ const request : HttpRequestConfig = {
598
+ method : 'POST' ,
599
+ url : mockUrl ,
600
+ headers : {
601
+ 'My-Custom-Header' : 'CustomValue' ,
602
+ } ,
603
+ data : reqData ,
604
+ } ;
605
+ const requestCopy = deepCopy ( request ) ;
606
+ return client . send ( request ) . then ( ( resp ) => {
607
+ expect ( resp . status ) . to . equal ( 200 ) ;
608
+ expect ( request ) . to . deep . equal ( requestCopy ) ;
609
+ } ) ;
610
+ } ) ;
547
611
} ) ;
548
612
549
613
describe ( 'ApiSettings' , ( ) => {
0 commit comments