@@ -698,4 +698,93 @@ describe('cors.test.js', function() {
698
698
. expect ( 500 , done ) ;
699
699
} ) ;
700
700
} ) ;
701
+
702
+ describe ( 'options.privateNetworkAccess=false' , function ( ) {
703
+ const app = new Koa ( ) ;
704
+ app . use ( cors ( {
705
+ privateNetworkAccess : false ,
706
+ } ) ) ;
707
+
708
+ app . use ( function ( ctx ) {
709
+ ctx . body = { foo : 'bar' } ;
710
+ } ) ;
711
+
712
+ it ( 'should not set `Access-Control-Allow-Private-Network` on not OPTIONS' , function ( done ) {
713
+ request ( app . listen ( ) )
714
+ . get ( '/' )
715
+ . set ( 'Origin' , 'http://koajs.com' )
716
+ . set ( 'Access-Control-Request-Method' , 'PUT' )
717
+ . expect ( res => {
718
+ assert ( ! ( 'Access-Control-Allow-Private-Network' in res . headers ) ) ;
719
+ } )
720
+ . expect ( 200 , done ) ;
721
+ } ) ;
722
+
723
+ it ( 'should not set `Access-Control-Allow-Private-Network` if `Access-Control-Request-Private-Network` not exist on OPTIONS' , function ( done ) {
724
+ request ( app . listen ( ) )
725
+ . options ( '/' )
726
+ . set ( 'Origin' , 'http://koajs.com' )
727
+ . set ( 'Access-Control-Request-Method' , 'PUT' )
728
+ . expect ( res => {
729
+ assert ( ! ( 'Access-Control-Allow-Private-Network' in res . headers ) ) ;
730
+ } )
731
+ . expect ( 204 , done ) ;
732
+ } ) ;
733
+
734
+ it ( 'should not set `Access-Control-Allow-Private-Network` if `Access-Control-Request-Private-Network` exist on OPTIONS' , function ( done ) {
735
+ request ( app . listen ( ) )
736
+ . options ( '/' )
737
+ . set ( 'Origin' , 'http://koajs.com' )
738
+ . set ( 'Access-Control-Request-Method' , 'PUT' )
739
+ . set ( 'Access-Control-Request-Private-Network' , 'true' )
740
+ . expect ( res => {
741
+ assert ( ! ( 'Access-Control-Allow-Private-Network' in res . headers ) ) ;
742
+ } )
743
+ . expect ( 204 , done ) ;
744
+ } ) ;
745
+ } ) ;
746
+
747
+ describe ( 'options.privateNetworkAccess=true' , function ( ) {
748
+ const app = new Koa ( ) ;
749
+ app . use ( cors ( {
750
+ privateNetworkAccess : true ,
751
+ } ) ) ;
752
+
753
+ app . use ( function ( ctx ) {
754
+ ctx . body = { foo : 'bar' } ;
755
+ } ) ;
756
+
757
+ it ( 'should not set `Access-Control-Allow-Private-Network` on not OPTIONS' , function ( done ) {
758
+ request ( app . listen ( ) )
759
+ . get ( '/' )
760
+ . set ( 'Origin' , 'http://koajs.com' )
761
+ . set ( 'Access-Control-Request-Method' , 'PUT' )
762
+ . expect ( res => {
763
+ assert ( ! ( 'Access-Control-Allow-Private-Network' in res . headers ) ) ;
764
+ } )
765
+ . expect ( 200 , done ) ;
766
+ } ) ;
767
+
768
+ it ( 'should not set `Access-Control-Allow-Private-Network` if `Access-Control-Request-Private-Network` not exist on OPTIONS' , function ( done ) {
769
+ request ( app . listen ( ) )
770
+ . options ( '/' )
771
+ . set ( 'Origin' , 'http://koajs.com' )
772
+ . set ( 'Access-Control-Request-Method' , 'PUT' )
773
+ . expect ( res => {
774
+ assert ( ! ( 'Access-Control-Allow-Private-Network' in res . headers ) ) ;
775
+ } )
776
+ . expect ( 204 , done ) ;
777
+ } ) ;
778
+
779
+ it ( 'should always set `Access-Control-Allow-Private-Network` if `Access-Control-Request-Private-Network` exist on OPTIONS' , function ( done ) {
780
+ request ( app . listen ( ) )
781
+ . options ( '/' )
782
+ . set ( 'Origin' , 'http://koajs.com' )
783
+ . set ( 'Access-Control-Request-Method' , 'PUT' )
784
+ . set ( 'Access-Control-Request-Private-Network' , 'true' )
785
+ . expect ( 'Access-Control-Allow-Private-Network' , 'true' )
786
+ . expect ( 204 , done ) ;
787
+ } ) ;
788
+ } ) ;
789
+
701
790
} ) ;
0 commit comments