Skip to content

Commit

Permalink
Merge 085b478 into 2497dc5
Browse files Browse the repository at this point in the history
  • Loading branch information
galvez committed Aug 28, 2018
2 parents 2497dc5 + 085b478 commit a88138e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 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
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: {
commit() {
this[CONTEXT_SESSION].commit();
},
get() {
return this[CONTEXT_SESSION].get();
},
Expand Down
26 changes: 26 additions & 0 deletions test/contextstore.test.js
Expand Up @@ -356,6 +356,32 @@ describe('Koa Session External Context Store', () => {
});
});

describe('when autoCommit is present', () => {
describe('and set to false', () => {
it('should not set headers if commit() 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('when maxAge present', () => {
describe('and set to be a session cookie', () => {
it('should not expire the session', done => {
Expand Down

0 comments on commit a88138e

Please sign in to comment.