@@ -49,6 +49,28 @@ api.get('/getHeader', function(req,res) {
49
49
} )
50
50
} )
51
51
52
+ api . get ( '/cors' , function ( req , res ) {
53
+ res . cors ( ) . json ( { } )
54
+ } )
55
+
56
+ api . get ( '/corsCustom' , function ( req , res ) {
57
+ res . cors ( {
58
+ origin : 'example.com' ,
59
+ methods : 'GET, OPTIONS' ,
60
+ headers : 'Content-Type, Authorization' ,
61
+ maxAge : 84000000 ,
62
+ credentials : true ,
63
+ exposeHeaders : 'Content-Type'
64
+ } ) . json ( { } )
65
+ } )
66
+
67
+ api . get ( '/corsOverride' , function ( req , res ) {
68
+ res . cors ( ) . cors ( {
69
+ origin : 'example.com' ,
70
+ credentials : true
71
+ } ) . json ( { } )
72
+ } )
73
+
52
74
53
75
/******************************************************************************/
54
76
/*** BEGIN TESTS ***/
@@ -104,4 +126,66 @@ describe('Header Tests:', function() {
104
126
} )
105
127
} ) // end it
106
128
129
+
130
+ it ( 'Add Default CORS Headers' , function ( ) {
131
+ let _event = Object . assign ( { } , event , { path : '/cors' } )
132
+
133
+ return new Promise ( ( resolve , reject ) => {
134
+ api . run ( _event , { } , function ( err , res ) { resolve ( res ) } )
135
+ } ) . then ( ( result ) => {
136
+ expect ( result ) . to . deep . equal ( {
137
+ headers : {
138
+ 'Content-Type' : 'application/json' ,
139
+ 'Access-Control-Allow-Headers' : 'Content-Type, Authorization, Content-Length, X-Requested-With' ,
140
+ 'Access-Control-Allow-Methods' : 'GET, PUT, POST, DELETE, OPTIONS' ,
141
+ 'Access-Control-Allow-Origin' : '*'
142
+ } , statusCode : 200 ,
143
+ body : '{}' ,
144
+ isBase64Encoded : false
145
+ } )
146
+ } )
147
+ } ) // end it
148
+
149
+ it ( 'Add Custom CORS Headers' , function ( ) {
150
+ let _event = Object . assign ( { } , event , { path : '/corsCustom' } )
151
+
152
+ return new Promise ( ( resolve , reject ) => {
153
+ api . run ( _event , { } , function ( err , res ) { resolve ( res ) } )
154
+ } ) . then ( ( result ) => {
155
+ expect ( result ) . to . deep . equal ( {
156
+ headers : {
157
+ 'Content-Type' : 'application/json' ,
158
+ 'Access-Control-Allow-Headers' : 'Content-Type, Authorization' ,
159
+ 'Access-Control-Allow-Methods' : 'GET, OPTIONS' ,
160
+ 'Access-Control-Allow-Origin' : 'example.com' ,
161
+ 'Access-Control-Allow-Credentials' : 'true' ,
162
+ 'Access-Control-Expose-Headers' : 'Content-Type' ,
163
+ 'Access-Control-Max-Age' : '84000'
164
+ } , statusCode : 200 ,
165
+ body : '{}' ,
166
+ isBase64Encoded : false
167
+ } )
168
+ } )
169
+ } ) // end it
170
+
171
+ it ( 'Override CORS Headers' , function ( ) {
172
+ let _event = Object . assign ( { } , event , { path : '/corsOverride' } )
173
+
174
+ return new Promise ( ( resolve , reject ) => {
175
+ api . run ( _event , { } , function ( err , res ) { resolve ( res ) } )
176
+ } ) . then ( ( result ) => {
177
+ expect ( result ) . to . deep . equal ( {
178
+ headers : {
179
+ 'Content-Type' : 'application/json' ,
180
+ 'Access-Control-Allow-Headers' : 'Content-Type, Authorization, Content-Length, X-Requested-With' ,
181
+ 'Access-Control-Allow-Methods' : 'GET, PUT, POST, DELETE, OPTIONS' ,
182
+ 'Access-Control-Allow-Origin' : 'example.com' ,
183
+ 'Access-Control-Allow-Credentials' : 'true'
184
+ } , statusCode : 200 ,
185
+ body : '{}' ,
186
+ isBase64Encoded : false
187
+ } )
188
+ } )
189
+ } ) // end it
190
+
107
191
} ) // end HEADER tests
0 commit comments