@@ -638,6 +638,67 @@ describe('session()', function(){
638638 } )
639639 } )
640640
641+ describe ( 'cookie option' , function ( ) {
642+ describe ( 'when "secure" set to "auto"' , function ( ) {
643+ describe ( 'when "proxy" is "true"' , function ( ) {
644+ before ( function ( ) {
645+ this . server = createServer ( { proxy : true , cookie : { maxAge : 5 , secure : 'auto' } } )
646+ } )
647+
648+ it ( 'should set secure when X-Forwarded-Proto is https' , function ( done ) {
649+ request ( this . server )
650+ . get ( '/' )
651+ . set ( 'X-Forwarded-Proto' , 'https' )
652+ . expect ( shouldSetCookie ( 'connect.sid' ) )
653+ . expect ( shouldSetSecureCookie ( 'connect.sid' ) )
654+ . expect ( 200 , done )
655+ } )
656+ } )
657+
658+ describe ( 'when "proxy" is "false"' , function ( ) {
659+ before ( function ( ) {
660+ this . server = createServer ( { proxy : false , cookie : { maxAge : 5 , secure : 'auto' } } )
661+ } )
662+
663+ it ( 'should not set secure when X-Forwarded-Proto is https' , function ( done ) {
664+ request ( this . server )
665+ . get ( '/' )
666+ . set ( 'X-Forwarded-Proto' , 'https' )
667+ . expect ( shouldSetCookie ( 'connect.sid' ) )
668+ . expect ( shouldNotSetSecureCookie ( 'connect.sid' ) )
669+ . expect ( 200 , done )
670+ } )
671+ } )
672+
673+ describe ( 'when "proxy" is undefined' , function ( ) {
674+ before ( function ( ) {
675+ this . app = express ( )
676+ . use ( function ( req , res , next ) { Object . defineProperty ( req , 'secure' , { value : JSON . parse ( req . headers [ 'x-secure' ] ) } ) ; next ( ) ; } )
677+ . use ( session ( { secret : 'keyboard cat' , cookie : { maxAge : min , secure : 'auto' } } ) )
678+ . use ( function ( req , res ) { res . json ( req . secure ) ; } ) ;
679+ } )
680+
681+ it ( 'should set secure if req.secure = true' , function ( done ) {
682+ request ( this . app )
683+ . get ( '/' )
684+ . set ( 'X-Secure' , 'true' )
685+ . expect ( shouldSetCookie ( 'connect.sid' ) )
686+ . expect ( shouldSetSecureCookie ( 'connect.sid' ) )
687+ . expect ( 200 , 'true' , done )
688+ } )
689+
690+ it ( 'should not set secure if req.secure = false' , function ( done ) {
691+ request ( this . app )
692+ . get ( '/' )
693+ . set ( 'X-Secure' , 'false' )
694+ . expect ( shouldSetCookie ( 'connect.sid' ) )
695+ . expect ( shouldNotSetSecureCookie ( 'connect.sid' ) )
696+ . expect ( 200 , 'false' , done )
697+ } )
698+ } )
699+ } )
700+ } )
701+
641702 describe ( 'genid option' , function ( ) {
642703 it ( 'should reject non-function values' , function ( ) {
643704 assert . throws ( session . bind ( null , { genid : 'bogus!' } ) , / g e n i d .* m u s t / )
@@ -2055,6 +2116,15 @@ function shouldNotHaveHeader(header) {
20552116 }
20562117}
20572118
2119+ function shouldNotSetSecureCookie ( name ) {
2120+ return function ( res ) {
2121+ var header = cookie ( res )
2122+ assert . ok ( header , 'should have a cookie header' )
2123+ assert . equal ( header . split ( '=' ) [ 0 ] , name , 'should set cookie ' + name )
2124+ assert . ok ( header . toLowerCase ( ) . split ( / ; * / ) . every ( function ( k ) { return k !== 'secure' ; } ) , 'should not set secure cookie' )
2125+ }
2126+ }
2127+
20582128function shouldSetCookie ( name ) {
20592129 return function ( res ) {
20602130 var header = cookie ( res )
@@ -2072,6 +2142,15 @@ function shouldSetCookieToValue(name, val) {
20722142 }
20732143}
20742144
2145+ function shouldSetSecureCookie ( name ) {
2146+ return function ( res ) {
2147+ var header = cookie ( res )
2148+ assert . ok ( header , 'should have a cookie header' )
2149+ assert . equal ( header . split ( '=' ) [ 0 ] , name , 'should set cookie ' + name )
2150+ assert . ok ( header . toLowerCase ( ) . split ( / ; * / ) . some ( function ( k ) { return k === 'secure' ; } ) , 'should set secure cookie' )
2151+ }
2152+ }
2153+
20752154function sid ( res ) {
20762155 var match = / ^ [ ^ = ] + = s % 3 A ( [ ^ ; \. ] + ) [ \. ; ] / . exec ( cookie ( res ) )
20772156 var val = match ? match [ 1 ] : undefined
0 commit comments