Skip to content

Commit

Permalink
fix: remvoe unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Jul 9, 2019
1 parent bdf51c3 commit c23bab4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 0 additions & 2 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions test/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c23bab4

Please sign in to comment.