Skip to content

Commit

Permalink
refactor: use async function and support egg@2 (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Nov 11, 2017
1 parent 705ffcb commit a9cadba
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 53 deletions.
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',
'egg-ci',
'egg-bin',
'autod',
Expand Down
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
sudo: false
language: node_js
node_js:
- '6'
- '7'
- '8'
- '9'
install:
- npm i npminstall && npminstall
script:
Expand Down
12 changes: 6 additions & 6 deletions app/extend/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ module.exports = {
throw err;
}

return function* jsonp(next) {
const jsonpFunction = getJsonpFunction(this.query, options.callback);
return async function jsonp(ctx, next) {
const jsonpFunction = getJsonpFunction(ctx.query, options.callback);

this[JSONP_CONFIG] = {
ctx[JSONP_CONFIG] = {
jsonpFunction,
options,
};

// before handle request, must do some security checks
securityAssert(this);
securityAssert(ctx);

yield next;
await next();

// generate jsonp body
this.createJsonpBody(this.body);
ctx.createJsonpBody(ctx.body);
};
},
};
Expand Down
3 changes: 1 addition & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
environment:
matrix:
- nodejs_version: '6'
- nodejs_version: '7'
- nodejs_version: '8'
- nodejs_version: '9'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
"jsonp-body": "^1.0.0"
},
"devDependencies": {
"autod": "^2.9.0",
"egg": "^1.8.0",
"egg-bin": "^4.3.2",
"autod": "^2.10.1",
"egg": "next",
"egg-bin": "^4.3.5",
"egg-ci": "^1.8.0",
"egg-mock": "^3.12.1",
"eslint": "^4.7.1",
"egg-mock": "^3.13.1",
"eslint": "^4.10.0",
"eslint-config-egg": "^5.1.1",
"supertest": "^3.0.0",
"webstorm-disable-index": "^1.2.0"
},
"engines": {
"node": ">=6.0.0"
"node": ">=8.0.0"
},
"scripts": {
"test": "npm run lint -- --fix && npm run test-local",
Expand All @@ -46,7 +46,7 @@
"autod": "autod"
},
"ci": {
"version": "6, 7, 8"
"version": "8, 9"
},
"repository": {
"type": "git",
Expand Down
12 changes: 6 additions & 6 deletions test/fixtures/jsonp-test/app/controller/jsonp.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict';

exports.index = function*() {
this.body = { foo: 'bar' };
exports.index = ctx => {
ctx.body = { foo: 'bar' };
};

exports.empty = function*() {};


exports.mark = function*() {
this.body = { jsonpFunction: this.acceptJSONP };
exports.mark = ctx => {
ctx.body = { jsonpFunction: ctx.acceptJSONP };
};

exports.error = function*() {
exports.error = async () => {
throw new Error('jsonpFunction is error');
};
};
57 changes: 28 additions & 29 deletions test/jsonp.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const request = require('supertest');
const mm = require('egg-mock');

describe('test/jsonp.test.js', () => {
Expand All @@ -16,55 +15,55 @@ describe('test/jsonp.test.js', () => {
afterEach(mm.restore);

it('should support json', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/default')
.expect(200)
.expect({ foo: 'bar' });
});

it('should support jsonp', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/default?callback=fn')
.expect(200)
.expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});');
});

it('should support _callback', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/default?_callback=fn')
.expect(200)
.expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});');
});

it('should support jsonp if response is empty', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/empty?callback=fn')
.expect(200)
.expect('/**/ typeof fn === \'function\' && fn(null);');
});

it('should not support jsonp if not use jsonp middleware', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/disable?_callback=fn')
.expect(200)
.expect({ foo: 'bar' });
});

it('should not support cutom callback name', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/fn?fn=fn')
.expect(200)
.expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});');
});

it('should not pass csrf', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/csrf')
.expect(403);
});

