@@ -32,6 +32,7 @@ import { FULL_ENDPOINT, MINIMAL_V2_ENDPOINT, FULL_OPTIONS, FULL_TRIGGER } from "
3232import { onInit } from "../../../src/v2/core" ;
3333import { Handler } from "express" ;
3434import { genkit } from "genkit" ;
35+ import { clearParams , defineList , Expression } from "../../../src/params" ;
3536
3637function request ( args : {
3738 data ?: any ;
@@ -227,6 +228,43 @@ describe("onRequest", () => {
227228 } ) ;
228229 } ) ;
229230
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+
230268 it ( "should add CORS headers if debug feature is enabled" , async ( ) => {
231269 sinon . stub ( debug , "isDebugFeatureEnabled" ) . withArgs ( "enableCors" ) . returns ( true ) ;
232270
@@ -301,13 +339,19 @@ describe("onRequest", () => {
301339} ) ;
302340
303341describe ( "onCall" , ( ) => {
342+ let origins : Expression < string [ ] > ;
304343 beforeEach ( ( ) => {
344+ origins = defineList ( "ORIGINS" ) ;
345+ process . env . ORIGINS = '["example.com","example2.com"]' ;
346+
305347 options . setGlobalOptions ( { } ) ;
306348 process . env . GCLOUD_PROJECT = "aProject" ;
307349 } ) ;
308350
309351 afterEach ( ( ) => {
310352 delete process . env . GCLOUD_PROJECT ;
353+ delete process . env . ORIGINS ;
354+ clearParams ( ) ;
311355 } ) ;
312356
313357 it ( "should return a minimal trigger/endpoint with appropriate values" , ( ) => {
@@ -441,6 +485,28 @@ describe("onCall", () => {
441485 } ) ;
442486 } ) ;
443487
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+
444510 it ( "overrides CORS headers if debug feature is enabled" , async ( ) => {
445511 sinon . stub ( debug , "isDebugFeatureEnabled" ) . withArgs ( "enableCors" ) . returns ( true ) ;
446512
0 commit comments