8000 refactor: upgrade koa-session, support egg2. [BREAKING CHANGE] by dead-horse · Pull Request #10 · eggjs/session · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: upgrade koa-session, support egg2. [BREAKING CHANGE] #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 9, 2017
Merged
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
1 change: 0 additions & 1 deletion .autod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module.exports = {
'benchmark',
],
devdep: [
'egg',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

干掉后 autod 会被干掉依赖吧?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不会,dev 不会去除,因为现在依赖的是 next tag,所以就没必要在这里写了

'egg-ci',
'egg-bin',
'autod',
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sudo: false
language: node_js
node_js:
- '6'
- '7'
- '8'
- '9'
install:
- npm i npminstall && npminstall
script:
Expand Down
3 changes: 3 additions & 0 deletions app/extend/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ module.exports = {
assert(typeof store.get === 'function', 'store.get must be function');
assert(typeof store.set === 'function', 'store.set must be function');
assert(typeof store.destroy === 'function', 'store.destroy must be function');
store.get = this.toAsyncFunction(store.get);
store.set = this.toAsyncFunction(store.set);
store.destroy = this.toAsyncFunction(store.destroy);
this.config.session.store = store;
},

Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@
"cookie"
],
"dependencies": {
"koa-session": "^4.0.0"
"koa-session": "^5.5.0"
},
"devDependencies": {
"autod": "^2.7.1",
"egg": "^1.0.0-rc.1",
"egg-bin": "^2.2.3",
"egg-ci": "^1.2.0",
"egg-mock": "^3.0.1",
"autod": "^2.10.1",
"egg": "next",
"egg-bin": "^4.3.5",
"egg-ci": "^1.8.0",
"egg-mock": "^3.13.1",
"egg-redis": "^1.0.1",
"eslint": "^3.16.1",
"eslint-config-egg": "^3.2.0",
"mz-modules": "^1.0.0",
"eslint": "^4.10.0",
"eslint-config-egg": "^5.1.1",
"mz-modules": "^2.0.0",
"supertest": "^3.0.0"
},
"engines": {
"node": ">=6.0.0"
"node": ">=8.0.0"
},
"scripts": {
"lint": "eslint .",
Expand All @@ -48,7 +48,7 @@
"autod": "autod"
},
"ci": {
"version": "6, 7",
"version": "8, 9",
"services": "redis-server",
"type": "travis"
},
Expand Down
123 changes: 62 additions & 61 deletions test/app/middleware/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,41 @@ describe('test/app/middlewares/session.test.js', () => {
it('should get sessionStore', function* () {
mm.empty(app.sessionStore, 'set');
yield agent
.get('/set?foo=bar')
.expect(200)
.expect({ foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);
.get('/set?foo=bar')
.expect(200)
.expect({ foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);

yield agent.get('/get')
.expect(200)
.expect({});
.expect(200)
.expect({});
});

it('should session store can be change', function* () {
mm(app.config, 'env', 'local');

yield agent
.get('/set?foo=bar')
.expect(200)
.expect({ foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);
.get('/set?foo=bar')
.expect(200)
.expect({ foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);

yield agent.get('/get')
.expect(200)
.expect({ foo: 'bar' });
.expect(200)
.expect({ foo: 'bar' });

app.sessionStore = null;

yield agent.get('/get')
.expect(200)
.expect({});
.expect(200)
.expect({});
});
});

[
'cookie-session',
'memory-session',
'memory-session-generator',
'redis-session',
].forEach(name => {
describe(name, () => {
Expand All @@ -75,85 +76,85 @@ describe('test/app/middlewares/session.test.js', () => {

it('should get empty session and do not set cookie when session not populated', function* () {
yield agent
.get('/get')
.expect(200)
.expect({})
.expect(res => {
assert(!res.header['set-cookie'].join('').match(/EGG_SESS/));
});
.get('/get')
.expect(200)
.expect({})
.expect(res => {
assert(!res.header['set-cookie'].join('').match(/EGG_SESS/));
});
});

it('should ctx.session= change the session', function* () {
yield agent
.get('/set?foo=bar')
.expect(200)
.expect({ foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);
.get('/set?foo=bar')
.expect(200)
.expect({ foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);
});

it('should ctx.session.key= change the session', function* () {
yield agent
.get('/set?key=foo&foo=bar')
.expect(200)
.expect({ key: 'foo', foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);
.get('/set?key=foo&foo=bar')
.expect(200)
.expect({ key: 'foo', foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);

yield agent
.get('/setKey?key=bar')
.expect(200)
.expect({ key: 'bar', foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);
.get('/setKey?key=bar')
.expect(200)
.expect({ key: 'bar', foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);
});

it('should ctx.session=null remove the session', function* () {
yield agent
.get('/set?key=foo&foo=bar')
.expect(200)
.expect({ key: 'foo', foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);
.get('/set?key=foo&foo=bar')
.expect(200)
.expect({ key: 'foo', foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);

yield agent
.get('/remove')
.expect(204)
.expect('set-cookie', /EGG_SESS=;/);
.get('/remove')
.expect(204)
.expect('set-cookie', /EGG_SESS=;/);

yield agent
.get('/get')
.expect(200)
.expect({});
.get('/get')
.expect(200)
.expect({});
});

it('should ctx.session.maxAge= change maxAge', function* () {
yield agent
.get('/set?key=foo&foo=bar')
.expect(200)
.expect({ key: 'foo', foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);
.get('/set?key=foo&foo=bar')
.expect(200)
.expect({ key: 'foo', foo: 'bar' })
.expect('set-cookie', /EGG_SESS=.*?;/);

let cookie;

yield agent
.get('/maxAge?maxAge=100')
.expect(200)
.expect({ key: 'foo', foo: 'bar' })
.expect(res => {
cookie = res.headers['set-cookie'].join(';');
assert(cookie.match(/EGG_SESS=.*?;/));
assert(cookie.match(/expires=/));
});
.get('/maxAge?maxAge=100')
.expect(200)
.expect({ key: 'foo', foo: 'bar' })
.expect(res => {
cookie = res.headers['set-cookie'].join(';');
assert(cookie.match(/EGG_SESS=.*?;/));
assert(cookie.match(/expires=/));
});

yield sleep(200);

yield agent
.get('/get')
.expect(200)
.expect({});
.get('/get')
.expect(200)
.expect({});

yield request(app.callback())
.get('/get')
.set('cookie', cookie)
.expect(200)
.expect({});
.get('/get')
.set('cookie', cookie)
.expect(200)
.expect({});
});
});
});
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/memory-session-generator/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const sessions = {};

module.exports = app => {
app.sessionStore = {
* get(key) {
return sessions[key];
},

* set(key, value) {
sessions[key] = value;
},

* destroy(key) {
sessions[key] = undefined;
},
};
};
25 changes: 25 additions & 0 deletions test/fixtures/memory-session-generator/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

exports.get = function* (ctx) {
ctx.body = ctx.session;
};

exports.set = function* (ctx) {
ctx.session = ctx.query;
ctx.body = ctx.session;
};

exports.setKey = function* (ctx) {
ctx.session.key = ctx.query.key;
ctx.body = ctx.session;
};

exports.remove = function* (ctx) {
ctx.session = null;
ctx.body = ctx.session;
};

exports.maxAge = function* (ctx) {
ctx.session.maxAge = Number(this.query.maxAge);
ctx.body = ctx.session;
};
9 changes: 9 additions & 0 deletions test/fixtures/memory-session-generator/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function(app) {
app.get('/get', 'home.get');
app.get('/set', 'home.set');
app.get('/setKey', 'home.setKey');
app.get('/remove', 'home.remove');
app.get('/maxAge', 'home.maxAge');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.keys = 'keys';
3 changes: 3 additions & 0 deletions test/fixtures/memory-session-generator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "memory-session-generator"
}
6 changes: 3 additions & 3 deletions test/fixtures/memory-session/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const sessions = {};

module.exports = app => {
app.sessionStore = {
* get(key) {
async get(key) {
return sessions[key];
},

* set(key, value) {
async set(key, value) {
sessions[key] = value;
},

* destroy(key) {
async destroy(key) {
sessions[key] = undefined;
},
};
Expand Down
12 changes: 6 additions & 6 deletions test/fixtures/memory-session/app/controller/home.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
'use strict';

exports.get = function* (ctx) {
exports.get = async ctx => {
ctx.body = ctx.session;
};

exports.set = function* (ctx) {
exports.set = async ctx => {
ctx.session = ctx.query;
ctx.body = ctx.session;
};

exports.setKey = function* (ctx) {
exports.setKey = async ctx => {
ctx.session.key = ctx.query.key;
ctx.body = ctx.session;
};

exports.remove = function* (ctx) {
exports.remove = async ctx => {
ctx.session = null;
ctx.body = ctx.session;
};

exports.maxAge = function* (ctx) {
ctx.session.maxAge = Number(this.query.maxAge);
exports.maxAge = async ctx => {
ctx.session.maxAge = Number(ctx.query.maxAge);
ctx.body = ctx.session;
};
0