Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ class ContextSession {
const opts = this.opts;
const ctx = this.ctx;

if (ctx.headerSent) {
debug('skipping commit due to ctx.headersSent');
return;
}

// not accessed
if (undefined === session) return;

Expand Down
26 changes: 26 additions & 0 deletions test/contextstore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,32 @@ describe('Koa Session External Context Store', () => {
});
});

describe('when headers are sent', () => {
it('should not commit', done => {
const app = App();
let appError;
app.on('error', err => {
appError = err;
});

app.use(async function(ctx) {
ctx.respond = false;
ctx.response.status = 200;
ctx.session.makeChange = true;

ctx.response.flushHeaders();
ctx.res.end();
});

request(app.listen())
.get('/')
.expect(200, err => {
done(err || appError);
});
});
});


describe('ctx.session', () => {
after(mm.restore);

Expand Down