Skip to content

Commit

Permalink
Merge b5df373 into 2497dc5
Browse files Browse the repository at this point in the history
  • Loading branch information
galvez committed Aug 28, 2018
2 parents 2497dc5 + b5df373 commit 6d11f1f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Readme.md
Expand Up @@ -53,6 +53,7 @@ const CONFIG = {
/** 'session' will result in a cookie that expires when session/browser is closed */
/** Warning: If a session cookie is stolen, this cookie will never expire */
maxAge: 86400000,
autoCommit: true, /** (boolean) automatically commit headers (default true) */
overwrite: true, /** (boolean) can overwrite or not (default true) */
httpOnly: true, /** (boolean) httpOnly or not (default true) */
signed: true, /** (boolean) signed or not (default true) */
Expand Down Expand Up @@ -150,6 +151,10 @@ if (this.session.isNew) {

Save this session no matter whether it is populated.

### Session#commitNow()

Session headers are auto committed by default. Use this if `autoCommit` is set to `false`.

### Destroying a session

To destroy a session simply set it to `null`:
Expand Down
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -42,7 +42,9 @@ module.exports = function(opts, app) {
} catch (err) {
throw err;
} finally {
await sess.commit();
if (opts.autoCommit) {
await sess.commit();
}
}
};
};
Expand All @@ -67,6 +69,7 @@ function formatOpts(opts) {
if (opts.overwrite == null) opts.overwrite = true;
if (opts.httpOnly == null) opts.httpOnly = true;
if (opts.signed == null) opts.signed = true;
if (opts.autoCommit == null) opts.autoCommit = true;

debug('session options %j', opts);

Expand Down Expand Up @@ -120,6 +123,9 @@ function extendContext(context, opts) {
},
},
session: {
commitNow() {
this[CONTEXT_SESSION].commit();
},
get() {
return this[CONTEXT_SESSION].get();
},
Expand Down
49 changes: 49 additions & 0 deletions test/contextstore.test.js
Expand Up @@ -356,6 +356,55 @@ describe('Koa Session External Context Store', () => {
});
});

describe('when autoCommit is present', () => {
describe('and set to false', () => {
it('should not set headers if commitNow() isn\'t called', done => {
const app = App({ autoCommit: false });
app.use(async function(ctx) {
if (ctx.method === 'POST') {
ctx.session.message = 'hi';
ctx.body = 200;
return;
}
ctx.body = ctx.session.message;
});
const server = app.listen();

request(server)
.post('/')
.end((err, res) => {
if (err) return done(err);
const cookie = res.headers['set-cookie'];
should.not.exists(cookie);
})
.expect(200, done);
});
});
describe('and set to false', () => {
it('should set headers if commitNow() is called', done => {
const app = App({ autoCommit: false });
app.use(async function(ctx) {
if (ctx.method === 'POST') {
ctx.session.message = 'hi';
await ctx.session.commitNow();
ctx.body = 200;
return;
}
ctx.body = ctx.session.message;
});
const server = app.listen();

request(server)
.post('/')
.expect('hi')
.end(err => {
if (err) return done(err);
})
.expect(200, done);
});
});
});

describe('when maxAge present', () => {
describe('and set to be a session cookie', () => {
it('should not expire the session', done => {
Expand Down

0 comments on commit 6d11f1f

Please sign in to comment.