@@ -130,6 +130,56 @@ describe('FunctionsApiClient', () => {
130130 expect ( ( ) => new FunctionsApiClient ( null as unknown as FirebaseApp ) )
131131 . to . throw ( 'First argument passed to getFunctions() must be a valid Firebase app instance.' ) ;
132132 } ) ;
133+
134+ it ( 'should cache CLOUD_TASKS_EMULATOR_HOST at construction time' , async ( ) => {
135+ delete process . env . CLOUD_TASKS_EMULATOR_HOST ;
136+ const prodClient = new FunctionsApiClient ( app ) ;
137+
138+ process . env . CLOUD_TASKS_EMULATOR_HOST = CLOUD_TASKS_EMULATOR_HOST ;
139+ const emulatorClient = new FunctionsApiClient ( app ) ;
140+
141+ delete process . env . CLOUD_TASKS_EMULATOR_HOST ;
142+
143+ const sendStub = sinon
144+ . stub ( HttpClient . prototype , 'send' )
145+ . resolves ( utils . responseFrom ( { } , 200 ) ) ;
146+ stubs . push ( sendStub ) ;
147+
148+ await prodClient . delete ( 'mock-task' , FUNCTION_NAME ) ;
149+ await emulatorClient . delete ( 'mock-task' , FUNCTION_NAME ) ;
150+
151+ expect ( sendStub ) . to . have . been . calledTwice ;
152+ expect ( sendStub . firstCall ) . to . have . been . calledWith ( {
153+ method : 'DELETE' ,
154+ url : CLOUD_TASKS_URL . concat ( '/' , 'mock-task' ) ,
155+ headers : EXPECTED_HEADERS ,
156+ } ) ;
157+ expect ( sendStub . secondCall ) . to . have . been . calledWith ( {
158+ method : 'DELETE' ,
159+ url : CLOUD_TASKS_URL_EMULATOR . concat ( '/' , 'mock-task' ) ,
160+ headers : EXPECTED_HEADERS_EMULATOR ,
161+ } ) ;
162+ } ) ;
163+
164+ for ( const hostVal of [ '' , ' ' ] ) {
165+ it ( `should ignore CLOUD_TASKS_EMULATOR_HOST when set to "${ hostVal } "` , async ( ) => {
166+ process . env . CLOUD_TASKS_EMULATOR_HOST = hostVal ;
167+ const emptyHostClient = new FunctionsApiClient ( app ) ;
168+ delete process . env . CLOUD_TASKS_EMULATOR_HOST ;
169+
170+ const stub = sinon
171+ . stub ( HttpClient . prototype , 'send' )
172+ . resolves ( utils . responseFrom ( { } , 200 ) ) ;
173+ stubs . push ( stub ) ;
174+
175+ await emptyHostClient . delete ( 'mock-task' , FUNCTION_NAME ) ;
176+ expect ( stub ) . to . have . been . calledWith ( {
177+ method : 'DELETE' ,
178+ url : CLOUD_TASKS_URL . concat ( '/' , 'mock-task' ) ,
179+ headers : EXPECTED_HEADERS ,
180+ } ) ;
181+ } ) ;
182+ }
133183 } ) ;
134184
135185 describe ( 'enqueue' , ( ) => {
@@ -529,6 +579,7 @@ describe('FunctionsApiClient', () => {
529579 . resolves ( utils . responseFrom ( { } , 200 ) ) ;
530580 stubs . push ( stub ) ;
531581 process . env . CLOUD_TASKS_EMULATOR_HOST = CLOUD_TASKS_EMULATOR_HOST ;
582+ apiClient = new FunctionsApiClient ( app ) ;
532583 return apiClient . enqueue ( { } , FUNCTION_NAME , '' , { uri : TEST_TASK_PAYLOAD . httpRequest . url } )
533584 . then ( ( ) => {
534585 expect ( stub ) . to . have . been . calledOnce . and . calledWith ( {
@@ -550,6 +601,7 @@ describe('FunctionsApiClient', () => {
550601 . resolves ( utils . responseFrom ( { } , 200 ) ) ;
551602 stubs . push ( stub ) ;
552603 process . env . CLOUD_TASKS_EMULATOR_HOST = CLOUD_TASKS_EMULATOR_HOST ;
604+ apiClient = new FunctionsApiClient ( app ) ;
553605 return apiClient . enqueue ( { } , FUNCTION_NAME )
554606 . then ( ( ) => {
555607 expect ( stub ) . to . have . been . calledOnce . and . calledWith ( {
@@ -569,7 +621,6 @@ describe('FunctionsApiClient', () => {
569621 projectId : 'test-project' ,
570622 serviceAccountId : ''
571623 } ) ;
572- apiClient = new FunctionsApiClient ( app ) ;
573624
574625 const expectedPayload = deepCopy ( TEST_TASK_PAYLOAD ) ;
575626 expectedPayload . httpRequest . oidcToken = { serviceAccountEmail : EMULATED_SERVICE_ACCOUNT_DEFAULT } ;
@@ -578,6 +629,7 @@ describe('FunctionsApiClient', () => {
578629 . resolves ( utils . responseFrom ( { } , 200 ) ) ;
579630 stubs . push ( stub ) ;
580631 process . env . CLOUD_TASKS_EMULATOR_HOST = CLOUD_TASKS_EMULATOR_HOST ;
632+ apiClient = new FunctionsApiClient ( app ) ;
581633 return apiClient . enqueue ( { } , FUNCTION_NAME , '' , { uri : TEST_TASK_PAYLOAD . httpRequest . url } )
582634 . then ( ( ) => {
583635 expect ( stub ) . to . have . been . calledOnce . and . calledWith ( {
@@ -631,6 +683,7 @@ describe('FunctionsApiClient', () => {
631683
632684 it ( 'should redirect to the emulator when CLOUD_TASKS_EMULATOR_HOST is set' , async ( ) => {
633685 process . env . CLOUD_TASKS_EMULATOR_HOST = CLOUD_TASKS_EMULATOR_HOST ;
686+ apiClient = new FunctionsApiClient ( app ) ;
634687 const stub = sinon
635688 . stub ( HttpClient . prototype , 'send' )
636689 . resolves ( utils . responseFrom ( { } , 200 ) ) ;
0 commit comments