Skip to content
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

fix: allow multiple body parser coexist #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 3 additions & 5 deletions lib/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ module.exports = function(req, opts){
opts.qs = opts.qs || qs;

// raw-body returns a Promise when no callback is specified
return Promise.resolve()
.then(function() {
return raw(inflate(req), opts);
})
.then(function(str){
return new Promise(function (resolve, reject){
resolve(raw(inflate(req), opts));
}).then(function(str){
try {
var parsed = opts.qs.parse(str, queryString);
return opts.returnRawBody ? { parsed: parsed, raw: str } : parsed;
Expand Down
8 changes: 3 additions & 5 deletions lib/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ module.exports = function(req, opts){
var strict = opts.strict !== false;

// raw-body returns a promise when no callback is specified
return Promise.resolve()
.then(function() {
return raw(inflate(req), opts);
})
.then(function(str) {
return new Promise(function (resolve, reject){
resolve(raw(inflate(req), opts));
}).then(function(str) {
try {
var parsed = parse(str);
return opts.returnRawBody ? { parsed: parsed, raw: str } : parsed;
Expand Down
8 changes: 3 additions & 5 deletions lib/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ module.exports = function(req, opts){
opts.limit = opts.limit || '1mb';

// raw-body returns a Promise when no callback is specified
return Promise.resolve()
.then(function() {
return raw(inflate(req), opts);
})
.then(str => {
return new Promise(function (resolve, reject){
resolve(raw(inflate(req), opts));
}).then(str => {
// ensure return the same format with json / form
return opts.returnRawBody ? { parsed: str, raw: str } : str;
});
Expand Down
43 changes: 43 additions & 0 deletions test/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,47 @@ describe('parse(req, opts)', function(){
})
})

describe('multiple parser coexist', function(){
var app = koa();

app.use(function *(next){
this.request.bodyParser = parse(this);
yield next;
});
app.use(function *(next){
this.body = yield parse(this);
yield next;
});
app.use(function *(next){
this.body = [this.body, yield this.request.bodyParser];
yield next;
});

it('should parse json', function(done){
request(app.listen())
.post('/')
.set('content-type', 'application/json')
.send({foo:'bar'})
.expect(200)
.expect([{foo:'bar'}, {foo:'bar'}], done);
})

it('should parse text', function(done){
request(app.listen())
.post('/')
.set('content-type', 'text/plain')
.send('plain text')
.expect(200)
.expect(['plain text', 'plain text'], done);
})

it('should parse form', function(done){
request(app.listen())
.post('/')
.type('form')
.send({foo: 'bar'})
.expect(200)
.expect([{foo: 'bar'}, {foo: 'bar'}], done);
})
})
})