@@ -58,6 +58,32 @@ describe('CORS by Middleware', () => {
58
58
} )
59
59
)
60
60
61
+ app . use (
62
+ '/api8/*' ,
63
+ cors ( {
64
+ origin : ( origin ) =>
65
+ new Promise < string > ( ( resolve ) =>
66
+ resolve ( origin . endsWith ( '.example.com' ) ? origin : 'http://example.com' )
67
+ ) ,
68
+ } )
69
+ )
70
+
71
+ app . use (
72
+ '/api9/*' ,
73
+ cors ( {
74
+ origin : ( origin ) =>
75
+ new Promise < string > ( ( resolve ) => resolve ( origin === 'http://example.com' ? origin : '*' ) ) ,
76
+ allowMethods : ( origin ) =>
77
+ new Promise < string [ ] > ( ( resolve ) =>
78
+ resolve (
79
+ origin === 'http://example.com'
80
+ ? [ 'GET' , 'HEAD' , 'POST' , 'PATCH' , 'DELETE' ]
81
+ : [ 'GET' , 'HEAD' ]
82
+ )
83
+ ) ,
84
+ } )
85
+ )
86
+
61
87
app . get ( '/api/abc' , ( c ) => {
62
88
return c . json ( { success : true } )
63
89
} )
@@ -202,6 +228,28 @@ describe('CORS by Middleware', () => {
202
228
expect ( res . headers . get ( 'Access-Control-Allow-Origin' ) ) . toBe ( 'http://example.com' )
203
229
} )
204
230
231
+ it ( 'Allow origins by promise returning function' , async ( ) => {
232
+ let req = new Request ( 'http://localhost/api8/abc' , {
233
+ headers : {
234
+ Origin : 'http://subdomain.example.com' ,
235
+ } ,
236
+ } )
237
+ let res = await app . request ( req )
238
+ expect ( res . headers . get ( 'Access-Control-Allow-Origin' ) ) . toBe ( 'http://subdomain.example.com' )
239
+
240
+ req = new Request ( 'http://localhost/api8/abc' )
241
+ res = await app . request ( req )
242
+ expect ( res . headers . get ( 'Access-Control-Allow-Origin' ) ) . toBe ( 'http://example.com' )
243
+
244
+ req = new Request ( 'http://localhost/api8/abc' , {
245
+ headers : {
246
+ Referer : 'http://evil-example.com/' ,
247
+ } ,
248
+ } )
249
+ res = await app . request ( req )
250
+ expect ( res . headers . get ( 'Access-Control-Allow-Origin' ) ) . toBe ( 'http://example.com' )
251
+ } )
252
+
205
253
it ( 'With raw Response object' , async ( ) => {
206
254
const res = await app . request ( 'http://localhost/api5/abc' )
207
255
@@ -240,4 +288,26 @@ describe('CORS by Middleware', () => {
240
288
expect ( res2 . headers . get ( 'Access-Control-Allow-Origin' ) ) . toBe ( '*' )
241
289
expect ( res2 . headers . get ( 'Access-Control-Allow-Methods' ) ) . toBe ( 'GET,HEAD' )
242
290
} )
291
+
292
+ it ( 'Allow methods by promise returning function' , async ( ) => {
293
+ const req = new Request ( 'http://localhost/api9/abc' , {
294
+ headers : {
295
+ Origin : 'http://example.com' ,
296
+ } ,
297
+ method : 'OPTIONS' ,
298
+ } )
299
+ const res = await app . request ( req )
300
+ expect ( res . headers . get ( 'Access-Control-Allow-Origin' ) ) . toBe ( 'http://example.com' )
301
+ expect ( res . headers . get ( 'Access-Control-Allow-Methods' ) ) . toBe ( 'GET,HEAD,POST,PATCH,DELETE' )
302
+
303
+ const req2 = new Request ( 'http://localhost/api9/abc' , {
304
+ headers : {
305
+ Origin : 'http://example.org' ,
306
+ } ,
307
+ method : 'OPTIONS' ,
308
+ } )
309
+ const res2 = await app . request ( req2 )
310
+ expect ( res2 . headers . get ( 'Access-Control-Allow-Origin' ) ) . toBe ( '*' )
311
+ expect ( res2 . headers . get ( 'Access-Control-Allow-Methods' ) ) . toBe ( 'GET,HEAD' )
312
+ } )
243
313
} )
0 commit comments