Skip to content

Commit

Permalink
Allow double parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
thomseddon committed Jun 10, 2014
1 parent 91bfa9e commit ec44a76
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function (opts) {
var length = 'content-length' in this.req.headers &&
this.req.headers['content-length'] !== 0;

if (encoding || length) {
if ((encoding || length) && !this.req._readableState.ended) {
try {
this.request.body = yield parse(this.request, opts);
} catch (err) {
Expand Down
20 changes: 19 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,25 @@ describe('koa-body', function () {
.expect(200, done);
});

describe('empty body', function () {
it('should allow body to be parsed twice', function (done) {
var app = koa();

app.use(parseBody());
app.use(parseBody());

app.use(function *() {
this.request.body.should.eql({ hello: 'world' });;
this.status = 200;
});

request(app.listen())
.post('/')
.set('Content-Type', 'application/json')
.send({ hello: 'world' })
.expect(200, done);
});

describe('with an empty body', function () {
it('should not throw when GET', function (done) {
var app = koa();

Expand Down

0 comments on commit ec44a76

Please sign in to comment.