From bfca51f3ae412e6ffe36747b3ecc3e2c6a2d2fe4 Mon Sep 17 00:00:00 2001 From: Simon Nord Date: Fri, 18 Aug 2017 16:01:48 +0200 Subject: [PATCH] Support ctx.respond = false --- lib/context.js | 5 +++++ test/contextstore.test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/context.js b/lib/context.js index f1dc702..040fe59 100644 --- a/lib/context.js +++ b/lib/context.js @@ -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; diff --git a/test/contextstore.test.js b/test/contextstore.test.js index 0905fae..7dd34fd 100644 --- a/test/contextstore.test.js +++ b/test/contextstore.test.js @@ -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);