@@ -801,6 +801,173 @@ describe('session()', function(){
801801 } )
802802 } )
803803 } )
804+
805+ describe ( 'when "sameSite" set to "auto"' , function ( ) {
806+ describe ( 'basic functionality' , function ( ) {
807+ before ( function ( ) {
808+ function setup ( req ) {
809+ req . secure = JSON . parse ( req . headers [ 'x-secure' ] )
810+ }
811+
812+ function respond ( req , res ) {
813+ res . end ( String ( req . secure ) )
814+ }
815+
816+ this . server = createServer ( setup , { cookie : { sameSite : 'auto' } } , respond )
817+ } )
818+
819+ it ( 'should set SameSite=None for secure connections' , function ( done ) {
820+ request ( this . server )
821+ . get ( '/' )
822+ . set ( 'X-Secure' , 'true' )
823+ . expect ( shouldSetCookieWithAttributeAndValue ( 'connect.sid' , 'SameSite' , 'None' ) )
824+ . expect ( 200 , 'true' , done )
825+ } )
826+
827+ it ( 'should set SameSite=Lax for insecure connections' , function ( done ) {
828+ request ( this . server )
829+ . get ( '/' )
830+ . set ( 'X-Secure' , 'false' )
831+ . expect ( shouldSetCookieWithAttributeAndValue ( 'connect.sid' , 'SameSite' , 'Lax' ) )
832+ . expect ( 200 , 'false' , done )
833+ } )
834+ } )
835+
836+ describe ( 'with proxy settings' , function ( ) {
837+ describe ( 'when "proxy" is "true"' , function ( ) {
838+ before ( function ( ) {
839+ this . server = createServer ( { proxy : true , cookie : { sameSite : 'auto' } } )
840+ } )
841+
842+ it ( 'should set SameSite=None when X-Forwarded-Proto is https' , function ( done ) {
843+ request ( this . server )
844+ . get ( '/' )
845+ . set ( 'X-Forwarded-Proto' , 'https' )
846+ . expect ( shouldSetCookieWithAttributeAndValue ( 'connect.sid' , 'SameSite' , 'None' ) )
847+ . expect ( 200 , done )
848+ } )
849+
850+ it ( 'should set SameSite=Lax when X-Forwarded-Proto is http' , function ( done ) {
851+ request ( this . server )
852+ . get ( '/' )
853+ . set ( 'X-Forwarded-Proto' , 'http' )
854+ . expect ( shouldSetCookieWithAttributeAndValue ( 'connect.sid' , 'SameSite' , 'Lax' ) )
855+ . expect ( 200 , done )
856+ } )
857+ } )
858+
859+ describe ( 'when "proxy" is "false"' , function ( ) {
860+ before ( function ( ) {
861+ this . server = createServer ( { proxy : false , cookie : { sameSite : 'auto' } } )
862+ } )
863+
864+ it ( 'should set SameSite=Lax when X-Forwarded-Proto is https' , function ( done ) {
865+ request ( this . server )
866+ . get ( '/' )
867+ . set ( 'X-Forwarded-Proto' , 'https' )
868+ . expect ( shouldSetCookieWithAttributeAndValue ( 'connect.sid' , 'SameSite' , 'Lax' ) )
869+ . expect ( 200 , done )
870+ } )
871+ } )
872+ } )
873+
874+ describe ( 'combined with secure auto' , function ( ) {
875+ describe ( 'when "secure" is "auto"' , function ( ) {
876+ before ( function ( ) {
877+ function setup ( req ) {
878+ req . secure = JSON . parse ( req . headers [ 'x-secure' ] )
879+ }
880+
881+ function respond ( req , res ) {
882+ res . end ( String ( req . secure ) )
883+ }
884+
885+ this . server = createServer ( setup , { cookie : { secure : 'auto' , sameSite : 'auto' } } , respond )
886+ } )
887+
888+ it ( 'should set both Secure and SameSite=None when secure' , function ( done ) {
889+ request ( this . server )
890+ . get ( '/' )
891+ . set ( 'X-Secure' , 'true' )
892+ . expect ( shouldSetCookieWithAttribute ( 'connect.sid' , 'Secure' ) )
893+ . expect ( shouldSetCookieWithAttributeAndValue ( 'connect.sid' , 'SameSite' , 'None' ) )
894+ . expect ( 200 , 'true' , done )
895+ } )
896+
897+ it ( 'should set neither Secure nor SameSite=None when insecure' , function ( done ) {
898+ request ( this . server )
899+ . get ( '/' )
900+ . set ( 'X-Secure' , 'false' )
901+ . expect ( shouldSetCookieWithoutAttribute ( 'connect.sid' , 'Secure' ) )
902+ . expect ( shouldSetCookieWithAttributeAndValue ( 'connect.sid' , 'SameSite' , 'Lax' ) )
903+ . expect ( 200 , 'false' , done )
904+ } )
905+ } )
906+
907+ describe ( 'when "secure" is "false"' , function ( ) {
908+ before ( function ( ) {
909+ function setup ( req ) {
910+ req . secure = JSON . parse ( req . headers [ 'x-secure' ] )
911+ }
912+
913+ function respond ( req , res ) {
914+ res . end ( String ( req . secure ) )
915+ }
916+
917+ this . server = createServer ( setup , { cookie : { secure : false , sameSite : 'auto' } } , respond )
918+ } )
919+
920+ it ( 'should set SameSite=None without Secure when secure' , function ( done ) {
921+ request ( this . server )
922+ . get ( '/' )
923+ . set ( 'X-Secure' , 'true' )
924+ . expect ( shouldSetCookieWithoutAttribute ( 'connect.sid' , 'Secure' ) )
925+ . expect ( shouldSetCookieWithAttributeAndValue ( 'connect.sid' , 'SameSite' , 'None' ) )
926+ . expect ( 200 , 'true' , done )
927+ } )
928+
929+ it ( 'should set SameSite=Lax without Secure when insecure' , function ( done ) {
930+ request ( this . server )
931+ . get ( '/' )
932+ . set ( 'X-Secure' , 'false' )
933+ . expect ( shouldSetCookieWithoutAttribute ( 'connect.sid' , 'Secure' ) )
934+ . expect ( shouldSetCookieWithAttributeAndValue ( 'connect.sid' , 'SameSite' , 'Lax' ) )
935+ . expect ( 200 , 'false' , done )
936+ } )
937+ } )
938+
939+ describe ( 'when "secure" is "true"' , function ( ) {
940+ before ( function ( ) {
941+ function setup ( req ) {
942+ req . secure = JSON . parse ( req . headers [ 'x-secure' ] )
943+ }
944+
945+ function respond ( req , res ) {
946+ res . end ( String ( req . secure ) )
947+ }
948+
949+ this . server = createServer ( setup , { cookie : { secure : true , sameSite : 'auto' } } , respond )
950+ } )
951+
952+ it ( 'should set both Secure and SameSite=None when secure' , function ( done ) {
953+ request ( this . server )
954+ . get ( '/' )
955+ . set ( 'X-Secure' , 'true' )
956+ . expect ( shouldSetCookieWithAttribute ( 'connect.sid' , 'Secure' ) )
957+ . expect ( shouldSetCookieWithAttributeAndValue ( 'connect.sid' , 'SameSite' , 'None' ) )
958+ . expect ( 200 , 'true' , done )
959+ } )
960+
961+ it ( 'should not set cookie when insecure' , function ( done ) {
962+ request ( this . server )
963+ . get ( '/' )
964+ . set ( 'X-Secure' , 'false' )
965+ . expect ( shouldNotHaveHeader ( 'Set-Cookie' ) )
966+ . expect ( 200 , 'false' , done )
967+ } )
968+ } )
969+ } )
970+ } )
804971 } )
805972
806973 describe ( 'genid option' , function ( ) {
0 commit comments