1
- 'use strict' ;
2
-
3
1
const assert = require ( 'assert' ) ;
4
2
const Koa = require ( 'koa' ) ;
5
3
const request = require ( 'supertest' ) ;
@@ -13,22 +11,19 @@ describe('cors.test.js', function() {
13
11
ctx . body = { foo : 'bar' } ;
14
12
} ) ;
15
13
16
- it ( 'should not set `Access-Control-Allow-Origin` when request Origin header missing' , function ( done ) {
14
+ it ( 'should set `Access-Control-Allow-Origin` to `* ` when request Origin header missing' , function ( done ) {
17
15
request ( app . listen ( ) )
18
16
. get ( '/' )
19
17
. expect ( { foo : 'bar' } )
20
- . expect ( 200 , function ( err , res ) {
21
- assert ( ! err ) ;
22
- assert ( ! res . headers [ 'access-control-allow-origin' ] ) ;
23
- done ( ) ;
24
- } ) ;
18
+ . expect ( 'access-control-allow-origin' , '*' )
19
+ . expect ( 200 , done ) ;
25
20
} ) ;
26
21
27
- it ( 'should set `Access-Control-Allow-Origin` to request origin header ' , function ( done ) {
22
+ it ( 'should set `Access-Control-Allow-Origin` to `*` ' , function ( done ) {
28
23
request ( app . listen ( ) )
29
24
. get ( '/' )
30
25
. set ( 'Origin' , 'http://koajs.com' )
31
- . expect ( 'Access-Control-Allow-Origin' , 'http://koajs.com ' )
26
+ . expect ( 'Access-Control-Allow-Origin' , '* ' )
32
27
. expect ( { foo : 'bar' } )
33
28
. expect ( 200 , done ) ;
34
29
} ) ;
@@ -38,7 +33,7 @@ describe('cors.test.js', function() {
38
33
. options ( '/' )
39
34
. set ( 'Origin' , 'http://koajs.com' )
40
35
. set ( 'Access-Control-Request-Method' , 'PUT' )
41
- . expect ( 'Access-Control-Allow-Origin' , 'http://koajs.com ' )
36
+ . expect ( 'Access-Control-Allow-Origin' , '* ' )
42
37
. expect ( 'Access-Control-Allow-Methods' , 'GET,HEAD,PUT,POST,DELETE,PATCH' )
43
38
. expect ( 204 , done ) ;
44
39
} ) ;
@@ -87,6 +82,44 @@ describe('cors.test.js', function() {
87
82
} ) ;
88
83
} ) ;
89
84
85
+ describe ( 'options.origin set the request Origin header' , function ( ) {
86
+ const app = new Koa ( ) ;
87
+ app . use ( cors ( {
88
+ origin ( ctx ) {
89
+ return ctx . get ( 'Origin' ) || '*' ;
90
+ } ,
91
+ } ) ) ;
92
+ app . use ( function ( ctx ) {
93
+ ctx . body = { foo : 'bar' } ;
94
+ } ) ;
95
+
96
+ it ( 'should set `Access-Control-Allow-Origin` to request `Origin` header' , function ( done ) {
97
+ request ( app . listen ( ) )
98
+ . get ( '/' )
99
+ . set ( 'Origin' , 'http://koajs.com' )
100
+ . expect ( 'Access-Control-Allow-Origin' , 'http://koajs.com' )
101
+ . expect ( { foo : 'bar' } )
102
+ . expect ( 200 , done ) ;
103
+ } ) ;
104
+
105
+ it ( 'should set `Access-Control-Allow-Origin` to request `origin` header' , function ( done ) {
106
+ request ( app . listen ( ) )
107
+ . get ( '/' )
108
+ . set ( 'origin' , 'http://origin.koajs.com' )
109
+ . expect ( 'Access-Control-Allow-Origin' , 'http://origin.koajs.com' )
110
+ . expect ( { foo : 'bar' } )
111
+ . expect ( 200 , done ) ;
112
+ } ) ;
113
+
114
+ it ( 'should set `Access-Control-Allow-Origin` to `*`, even if no Origin is passed on request' , function ( done ) {
115
+ request ( app . listen ( ) )
116
+ . get ( '/' )
117
+ . expect ( 'Access-Control-Allow-Origin' , '*' )
118
+ . expect ( { foo : 'bar' } )
119
+ . expect ( 200 , done ) ;
120
+ } ) ;
121
+ } ) ;
122
+
90
123
describe ( 'options.secureContext=true' , function ( ) {
91
124
const app = new Koa ( ) ;
92
125
app . use ( cors ( {
@@ -651,7 +684,11 @@ describe('cors.test.js', function() {
651
684
describe ( 'options.headersKeptOnError' , function ( ) {
652
685
it ( 'should keep CORS headers after an error' , function ( done ) {
653
686
const app = new Koa ( ) ;
654
- app . use ( cors ( ) ) ;
687
+ app . use ( cors ( {
688
+ origin ( ctx ) {
689
+ return ctx . get ( 'Origin' ) || '*' ;
690
+ } ,
691
+ } ) ) ;
655
692
app . use ( function ( ctx ) {
656
693
ctx . body = { foo : 'bar' } ;
657
694
throw new Error ( 'Whoops!' ) ;
@@ -668,7 +705,11 @@ describe('cors.test.js', function() {
668
705
669
706
it ( 'should not affect OPTIONS requests' , function ( done ) {
670
707
const app = new Koa ( ) ;
671
- app . use ( cors ( ) ) ;
708
+ app . use ( cors ( {
709
+ origin ( ctx ) {
710
+ return ctx . get ( 'Origin' ) || '*' ;
711
+ } ,
712
+ } ) ) ;
672
713
app . use ( function ( ctx ) {
673
714
ctx . body = { foo : 'bar' } ;
674
715
throw new Error ( 'Whoops!' ) ;
@@ -684,7 +725,11 @@ describe('cors.test.js', function() {
684
725
685
726
it ( 'should not keep unrelated headers' , function ( done ) {
686
727
const app = new Koa ( ) ;
687
- app . use ( cors ( ) ) ;
728
+ app . use ( cors ( {
729
+ origin ( ctx ) {
730
+ return ctx . get ( 'Origin' ) || '*' ;
731
+ } ,
732
+ } ) ) ;
688
733
app . use ( function ( ctx ) {
689
734
ctx . body = { foo : 'bar' } ;
690
735
ctx . set ( 'X-Example' , 'Value' ) ;
@@ -752,6 +797,7 @@ describe('cors.test.js', function() {
752
797
. expect ( 200 , done ) ;
753
798
} ) ;
754
799
} ) ;
800
+
755
801
describe ( 'other middleware has set vary header on Error' , function ( ) {
756
802
it ( 'should append `Origin to other `Vary` header' , function ( done ) {
757
803
const app = new Koa ( ) ;