Skip to content
Browse files

added multipart end listener

  • Loading branch information...
1 parent 54708f9 commit ab638976c06de69925e669903f045b1c5cfe2eed @kilianc committed Jun 2, 2012
View
18 lib-cov/middleware/multipart-parser.js
@@ -11,7 +11,10 @@ if (! _$jscoverage['middleware/multipart-parser.js']) {
_$jscoverage['middleware/multipart-parser.js'][9] = 0;
_$jscoverage['middleware/multipart-parser.js'][10] = 0;
_$jscoverage['middleware/multipart-parser.js'][11] = 0;
+ _$jscoverage['middleware/multipart-parser.js'][12] = 0;
_$jscoverage['middleware/multipart-parser.js'][13] = 0;
+ _$jscoverage['middleware/multipart-parser.js'][15] = 0;
+ _$jscoverage['middleware/multipart-parser.js'][17] = 0;
}
_$jscoverage['middleware/multipart-parser.js'][1]++;
var formidable = require("formidable");
@@ -29,12 +32,19 @@ module.exports = (function () {
_$jscoverage['middleware/multipart-parser.js'][9]++;
request.form = new formidable.IncomingForm();
_$jscoverage['middleware/multipart-parser.js'][10]++;
- request.form.parse(request);
- _$jscoverage['middleware/multipart-parser.js'][11]++;
+ request.form.parse(request, (function (err, fields, files) {
+ _$jscoverage['middleware/multipart-parser.js'][11]++;
+ request.body = fields;
+ _$jscoverage['middleware/multipart-parser.js'][12]++;
+ request.files = files;
+ _$jscoverage['middleware/multipart-parser.js'][13]++;
+ request.parseError = err;
+}));
+ _$jscoverage['middleware/multipart-parser.js'][15]++;
return next();
}
- _$jscoverage['middleware/multipart-parser.js'][13]++;
+ _$jscoverage['middleware/multipart-parser.js'][17]++;
next();
});
});
-_$jscoverage['middleware/multipart-parser.js'].source = ["var formidable = require('formidable')","","module.exports = function () {"," return function (request, response, next) {"," if (!request.method.match(/(PUT|POST|OPTIONS)/)) {"," return next()"," }"," if (request.headers['content-type'].match(/multipart\\/form-data/)) {"," request.form = new formidable.IncomingForm()"," request.form.parse(request)"," return next()"," }"," next()"," }","}"];
+_$jscoverage['middleware/multipart-parser.js'].source = ["var formidable = require('formidable')","","module.exports = function () {"," return function (request, response, next) {"," if (!request.method.match(/(PUT|POST|OPTIONS)/)) {"," return next()"," }"," if (request.headers['content-type'].match(/multipart\\/form-data/)) {"," request.form = new formidable.IncomingForm()"," request.form.parse(request, function (err, fields, files) {"," request.body = fields"," request.files = files"," request.parseError = err"," })"," return next()"," }"," next()"," }","}"];
View
6 lib/middleware/multipart-parser.js
@@ -7,7 +7,11 @@ module.exports = function () {
}
if (request.headers['content-type'].match(/multipart\/form-data/)) {
request.form = new formidable.IncomingForm()
- request.form.parse(request)
+ request.form.parse(request, function (err, fields, files) {
+ request.body = fields
+ request.files = files
+ request.parseError = err
+ })
return next()
}
next()
View
6 test/fixtures/multipart-module.js
@@ -14,6 +14,12 @@ module.exports = {
response.serveJSON(fields)
})
},
+ 'multipartEnd': function (request, response) {
+ request.resume()
+ request.form.once('end', function () {
+ response.serveJSON({ fields: request.body, files: Object.keys(request.files), err: request.parseError })
+ })
+ },
'skip': function (request, response) {
request.resume()
request.once('end', function () {
View
54 test/middleware/multipart-parser-test.js
@@ -6,6 +6,20 @@ var should = require('should'),
var apiserver
var defaultPort = 9000
var customPort = 8080
+var requestBody = '--AaB03x\r\n'+
+ 'content-disposition: form-data; name="foo"\r\n'+
+ '\r\n'+
+ 'bar\r\n'+
+ '--AaB03x\r\n'+
+ 'content-disposition: form-data; name="bar"\r\n'+
+ '\r\n'+
+ 'foo\r\n'+
+ '--AaB03x\r\n'+
+ 'content-disposition: form-data; name="quote"; filename="quote.txt"\r\n'+
+ 'Content-Type: text/plain\r\n'+
+ '\r\n'+
+ 'no detail is too small\r\n'+
+ '--AaB03x--'
describe('middleware/MultipartParser', function () {
describe('should skip', function () {
@@ -69,26 +83,36 @@ describe('middleware/MultipartParser', function () {
jsonreq.post({
uri: 'http://localhost:' + defaultPort + '/v1/test/multipart',
headers: { 'content-type': 'multipart/form-data; boundary=AaB03x' },
- body:
- '--AaB03x\r\n'+
- 'content-disposition: form-data; name="foo"\r\n'+
- '\r\n'+
- 'bar\r\n'+
- '--AaB03x\r\n'+
- 'content-disposition: form-data; name="bar"\r\n'+
- '\r\n'+
- 'foo\r\n'+
- '--AaB03x\r\n'+
- 'content-disposition: form-data; name="quote"; filename="quote.txt"\r\n'+
- 'Content-Type: text/plain\r\n'+
- '\r\n'+
- 'no detail is too small\r\n'+
- '--AaB03x--'
+ body: requestBody
}, function (err, response, body) {
body = JSON.parse(body)
body.should.be.eql({ foo: 'bar', bar: 'foo', quote: 'no detail is too small' })
done(err)
})
})
})
+ describe('end listener', function () {
+ before(function (done) {
+ apiserver = new ApiServer()
+ apiserver.addModule('v1', 'test', testModule)
+ apiserver.use(/./, ApiServer.multipartParser())
+ apiserver.listen(defaultPort, done)
+ })
+ after(function () {
+ apiserver.close()
+ })
+ it('should attach files and fields to the request', function (done) {
+ jsonreq.post({
+ uri: 'http://localhost:' + defaultPort + '/v1/test/multipart_end',
+ headers: { 'content-type': 'multipart/form-data; boundary=AaB03x' },
+ body: requestBody
+ }, function (err, response, body) {
+ body = JSON.parse(body)
+ body.fields.should.be.eql({ foo: 'bar', bar: 'foo' })
+ body.files.should.be.eql(['quote'])
+ should.not.exist(body.parseError)
+ done(err)
+ })
+ })
+ })
})

0 comments on commit ab63897

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