diff --git a/index.js b/index.js index d7efeab9..e1a2118a 100644 --- a/index.js +++ b/index.js @@ -444,6 +444,10 @@ function session(options) { return false; } + if (savedHash) { + return !isSaved(req.session); + } + return !saveUninitializedSession && cookieId !== req.sessionID ? isModified(req.session) : !isSaved(req.session) diff --git a/test/session.js b/test/session.js index 7ce3c194..835e4763 100644 --- a/test/session.js +++ b/test/session.js @@ -1775,6 +1775,29 @@ describe('session()', function(){ .expect(200, 'saved', done) }) }) + + it('should prevent end-of-request save when saveUninitialized option is set to false', function (done) { + var store = new session.MemoryStore() + var server = createServer({ store: store, saveUninitialized: false }, function (req, res) { + req.session.hit = true + req.session.save(function (err) { + if (err) return res.end(err.message) + res.end('saved') + }) + }) + + request(server) + .get('/') + .expect(shouldSetSessionInStore(store)) + .expect(200, 'saved', function (err, res) { + if (err) return done(err) + request(server) + .get('/') + .set('Cookie', cookie(res)) + .expect(shouldSetSessionInStore(store)) + .expect(200, 'saved', done) + }) + }) }) describe('.touch()', function () {