diff --git a/lib/middleware/bodyParser.js b/lib/middleware/bodyParser.js index f7ef37a29..2524010cc 100644 --- a/lib/middleware/bodyParser.js +++ b/lib/middleware/bodyParser.js @@ -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]; @@ -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); diff --git a/test/bodyParser.js b/test/bodyParser.js index a32cbb9f8..e2d8faabe 100644 --- a/test/bodyParser.js +++ b/test/bodyParser.js @@ -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(); + }); + }) }) }) \ No newline at end of file