File tree 2 files changed +36
-2
lines changed
2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -33,8 +33,6 @@ class ContextSession {
33
33
// unset
34
34
if ( session === false ) return null ;
35
35
36
- // cookie session store
37
- if ( this . session ) return this . session ;
38
36
// create an empty session or init from cookie
39
37
this . store ? this . create ( ) : this . initFromCookie ( ) ;
40
38
return this . session ;
Original file line number Diff line number Diff line change @@ -769,6 +769,42 @@ describe('Koa Session External Store', () => {
769
769
should . not . exist ( res . headers [ 'set-cookie' ] ) ;
770
770
} ) ;
771
771
} ) ;
772
+
773
+ describe ( 'when get session before middleware' , ( ) => {
774
+ it ( 'should return empty session' , async ( ) => {
775
+ const app = new Koa ( ) ;
776
+ app . keys = [ 'a' , 'b' ] ;
777
+ const options = { } ;
778
+ options . store = store ;
779
+ app . use ( async ( ctx , next ) => {
780
+ // will not take effect
781
+ ctx . session . should . be . ok ( ) ;
782
+ ctx . session . foo = '123' ;
783
+ await next ( ) ;
784
+ } ) ;
785
+ app . use ( session ( options , app ) ) ;
786
+ app . use ( async ctx => {
787
+ if ( ctx . path === '/set' ) ctx . session = { foo : 'bar' } ;
788
+ ctx . body = ctx . session ;
789
+ } ) ;
790
+
791
+ let res = await request ( app . callback ( ) )
792
+ . get ( '/' )
793
+ . expect ( { } ) ;
794
+
795
+ res = await request ( app . callback ( ) )
796
+ . get ( '/set' )
797
+ . expect ( { foo : 'bar' } ) ;
798
+
799
+ res . headers [ 'set-cookie' ] . should . have . length ( 2 ) ;
800
+ const cookie = res . headers [ 'set-cookie' ] . join ( ';' ) ;
801
+ await sleep ( 1200 ) ;
802
+ res = await request ( app . callback ( ) )
803
+ . get ( '/' )
804
+ . set ( 'cookie' , cookie )
805
+ . expect ( { foo : 'bar' } ) ;
806
+ } ) ;
807
+ } ) ;
772
808
} ) ;
773
809
774
810
function App ( options ) {
You can’t perform that action at this time.
0 commit comments