Skip to content

Commit

Permalink
Merge pull request #664 from bminer/master
Browse files Browse the repository at this point in the history
Added `defer` property to multipart() middleware
  • Loading branch information
tj committed Oct 2, 2012
2 parents 8760300 + ff36e83 commit cb80fcd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
18 changes: 14 additions & 4 deletions lib/middleware/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ function noop(req, res, next) {
* Options:
*
* - `limit` byte limit defaulting to none
* - `defer` defers processing and exposes the Formidable form object as `req.form`.
* `next()` is called without waiting for the form's "end" event.
* This option is useful if you need to bind to the "progress" event, for example.
*
* @param {Object} options
* @return {Function}
Expand Down Expand Up @@ -99,8 +102,10 @@ exports = module.exports = function(options){
});

form.on('error', function(err){
err.status = 400;
next(err);
if (!options.defer) {
err.status = 400;
next(err);
}
done = true;
});

Expand All @@ -109,13 +114,18 @@ exports = module.exports = function(options){
try {
req.body = qs.parse(data);
req.files = qs.parse(files);
next();
if (!options.defer) next();
} catch (err) {
next(err);
form.emit('error', err);
}
});

form.parse(req);

if (options.defer) {
req.form = form;
next();
}
});
}
};
32 changes: 31 additions & 1 deletion test/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,35 @@ describe('connect.multipart()', function(){
});
})

it('should defer processing if `defer` is set', function(done){
var app = connect();

app.use(connect.multipart({"defer": true}));

app.use(function(req, res){
JSON.stringify(req.body).should.equal("{}");
req.form.on("end", function() {
res.end(JSON.stringify(req.body));
});
});

app.request()
.post('/')
.set('Content-Type', 'multipart/form-data; boundary=foo')
.write('--foo\r\n')
.write('Content-Disposition: form-data; name="user"\r\n')
.write('\r\n')
.write('Tobi')
.write('\r\n--foo\r\n')
.write('Content-Disposition: form-data; name="age"\r\n')
.write('\r\n')
.write('1')
.write('\r\n--foo--')
.end(function(res){
res.body.should.equal('{"user":"Tobi","age":"1"}');
done();
});
})

})
})
})

0 comments on commit cb80fcd

Please sign in to comment.