From c23bab4023b95c65be46b4eeaf089608ddaa738e Mon Sep 17 00:00:00 2001 From: dead-horse Date: Wed, 10 Jul 2019 02:00:50 +0800 Subject: [PATCH] fix: remvoe unused code --- lib/context.js | 2 -- test/store.test.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/context.js b/lib/context.js index ac3a546..c0437b4 100644 --- a/lib/context.js +++ b/lib/context.js @@ -33,8 +33,6 @@ class ContextSession { // unset if (session === false) return null; - // cookie session store - if (this.session) return this.session; // create an empty session or init from cookie this.store ? this.create() : this.initFromCookie(); return this.session; diff --git a/test/store.test.js b/test/store.test.js index 7f9587c..f2f0765 100644 --- a/test/store.test.js +++ b/test/store.test.js @@ -769,6 +769,42 @@ describe('Koa Session External Store', () => { should.not.exist(res.headers['set-cookie']); }); }); + + describe('when get session before middleware', () => { + it('should return empty session', async () => { + const app = new Koa(); + app.keys = [ 'a', 'b' ]; + const options = {}; + options.store = store; + app.use(async (ctx, next) => { + // will not take effect + ctx.session.should.be.ok(); + ctx.session.foo = '123'; + await next(); + }); + app.use(session(options, app)); + app.use(async ctx => { + if (ctx.path === '/set') ctx.session = { foo: 'bar' }; + ctx.body = ctx.session; + }); + + let res = await request(app.callback()) + .get('/') + .expect({}); + + res = await request(app.callback()) + .get('/set') + .expect({ foo: 'bar' }); + + res.headers['set-cookie'].should.have.length(2); + const cookie = res.headers['set-cookie'].join(';'); + await sleep(1200); + res = await request(app.callback()) + .get('/') + .set('cookie', cookie) + .expect({ foo: 'bar' }); + }); + }); }); function App(options) {