Skip to content

Commit

Permalink
Merge c9e274c into 5ca903f
Browse files Browse the repository at this point in the history
  • Loading branch information
zss007 committed May 16, 2019
2 parents 5ca903f + c9e274c commit 45a2ace
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/context.js
Expand Up @@ -202,7 +202,7 @@ class ContextSession {

create(val, externalKey) {
debug('create session with val: %j externalKey: %s', val, externalKey);
if (this.store) this.externalKey = externalKey || this.opts.genid();
if (this.store) this.externalKey = externalKey || this.opts.genid && this.opts.genid(this.ctx);
this.session = new Session(this, val);
}

Expand Down
26 changes: 24 additions & 2 deletions test/contextstore.test.js
Expand Up @@ -96,6 +96,26 @@ describe('Koa Session External Context Store', () => {
});
});

it('should pass sid to middleware', done => {
const app = App();

app.use(async function(ctx) {
ctx.session.message = 'hello';
ctx.state.sid.indexOf('_suffix').should.greaterThan(1);
ctx.body = '';
});

request(app.listen())
.get('/')
.expect('Set-Cookie', /koa:sess/)
.expect(200, (err, res) => {
if (err) return done(err);
cookie = res.header['set-cookie'].join(';');
cookie.indexOf('_suffix').should.greaterThan(1);
done();
});
});

it('should not Set-Cookie', done => {
const app = App();

Expand Down Expand Up @@ -726,8 +746,10 @@ function App(options) {
app.keys = [ 'a', 'b' ];
options = options || {};
options.ContextStore = ContextStore;
options.genid = () => {
return Date.now() + '_suffix';
options.genid = ctx => {
const sid = Date.now() + '_suffix';
ctx.state.sid = sid;
return sid;
};
app.use(session(options, app));
return app;
Expand Down

0 comments on commit 45a2ace

Please sign in to comment.