@@ -165,6 +165,19 @@ describe('Server', () => {
165165 } )
166166 } )
167167
168+ it ( 'OPTIONS should return returns custom headers in Access-Control-Allow-Credentials' , ( done ) => {
169+ server . options . allowedCredentials = true
170+
171+ request ( listener )
172+ . options ( '/' )
173+ . expect ( 204 , '' , ( err , res ) => {
174+ res . headers . should . have . property ( 'access-control-allow-credentials' )
175+ res . headers [ 'access-control-allow-credentials' ] . should . containEql ( 'true' )
176+ server . options . allowedCredentials = undefined
177+ done ( err )
178+ } )
179+ } )
180+
168181 it ( 'HEAD should 404 non files' , ( done ) => {
169182 request ( listener )
170183 . head ( '/' )
@@ -252,8 +265,37 @@ describe('Server', () => {
252265 done ( )
253266 } )
254267
255- it ( 'should allow overriding the HTTP method' , async ( ) => {
268+ it ( 'should allow overriding the HTTP origin' , async ( ) => {
269+ const origin = 'vimeo.com'
270+ const req = httpMocks . createRequest ( {
271+ headers : { origin} ,
272+ method : 'OPTIONS' ,
273+ url : '/' ,
274+ } )
275+ // @ts -expect-error todo
276+ const res = new http . ServerResponse ( { method : 'OPTIONS' } )
277+ await server . handle ( req , res )
278+ assert . equal ( res . hasHeader ( 'Access-Control-Allow-Origin' ) , true )
279+ } )
280+
281+ it ( 'should allow overriding the HTTP origin only if match allowedOrigins' , async ( ) => {
282+ const origin = 'vimeo.com'
283+ server . options . allowedOrigins = [ 'vimeo.com' ]
284+ const req = httpMocks . createRequest ( {
285+ headers : { origin} ,
286+ method : 'OPTIONS' ,
287+ url : '/' ,
288+ } )
289+ // @ts -expect-error todo
290+ const res = new http . ServerResponse ( { method : 'OPTIONS' } )
291+ await server . handle ( req , res )
292+ assert . equal ( res . hasHeader ( 'Access-Control-Allow-Origin' ) , true )
293+ assert . equal ( res . getHeader ( 'Access-Control-Allow-Origin' ) , 'vimeo.com' )
294+ } )
295+
296+ it ( 'should allow overriding the HTTP origin only if match allowedOrigins with multiple allowed domains' , async ( ) => {
256297 const origin = 'vimeo.com'
298+ server . options . allowedOrigins = [ 'google.com' , 'vimeo.com' ]
257299 const req = httpMocks . createRequest ( {
258300 headers : { origin} ,
259301 method : 'OPTIONS' ,
@@ -263,6 +305,35 @@ describe('Server', () => {
263305 const res = new http . ServerResponse ( { method : 'OPTIONS' } )
264306 await server . handle ( req , res )
265307 assert . equal ( res . hasHeader ( 'Access-Control-Allow-Origin' ) , true )
308+ assert . equal ( res . getHeader ( 'Access-Control-Allow-Origin' ) , 'vimeo.com' )
309+ } )
310+
311+ it ( `should now allow overriding the HTTP origin if doesn't match allowedOrigins` , async ( ) => {
312+ const origin = 'vimeo.com'
313+ server . options . allowedOrigins = [ 'google.com' ]
314+ const req = httpMocks . createRequest ( {
315+ headers : { origin} ,
316+ method : 'OPTIONS' ,
317+ url : '/' ,
318+ } )
319+ // @ts -expect-error todo
320+ const res = new http . ServerResponse ( { method : 'OPTIONS' } )
321+ await server . handle ( req , res )
322+ assert . equal ( res . hasHeader ( 'Access-Control-Allow-Origin' ) , true )
323+ assert . equal ( res . getHeader ( 'Access-Control-Allow-Origin' ) , 'google.com' )
324+ } )
325+
326+ it ( 'should return Access-Control-Allow-Origin if no origin header' , async ( ) => {
327+ server . options . allowedOrigins = [ 'google.com' ]
328+ const req = httpMocks . createRequest ( {
329+ method : 'OPTIONS' ,
330+ url : '/' ,
331+ } )
332+ // @ts -expect-error todo
333+ const res = new http . ServerResponse ( { method : 'OPTIONS' } )
334+ await server . handle ( req , res )
335+ assert . equal ( res . hasHeader ( 'Access-Control-Allow-Origin' ) , true )
336+ assert . equal ( res . getHeader ( 'Access-Control-Allow-Origin' ) , 'google.com' )
266337 } )
267338
268339 it ( 'should not invoke handlers if onIncomingRequest throws' , ( done ) => {
0 commit comments