Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

integrated new node-querystring

  • Loading branch information...
commit 31f0b1b3eda98210dbbe3a7691a405f99c48fb67 1 parent e7b6aae
@tj tj authored
Showing with 45 additions and 19 deletions.
  1. +15 −19 lib/middleware/bodyParser.js
  2. +30 −0 test/bodyParser.js
View
34 lib/middleware/bodyParser.js
@@ -126,31 +126,32 @@ exports.parse['application/json'] = function(req, options, fn){
/**
* Parse multipart/form-data.
+ *
+ * TODO: make multiple support optional
+ * TODO: revisit "error" flag if it's a formidable bug
*/
exports.parse['multipart/form-data'] = function(req, options, fn){
var form = new formidable.IncomingForm
- , query = []
- , files = {}
+ , data = {}
, done;
Object.keys(options).forEach(function(key){
form[key] = options[key];
});
- form.on('field', function(name, val){
- query.push(name + '=' + val);
- });
-
- form.on('file', function(name, file){
- if (Array.isArray(files[name])) {
- files[name].push(file);
- } else if (files[name]) {
- files[name] = [files[name], file];
+ function ondata(name, val){
+ if (Array.isArray(data[name])) {
+ data[name].push(val);
+ } else if (data[name]) {
+ data[name] = [data[name], val];
} else {
- files[name] = file;
+ data[name] = val;
}
- });
+ }
+
+ form.on('field', ondata);
+ form.on('file', ondata);
form.on('error', function(err){
fn(err);
@@ -160,12 +161,7 @@ exports.parse['multipart/form-data'] = function(req, options, fn){
form.on('end', function(){
if (done) return;
try {
- query = query.join('&');
- query = qs.parse(query);
- req.body = query;
- Object.keys(files).forEach(function(name){
- req.body[name] = files[name];
- });
+ req.body = qs.parse(data);
fn();
} catch (err) {
fn(err);
View
30 test/bodyParser.js
@@ -191,6 +191,36 @@ describe('connect.bodyParser()', function(){
});
})
+ it('should support nested files', function(done){
+ var app = connect();
+
+ app.use(connect.bodyParser());
+
+ app.use(function(req, res){
+ Object.keys(req.body.docs).should.have.length(2);
+ req.body.docs.foo.name.should.equal('foo.txt');
+ req.body.docs.bar.name.should.equal('bar.txt');
+ res.end();
+ });
+
+ app.request()
+ .post('/')
+ .set('Content-Type', 'multipart/form-data; boundary=foo')
+ .write('--foo\r\n')
+ .write('Content-Disposition: form-data; name="docs[foo]"; filename="foo.txt"\r\n')
+ .write('\r\n')
+ .write('some text here')
+ .write('\r\n--foo\r\n')
+ .write('Content-Disposition: form-data; name="docs[bar]"; filename="bar.txt"\r\n')
+ .write('\r\n')
+ .write('some more text stuff')
+ .write('\r\n--foo--')
+ .end(function(res){
+ res.statusCode.should.equal(200);
+ done();
+ });
+ })
+
it('should next(err) on multipart failure', function(done){
var app = connect();
Please sign in to comment.
Something went wrong with that request. Please try again.