Skip to content

Commit

Permalink
feat: body parser support disable, ignore and match (eggjs#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored and popomore committed Nov 17, 2016
1 parent c736ac9 commit 1082d6d
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 1 deletion.
14 changes: 13 additions & 1 deletion app/middleware/body_parser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
'use strict';

module.exports = require('koa-bodyparser');
const bodyParser = require('koa-bodyparser');
const pathMatching = require('egg-path-matching');

module.exports = function(options) {
const bodyParserMiddleware = bodyParser(options);
const match = pathMatching(options);
return function* (next) {
if (!options.enable) return yield next;
if (!match(this)) return yield next;

return yield bodyParserMiddleware.call(this, next);
};
};
4 changes: 4 additions & 0 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ module.exports = appInfo => {
/**
* bodyParser options
* @member Config#bodyParser
* @property {Boolean} enable - enable bodyParser or not, default to true
* @property {String | RegExp | Function | Array} ignore - won't parse request body when url path hit ignore pattern, can not set `ignore` when `match` presented
* @property {String | RegExp | Function | Array} match - will parse request body only when url path hit match pattern
* @property {String} encoding - body 的编码格式,默认为 utf8
* @property {String} formLimit - form body 的大小限制,默认为 100kb
* @property {String} jsonLimit - json body 的大小限制,默认为 100kb
Expand All @@ -114,6 +117,7 @@ module.exports = appInfo => {
* @property {Number} queryString.parameterLimit - 参数个数限制,默认 1000
*/
exports.bodyParser = {
enable: true,
encoding: 'utf8',
formLimit: '100kb',
jsonLimit: '100kb',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"egg-logrotator": "^2.2.2",
"egg-multipart": "^1.0.0",
"egg-onerror": "^1.1.0",
"egg-path-matching": "^1.0.0",
"egg-rest": "^1.1.0",
"egg-schedule": "^2.2.1",
"egg-security": "^1.2.1",
Expand Down
39 changes: 39 additions & 0 deletions test/app/middleware/body_parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,43 @@ describe('test/app/middleware/body_parser.test.js', () => {
.expect({ foo: 'bar', _csrf: csrf })
.expect(200, done);
});

it('should disable body parser', function* () {
const app = utils.app('apps/body_parser_testapp_disable');
yield request(app.callback())
.post('/test/body_parser/foo.json')
.send({ foo: 'bar' })
.expect(204);
app.close();
});

it('should body parser support ignore', function* () {
const app = utils.app('apps/body_parser_testapp_ignore');
yield request(app.callback())
.post('/test/body_parser/foo.json')
.send({ foo: 'bar' })
.expect(204);

yield request(app.callback())
.post('/test/body_parser/form.json')
.send({ foo: 'bar' })
.expect({ foo: 'bar' });

app.close();
});

it('should body parser support match', function* () {
const app = utils.app('apps/body_parser_testapp_match');
yield request(app.callback())
.post('/test/body_parser/foo.json')
.send({ foo: 'bar' })
.expect({ foo: 'bar' });

yield request(app.callback())
.post('/test/body_parser/form.json')
.send({ foo: 'bar' })
.expect(204);

app.close();
});
});
19 changes: 19 additions & 0 deletions test/fixtures/apps/body_parser_testapp_disable/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = app => {
app.get('/test/body_parser/user', function* () {
this.body = {
url: this.url,
csrf: this.csrf
};
});

app.post('/test/body_parser/user', function* () {
this.body = this.request.body;
});

app.post('/test/body_parser/foo.json', function* () {
this.body = this.request.body;
});
app.post('/test/body_parser/form.json', function* () {
this.body = this.request.body;
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports.bodyParser = {
enable: false,
};

exports.security = {
csrf: false,
};

exports.keys = 'foo';
3 changes: 3 additions & 0 deletions test/fixtures/apps/body_parser_testapp_disable/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "body_parser_testapp_disable"
}
19 changes: 19 additions & 0 deletions test/fixtures/apps/body_parser_testapp_ignore/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = app => {
app.get('/test/body_parser/user', function* () {
this.body = {
url: this.url,
csrf: this.csrf
};
});

app.post('/test/body_parser/user', function* () {
this.body = this.request.body;
});

app.post('/test/body_parser/foo.json', function* () {
this.body = this.request.body;
});
app.post('/test/body_parser/form.json', function* () {
this.body = this.request.body;
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports.bodyParser = {
ignore: '/test/body_parser/foo.json',
};

exports.security = {
csrf: false,
};

exports.keys = 'foo';
3 changes: 3 additions & 0 deletions test/fixtures/apps/body_parser_testapp_ignore/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "body_parser_testapp_ignore"
}
19 changes: 19 additions & 0 deletions test/fixtures/apps/body_parser_testapp_match/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = app => {
app.get('/test/body_parser/user', function* () {
this.body = {
url: this.url,
csrf: this.csrf
};
});

app.post('/test/body_parser/user', function* () {
this.body = this.request.body;
});

app.post('/test/body_parser/foo.json', function* () {
this.body = this.request.body;
});
app.post('/test/body_parser/form.json', function* () {
this.body = this.request.body;
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports.bodyParser = {
match: '/test/body_parser/foo.json',
};

exports.security = {
csrf: false,
};

exports.keys = 'foo';
3 changes: 3 additions & 0 deletions test/fixtures/apps/body_parser_testapp_match/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "body_parser_testapp_match"
}

0 comments on commit 1082d6d

Please sign in to comment.