2020// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121// SOFTWARE.
2222
23+ import * as sinon from 'sinon' ;
2324import { expect } from 'chai' ;
2425
26+ import * as debug from '../../../src/common/debug' ;
2527import * as options from '../../../src/v2/options' ;
2628import * as https from '../../../src/v2/providers/https' ;
2729import {
@@ -166,7 +168,7 @@ describe('onRequest', () => {
166168 {
167169 'Access-Control-Request-Method' : 'POST' ,
168170 'Access-Control-Request-Headers' : 'origin' ,
169- Origin : 'example.com' ,
171+ origin : 'example.com' ,
170172 }
171173 ) ;
172174 req . method = 'OPTIONS' ;
@@ -181,6 +183,41 @@ describe('onRequest', () => {
181183 Vary : 'Origin, Access-Control-Request-Headers' ,
182184 } ) ;
183185 } ) ;
186+
187+ it ( 'should add CORS headers if debug feature is enabled' , async ( ) => {
188+ sinon
189+ . stub ( debug , 'isDebugFeatureEnabled' )
190+ . withArgs ( 'enableCors' )
191+ . returns ( true ) ;
192+
193+ const func = https . onRequest ( ( req , res ) => {
194+ throw new Error ( 'Should not reach here for OPTIONS preflight' ) ;
195+ } ) ;
196+
197+ const req = new MockRequest (
198+ {
199+ data : { } ,
200+ } ,
201+ {
202+ 'Access-Control-Request-Method' : 'POST' ,
203+ 'Access-Control-Request-Headers' : 'origin' ,
204+ origin : 'localhost' ,
205+ }
206+ ) ;
207+ req . method = 'OPTIONS' ;
208+
209+ const resp = await runHandler ( func , req as any ) ;
210+ expect ( resp . status ) . to . equal ( 204 ) ;
211+ expect ( resp . body ) . to . be . undefined ;
212+ expect ( resp . headers ) . to . deep . equal ( {
213+ 'Access-Control-Allow-Methods' : 'GET,HEAD,PUT,PATCH,POST,DELETE' ,
214+ 'Access-Control-Allow-Origin' : 'localhost' ,
215+ 'Content-Length' : '0' ,
216+ Vary : 'Origin, Access-Control-Request-Headers' ,
217+ } ) ;
218+
219+ sinon . restore ( ) ;
220+ } ) ;
184221} ) ;
185222
186223describe ( 'onCall' , ( ) => {
@@ -317,7 +354,7 @@ describe('onCall', () => {
317354 {
318355 'Access-Control-Request-Method' : 'POST' ,
319356 'Access-Control-Request-Headers' : 'origin' ,
320- Origin : 'example.com' ,
357+ origin : 'example.com' ,
321358 }
322359 ) ;
323360 req . method = 'OPTIONS' ;
@@ -333,6 +370,41 @@ describe('onCall', () => {
333370 } ) ;
334371 } ) ;
335372
373+ it ( 'overrides CORS headers if debug feature is enabled' , async ( ) => {
374+ sinon
375+ . stub ( debug , 'isDebugFeatureEnabled' )
376+ . withArgs ( 'enableCors' )
377+ . returns ( true ) ;
378+
379+ const func = https . onCall ( { cors : 'example.com' } , ( request ) => {
380+ throw new Error ( 'Should not reach here for OPTIONS preflight' ) ;
381+ } ) ;
382+ const req = new MockRequest (
383+ {
384+ data : { } ,
385+ } ,
386+ {
387+ 'Access-Control-Request-Method' : 'POST' ,
388+ 'Access-Control-Request-Headers' : 'origin' ,
389+ origin : 'localhost' ,
390+ }
391+ ) ;
392+ req . method = 'OPTIONS' ;
393+
394+ const response = await runHandler ( func , req as any ) ;
395+
396+ expect ( response . status ) . to . equal ( 204 ) ;
397+ expect ( response . body ) . to . be . undefined ;
398+ expect ( response . headers ) . to . deep . equal ( {
399+ 'Access-Control-Allow-Methods' : 'POST' ,
400+ 'Access-Control-Allow-Origin' : 'localhost' ,
401+ 'Content-Length' : '0' ,
402+ Vary : 'Origin, Access-Control-Request-Headers' ,
403+ } ) ;
404+
405+ sinon . restore ( ) ;
406+ } ) ;
407+
336408 it ( 'adds CORS headers' , async ( ) => {
337409 const func = https . onCall ( ( request ) => 42 ) ;
338410 const req = new MockRequest (
0 commit comments