Skip to content

Commit

Permalink
Merge pull request #1 from yhanwen/master
Browse files Browse the repository at this point in the history
fix: fresh session not save to redis
  • Loading branch information
welefen committed Dec 18, 2017
2 parents d80f250 + 9c83429 commit 663b6c7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -31,6 +31,8 @@ class RedisSession {
if (this.initPromise) {
return this.initPromise;
}

this[autoSave]();
if (this.options.fresh || this.status === -1) {
this.initPromise = Promise.resolve();
return this.initPromise;
Expand All @@ -40,7 +42,6 @@ class RedisSession {
if (helper.isEmpty(content)) return;
this.data = content;
}).catch(err => debug(err));
this[autoSave]();
return this.initPromise;
}
/**
Expand Down
29 changes: 17 additions & 12 deletions test/index.js
Expand Up @@ -10,7 +10,7 @@ function mockRequire() {
}
function mockDebug(param = []) {
const debug1 = function(str) {

return function(err) {
param.push(err);
}
Expand All @@ -32,7 +32,7 @@ function mockIORedis() {
set(key, val, type, expire) {
return new Promise((resolve, reject) => {
redisData[key] = val;

if(expire) {
if(this.expireTimeout) {
clearTimeout(this.expireTimeout);
Expand All @@ -41,7 +41,7 @@ function mockIORedis() {
this.expireTimeout = null;
this.del(key);
}, expire);
}
}
resolve('OK');
});
}
Expand Down Expand Up @@ -125,7 +125,7 @@ test.serial('2. set and get session data with short maxAge', t => {
'abc': '123'
});
await sleep(100);

const redisSession1 = new RedisSession(options, ctx);
const data1 = await redisSession1.get('abc');
t.is(data1, undefined);
Expand All @@ -146,6 +146,7 @@ test.serial('3. set and get session data with fresh param', t => {
once: function(status, fn) {
setTimeout(function() {
fn();
resolve();
}, 10);
}
}
Expand All @@ -157,7 +158,11 @@ test.serial('3. set and get session data with fresh param', t => {
'abc': '123'
});

resolve();
setTimeout(() => {
t.fail('new session value not set to redis');
reject();
}, 20);

});
});
test.serial('4. set and get session data when JSON.parse(content) returns error', t => {
Expand All @@ -184,21 +189,21 @@ test.serial('4. set and get session data when JSON.parse(content) returns error'
const redisSession = new RedisSession(options, ctx);
await redisSession.set('abc', '123');
await sleep(100);

redisData[cookieName] = 'zz';
const redisSession1 = new RedisSession(options, ctx);
await redisSession1.set('abc', '123');

t.is(debugParam.length, 1);
resolve();
});
});

test.serial('5. set and get session data when options is undefined', t => {
return new Promise(async (resolve, reject) => {

const RedisSession = mockRequire();

const ctx = {
res: {
once: function(status, fn) {
Expand All @@ -213,7 +218,7 @@ test.serial('5. set and get session data when options is undefined', t => {
const redisSession = new RedisSession(undefined, ctx);
});
t.is(error.message, '.cookie required');

resolve();
});
});
});

0 comments on commit 663b6c7

Please sign in to comment.