@@ -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 } from "../../../src/params" ;
35
36
36
37
function request ( args : {
37
38
data ?: any ;
@@ -227,6 +228,46 @@ 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 = new MockRequest (
245
+ {
246
+ data : { } ,
247
+ } ,
248
+ {
249
+ referrer : "example.com" ,
250
+ "content-type" : "application/json" ,
251
+ origin : "example.com" ,
252
+ }
253
+ ) ;
254
+ req . method = "OPTIONS" ;
255
+
256
+ const response = await runHandler ( func , req as any ) ;
257
+
258
+ expect ( response . status ) . to . equal ( 204 ) ;
259
+ expect ( response . headers ) . to . deep . equal ( {
260
+ "Access-Control-Allow-Origin" : "example.com" ,
261
+ "Access-Control-Allow-Methods" : "GET,HEAD,PUT,PATCH,POST,DELETE" ,
262
+ "Content-Length" : "0" ,
263
+ Vary : "Origin, Access-Control-Request-Headers" ,
264
+ } ) ;
265
+ } finally {
266
+ delete process . env . ORIGINS ;
267
+ clearParams ( ) ;
268
+ }
269
+ } ) ;
270
+
230
271
it ( "should add CORS headers if debug feature is enabled" , async ( ) => {
231
272
sinon . stub ( debug , "isDebugFeatureEnabled" ) . withArgs ( "enableCors" ) . returns ( true ) ;
232
273
0 commit comments