Skip to content

Commit

Permalink
fixed parse error since formidable emits "done"
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Nov 17, 2011
1 parent 1ebbc93 commit 7434653
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/middleware/bodyParser.js
Expand Up @@ -131,7 +131,8 @@ exports.parse['application/json'] = function(req, options, fn){
exports.parse['multipart/form-data'] = function(req, options, fn){
var form = new formidable.IncomingForm
, query = []
, files = {};
, files = {}
, done;

Object.keys(options).forEach(function(key){
form[key] = options[key];
Expand All @@ -151,9 +152,13 @@ exports.parse['multipart/form-data'] = function(req, options, fn){
}
});

form.on('error', fn);
form.on('error', function(err){
fn(err);
done = true;
});

form.on('end', function(){
if (done) return;
try {
query = query.join('&');
query = qs.parse(query);
Expand Down
32 changes: 32 additions & 0 deletions test/bodyParser.js
Expand Up @@ -190,6 +190,38 @@ describe('connect.bodyParser()', function(){
done();
});
})

it('should next(err) on multipart failure', function(done){
var app = connect();

app.use(connect.bodyParser());

app.use(function(req, res){
res.end('whoop');
});

app.use(function(err, req, res, next){
err.message.should.equal('parser error, 16 of 28 bytes parsed');
res.statusCode = 500;
res.end();
});

app.request()
.post('/')
.set('Content-Type', 'multipart/form-data; boundary=foo')
.write('--foo\r\n')
.write('Content-filename="foo.txt"\r\n')
.write('\r\n')
.write('some text here')
.write('Content-Disposition: form-data; name="text"; filename="bar.txt"\r\n')
.write('\r\n')
.write('some more text stuff')
.write('\r\n--foo--')
.end(function(res){
res.statusCode.should.equal(500);
done();
});
})

})
})

0 comments on commit 7434653

Please sign in to comment.