@@ -201,6 +201,37 @@ describe('cors.test.js', function() {
201
201
. expect ( 'Access-Control-Allow-Origin' , '*' )
202
202
. expect ( 200 , done ) ;
203
203
} ) ;
204
+
205
+ it ( 'behaves correctly when the return type is promise-like' , function ( done ) {
206
+ class WrappedPromise {
207
+ constructor ( ...args ) {
208
+ this . internalPromise = new Promise ( ...args ) ;
209
+ }
210
+
211
+ then ( onFulfilled ) {
212
+ this . internalPromise . then ( onFulfilled ) ;
213
+ }
214
+ }
215
+
216
+ const app = new Koa ( )
217
+ . use ( cors ( {
218
+ origin ( ) {
219
+ return new WrappedPromise ( resolve => {
220
+ return resolve ( '*' ) ;
221
+ } ) ;
222
+ } ,
223
+ } ) )
224
+ . use ( function ( ctx ) {
225
+ ctx . body = { foo : 'bar' } ;
226
+ } ) ;
227
+
228
+ request ( app . listen ( ) )
229
+ . get ( '/' )
230
+ . set ( 'Origin' , 'http://koajs.com' )
231
+ . expect ( { foo : 'bar' } )
232
+ . expect ( 'Access-Control-Allow-Origin' , '*' )
233
+ . expect ( 200 , done ) ;
234
+ } ) ;
204
235
} ) ;
205
236
206
237
describe ( 'options.exposeHeaders' , function ( ) {
@@ -449,6 +480,37 @@ describe('cors.test.js', function() {
449
480
. expect ( 'Access-Control-Allow-Credentials' , 'true' )
450
481
. expect ( 204 , done ) ;
451
482
} ) ;
483
+
484
+ it ( 'behaves correctly when the return type is promise-like' , function ( done ) {
485
+ class WrappedPromise {
486
+ constructor ( ...args ) {
487
+ this . internalPromise = new Promise ( ...args ) ;
488
+ }
489
+
490
+ then ( onFulfilled ) {
491
+ this . internalPromise . then ( onFulfilled ) ;
492
+ }
493
+ }
494
+
495
+ const app = new Koa ( )
496
+ . use ( cors ( {
497
+ credentials ( ) {
498
+ return new WrappedPromise ( resolve => {
499
+ resolve ( true ) ;
500
+ } ) ;
501
+ } ,
502
+ } ) )
503
+ . use ( function ( ctx ) {
504
+ ctx . body = { foo : 'bar' } ;
505
+ } ) ;
506
+
507
+ request ( app . listen ( ) )
508
+ . get ( '/' )
509
+ . set ( 'Origin' , 'http://koajs.com' )
510
+ . expect ( 'Access-Control-Allow-Credentials' , 'true' )
511
+ . expect ( { foo : 'bar' } )
512
+ . expect ( 200 , done ) ;
513
+ } ) ;
452
514
} ) ;
453
515
454
516
describe ( 'options.allowHeaders' , function ( ) {
0 commit comments