it('should pass csrf with cookie', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/csrf')
.set('cookie', 'csrfToken=token;')
.set('x-csrf-token', 'token')
Expand All @@ -73,7 +72,7 @@ describe('test/jsonp.test.js', () => {
});

it('should pass csrf with cookie and support jsonp', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/csrf')
.set('cookie', 'csrfToken=token;')
.set('x-csrf-token', 'token')
Expand All @@ -82,85 +81,85 @@ describe('test/jsonp.test.js', () => {
});

it('should pass referrer white list check with subdomain', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/referrer/subdomain')
.set('referrer', 'http://test.com/')
.expect(200)
.expect({ foo: 'bar' });

yield request(app.callback())
yield app.httpRequest()
.get('/referrer/subdomain')
.set('referrer', 'http://sub.test.com/')
.expect(200)
.expect({ foo: 'bar' });

yield request(app.callback())
yield app.httpRequest()
.get('/referrer/subdomain')
.set('referrer', 'https://sub.sub.test.com/')
.expect(200)
.expect({ foo: 'bar' });

yield request(app.callback())
yield app.httpRequest()
.get('/referrer/subdomain')
.set('referrer', 'https://sub.sub.test1.com/')
.expect(403)
.expect(/jsonp request security validate failed/);
});

it('should pass referrer white list with domain', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/referrer/equal')
.set('referrer', 'http://test.com/')
.expect(200)
.expect({ foo: 'bar' });

yield request(app.callback())
yield app.httpRequest()
.get('/referrer/equal')
.set('referrer', 'https://test.com/')
.expect(200)
.expect({ foo: 'bar' });

yield request(app.callback())
yield app.httpRequest()
.get('/referrer/equal')
.set('referrer', 'https://sub.sub.test.com/')
.expect(403)
.expect(/jsonp request security validate failed/);

yield request(app.callback())
yield app.httpRequest()
.get('/referrer/equal')
.set('referrer', 'https://sub.sub.test1.com/')
.expect(403)
.expect(/jsonp request security validate failed/);
});

it('should pass referrer white array and regexp', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/referrer/regexp')
.set('referrer', 'http://test.com/')
.expect(200)
.expect({ foo: 'bar' });

yield request(app.callback())
yield app.httpRequest()
.get('/referrer/regexp')
.set('referrer', 'https://foo.com/')
.expect(200)
.expect({ foo: 'bar' });

yield request(app.callback())
yield app.httpRequest()
.get('/referrer/regexp')
.set('referrer', 'https://sub.sub.test.com/')
.expect(403)
.expect(/jsonp request security validate failed/);

yield request(app.callback())
yield app.httpRequest()
.get('/referrer/regexp')
.set('referrer', 'https://sub.sub.test1.com/')
.expect(403)
.expect(/jsonp request security validate failed/);
});

it('should pass when pass csrf but not hit referrer white list', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/both')
.set('cookie', 'csrfToken=token;')
.set('x-csrf-token', 'token')
Expand All @@ -169,44 +168,44 @@ describe('test/jsonp.test.js', () => {
});

it('should pass when not pass csrf but hit referrer white list', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/both')
.set('referrer', 'https://test.com/')
.expect(200)
.expect({ foo: 'bar' });
});

it('should 403 when not pass csrf and not hit referrer white list', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/both')
.expect(403)
.expect(/jsonp request security validate failed/);
});

it('should 403 when not pass csrf and referrer illegal', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/both')
.set('referrer', '/hello')
.expect(403)
.expect(/jsonp request security validate failed/);
});

it('should pass and return is a jsonp function', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/mark?_callback=fn')
.expect(200)
.expect('/**/ typeof fn === \'function\' && fn({"jsonpFunction":true});');
});

it('should pass and return is not a jsonp function', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/mark')
.expect(200)
.expect({ jsonpFunction: false });
});

it('should pass and return error message', function* () {
yield request(app.callback())
yield app.httpRequest()
.get('/error?_callback=fn')
.expect(200)
.expect('/**/ typeof fn === \'function\' && fn({"msg":"jsonpFunction is error"});');
Expand Down

0 comments on commit a9cadba

Please sign in to comment.