Skip to content
Browse files

added option support

  • Loading branch information...
1 parent 4cf4b80 commit 32b26bb8b483d410c7e23cd98bcfa0f344fbcd89 @tj tj committed Nov 17, 2011
Showing with 44 additions and 5 deletions.
  1. +11 −5 lib/middleware/bodyParser.js
  2. +33 −0 test/bodyParser.js
View
16 lib/middleware/bodyParser.js
@@ -51,19 +51,21 @@ function mime(req) {
* $ curl -d 'user[name]=tj' http://localhost/
* $ curl -d '{"user":{"name":"tj"}}' -H "Content-Type: application/json" http://localhost/
*
+ * @param {Object} options
* @return {Function}
* @api public
*/
-exports = module.exports = function bodyParser(){
+exports = module.exports = function bodyParser(options){
+ options = options || {};
return function bodyParser(req, res, next) {
if (req.body) return next();
req.body = {};
if ('GET' == req.method || 'HEAD' == req.method) return next();
var parser = exports.parse[mime(req)];
if (parser) {
- parser(req, next);
+ parser(req, options, next);
} else {
next();
}
@@ -80,7 +82,7 @@ exports.parse = {};
* Parse application/x-www-form-urlencoded.
*/
-exports.parse['application/x-www-form-urlencoded'] = function(req, fn){
+exports.parse['application/x-www-form-urlencoded'] = function(req, options, fn){
var buf = '';
req.setEncoding('utf8');
req.on('data', function(chunk){ buf += chunk });
@@ -98,7 +100,7 @@ exports.parse['application/x-www-form-urlencoded'] = function(req, fn){
* Parse application/json.
*/
-exports.parse['application/json'] = function(req, fn){
+exports.parse['application/json'] = function(req, options, fn){
var buf = '';
req.setEncoding('utf8');
req.on('data', function(chunk){ buf += chunk });
@@ -116,11 +118,15 @@ exports.parse['application/json'] = function(req, fn){
* Parse multipart/form-data.
*/
-exports.parse['multipart/form-data'] = function(req, fn){
+exports.parse['multipart/form-data'] = function(req, options, fn){
var form = new formidable.IncomingForm
, query = []
, files = {};
+ Object.keys(options).forEach(function(key){
+ form[key] = options[key];
+ });
+
form.on('field', function(name, val){
query.push(name + '=' + val);
});
View
33 test/bodyParser.js
@@ -64,6 +64,39 @@ describe('connect.bodyParser()', function(){
app.use(function(req, res){
req.body.user.should.eql({ name: 'Tobi' });
+ req.body.text.path.should.not.include.string('.txt');
+ req.body.text.constructor.name.should.equal('File');
+ res.end(req.body.text.name);
+ });
+
+ app.request()
+ .post('/')
+ .set('Content-Type', 'multipart/form-data; boundary=foo')
+ .write('--foo\r\n')
+ .write('Content-Disposition: form-data; name="user[name]"\r\n')
+ .write('\r\n')
+ .write('Tobi')
+ .write('\r\n--foo\r\n')
+ .write('Content-Disposition: form-data; name="text"; filename="foo.txt"\r\n')
+ .write('\r\n')
+ .write('some text here')
+ .write('\r\n--foo--')
+ .end(function(res){
+ res.body.should.equal('foo.txt');
+ done();
+ });
+ })
+
+ it('should expose options to formidable', function(done){
+ var app = connect();
+
+ app.use(connect.bodyParser({
+ keepExtensions: true
+ }));
+
+ app.use(function(req, res){
+ req.body.user.should.eql({ name: 'Tobi' });
+ req.body.text.path.should.include.string('.txt');
req.body.text.constructor.name.should.equal('File');
res.end(req.body.text.name);
});

0 comments on commit 32b26bb

Please sign in to comment.
Something went wrong with that request. Please try again.