@@ -2620,6 +2620,115 @@ describe('session()', function(){
26202620 } )
26212621} )
26222622
2623+ describe ( 'path matching (RFC 6265)' , function ( ) {
2624+ describe ( 'when "path" is "/" (root path)' , function ( ) {
2625+ before ( function ( ) {
2626+ this . server = createServer ( { cookie : { path : '/' } } )
2627+ } )
2628+
2629+ it ( 'should set cookie when request-path is "/" (root path)' , function ( done ) {
2630+ // RFC 6265 5.1.4: "The cookie-path and the request-path are identical."
2631+ request ( this . server )
2632+ . get ( '/' )
2633+ . expect ( shouldSetCookie ( 'connect.sid' ) )
2634+ . expect ( 200 , done )
2635+ } )
2636+
2637+ it ( 'should set cookie when request-path is any path ("/foo")' , function ( done ) {
2638+ // RFC 6265 5.1.4: "The cookie-path is a prefix of the request-path, and the last
2639+ // character of the cookie-path is %x2F ("/")."
2640+ request ( this . server )
2641+ . get ( '/foo' )
2642+ . expect ( shouldSetCookie ( 'connect.sid' ) )
2643+ . expect ( 200 , done )
2644+ } )
2645+
2646+ it ( 'should set cookie when request-path has multiple segments ("/foo/bar/baz")' , function ( done ) {
2647+ // RFC 6265 5.1.4: "The cookie-path is a prefix of the request-path, and the last
2648+ // character of the cookie-path is %x2F ("/")."
2649+ request ( this . server )
2650+ . get ( '/foo/bar/baz' )
2651+ . expect ( shouldSetCookie ( 'connect.sid' ) )
2652+ . expect ( 200 , done )
2653+ } )
2654+ } )
2655+
2656+ describe ( 'when "path" is "/admin"' , function ( ) {
2657+ before ( function ( ) {
2658+ this . server = createServer ( { cookie : { path : '/admin' } } )
2659+ } )
2660+
2661+ it ( 'should set cookie when request-path and cookie-path are identical ("/admin")' , function ( done ) {
2662+ // RFC 6265 5.1.4: "The cookie-path and the request-path are identical."
2663+ request ( this . server )
2664+ . get ( '/admin' )
2665+ . expect ( shouldSetCookie ( 'connect.sid' ) )
2666+ . expect ( 200 , done )
2667+ } )
2668+
2669+ it ( 'should set cookie when cookie-path is prefix and last char is "/" ("/admin/")' , function ( done ) {
2670+ // RFC 6265 5.1.4: "The cookie-path is a prefix of the request-path, and the last
2671+ // character of the cookie-path is %x2F ("/")."
2672+ request ( this . server )
2673+ . get ( '/admin/' )
2674+ . expect ( shouldSetCookie ( 'connect.sid' ) )
2675+ . expect ( 200 , done )
2676+ } )
2677+
2678+ it ( 'should set cookie when cookie-path is prefix and next char is "/" ("/admin/users")' , function ( done ) {
2679+ // RFC 6265 5.1.4: "The cookie-path is a prefix of the request-path, and the first
2680+ // character of the request-path that is not included in the cookie-path is a %x2F ("/") character."
2681+ request ( this . server )
2682+ . get ( '/admin/users' )
2683+ . expect ( shouldSetCookie ( 'connect.sid' ) )
2684+ . expect ( 200 , done )
2685+ } )
2686+
2687+ it ( 'should NOT set cookie when cookie-path is not a prefix ("/administrator")' , function ( done ) {
2688+ // RFC 6265 5.1.4: None of the path-match conditions are met
2689+ request ( this . server )
2690+ . get ( '/administrator' )
2691+ . expect ( shouldNotHaveHeader ( 'Set-Cookie' ) )
2692+ . expect ( 200 , done )
2693+ } )
2694+ } )
2695+
2696+ describe ( 'when "path" is "/admin/" (trailing slash)' , function ( ) {
2697+ before ( function ( ) {
2698+ this . server = createServer ( { cookie : { path : '/admin/' } } )
2699+ } )
2700+
2701+ it ( 'should set cookie when cookie-path is prefix and last char is "/" ("/admin/x")' , function ( done ) {
2702+ // RFC 6265 5.1.4: "The cookie-path is a prefix of the request-path, and the last
2703+ // character of the cookie-path is %x2F ("/")."
2704+ request ( this . server )
2705+ . get ( '/admin/x' )
2706+ . expect ( shouldSetCookie ( 'connect.sid' ) )
2707+ . expect ( 200 , done )
2708+ } )
2709+
2710+ it ( 'should NOT set cookie when request-path is not prefixed by cookie-path ("/admin")' , function ( done ) {
2711+ // RFC 6265 5.1.4: cookie-path "/admin/" is not a prefix of request-path "/admin"
2712+ request ( this . server )
2713+ . get ( '/admin' )
2714+ . expect ( shouldNotHaveHeader ( 'Set-Cookie' ) )
2715+ . expect ( 200 , done )
2716+ } )
2717+
2718+ it ( 'should NOT set cookie when cookie-path is not a prefix ("/administrator")' , function ( done ) {
2719+ // RFC 6265 5.1.4: None of the path-match conditions are met:
2720+ // 1. The paths are not identical
2721+ // 2. "/admin/" is not a prefix of "/administrator"
2722+ // 3. The prefix condition with next character "/" is not applicable
2723+ request ( this . server )
2724+ . get ( '/administrator' )
2725+ . expect ( shouldNotHaveHeader ( 'Set-Cookie' ) )
2726+ . expect ( 200 , done )
2727+ } )
2728+ } )
2729+ } )
2730+
2731+
26232732function cookie ( res ) {
26242733 var setCookie = res . headers [ 'set-cookie' ] ;
26252734 return ( setCookie && setCookie [ 0 ] ) || undefined ;
0 commit comments