@@ -32,6 +32,7 @@ import { FULL_ENDPOINT, MINIMAL_V2_ENDPOINT, FULL_OPTIONS, FULL_TRIGGER } from "
32
32
import { onInit } from "../../../src/v2/core" ;
33
33
import { Handler } from "express" ;
34
34
import { genkit } from "genkit" ;
35
+ import { clearParams , defineList , Expression } from "../../../src/params" ;
35
36
36
37
function request ( args : {
37
38
data ?: any ;
@@ -227,6 +228,43 @@ describe("onRequest", () => {
227
228
} ) ;
228
229
} ) ;
229
230
231
+ it ( "should allow cors params" , async ( ) => {
232
+ const origins = defineList ( "ORIGINS" ) ;
233
+
234
+ try {
235
+ process . env . ORIGINS = '["example.com","example2.com"]' ;
236
+ const func = https . onRequest (
237
+ {
238
+ cors : origins ,
239
+ } ,
240
+ ( req , res ) => {
241
+ res . send ( "42" ) ;
242
+ }
243
+ ) ;
244
+ const req = request ( {
245
+ headers : {
246
+ referrer : "example.com" ,
247
+ "content-type" : "application/json" ,
248
+ origin : "example.com" ,
249
+ } ,
250
+ method : "OPTIONS" ,
251
+ } ) ;
252
+
253
+ const response = await runHandler ( func , req ) ;
254
+
255
+ expect ( response . status ) . to . equal ( 204 ) ;
256
+ expect ( response . headers ) . to . deep . equal ( {
257
+ "Access-Control-Allow-Origin" : "example.com" ,
258
+ "Access-Control-Allow-Methods" : "GET,HEAD,PUT,PATCH,POST,DELETE" ,
259
+ "Content-Length" : "0" ,
260
+ Vary : "Origin, Access-Control-Request-Headers" ,
261
+ } ) ;
262
+ } finally {
263
+ delete process . env . ORIGINS ;
264
+ clearParams ( ) ;
265
+ }
266
+ } ) ;
267
+
230
268
it ( "should add CORS headers if debug feature is enabled" , async ( ) => {
231
269
sinon . stub ( debug , "isDebugFeatureEnabled" ) . withArgs ( "enableCors" ) . returns ( true ) ;
232
270
@@ -301,13 +339,19 @@ describe("onRequest", () => {
301
339
} ) ;
302
340
303
341
describe ( "onCall" , ( ) => {
342
+ let origins : Expression < string [ ] > ;
304
343
beforeEach ( ( ) => {
344
+ origins = defineList ( "ORIGINS" ) ;
345
+ process . env . ORIGINS = '["example.com","example2.com"]' ;
346
+
305
347
options . setGlobalOptions ( { } ) ;
306
348
process . env . GCLOUD_PROJECT = "aProject" ;
307
349
} ) ;
308
350
309
351
afterEach ( ( ) => {
310
352
delete process . env . GCLOUD_PROJECT ;
353
+ delete process . env . ORIGINS ;
354
+ clearParams ( ) ;
311
355
} ) ;
312
356
313
357
it ( "should return a minimal trigger/endpoint with appropriate values" , ( ) => {
@@ -441,6 +485,28 @@ describe("onCall", () => {
441
485
} ) ;
442
486
} ) ;
443
487
488
+ it ( "should allow cors params" , async ( ) => {
489
+ const func = https . onCall ( { cors : origins } , ( ) => 42 ) ;
490
+ const req = request ( {
491
+ headers : {
492
+ referrer : "example.com" ,
493
+ "content-type" : "application/json" ,
494
+ origin : "example.com" ,
495
+ } ,
496
+ method : "OPTIONS" ,
497
+ } ) ;
498
+
499
+ const response = await runHandler ( func , req ) ;
500
+
501
+ expect ( response . status ) . to . equal ( 204 ) ;
502
+ expect ( response . headers ) . to . deep . equal ( {
503
+ "Access-Control-Allow-Origin" : "example.com" ,
504
+ "Access-Control-Allow-Methods" : "POST" ,
505
+ "Content-Length" : "0" ,
506
+ Vary : "Origin, Access-Control-Request-Headers" ,
507
+ } ) ;
508
+ } ) ;
509
+
444
510
it ( "overrides CORS headers if debug feature is enabled" , async ( ) => {
445
511
sinon . stub ( debug , "isDebugFeatureEnabled" ) . withArgs ( "enableCors" ) . returns ( true ) ;
446
512
0 commit comments