From 6436d4dcf327fc6229d389b0066ec41af46b1c95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Wed, 22 Jul 2015 23:07:05 +0200 Subject: [PATCH 1/2] test: express integration --- package.json | 1 + test/express-integration.js | 67 +++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 test/express-integration.js diff --git a/package.json b/package.json index 87793ac2..fdf3be99 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "xtend": "^4.0.0" }, "devDependencies": { + "express": "^4.13.1", "form-data": "^1.0.0-rc1", "fs-temp": "^0.1.2", "mocha": "^2.2.5", diff --git a/test/express-integration.js b/test/express-integration.js new file mode 100644 index 00000000..10ffb53c --- /dev/null +++ b/test/express-integration.js @@ -0,0 +1,67 @@ +/* eslint-env mocha */ + +var assert = require('assert') + +var multer = require('../') +var util = require('./_util') + +var express = require('express') +var FormData = require('form-data') +var concat = require('concat-stream') + +var port = 34279 + +describe('Express Integration', function () { + it('should work with express error handling', function (done) { + var app = express() + var limits = { fileSize: 200 } + var upload = multer({ limits: limits }) + var form = new FormData() + + var routeCalled = 0 + var errorCalled = 0 + + form.append('avatar', util.file('large.jpg')) + + app.post('/profile', upload.single('avatar'), function (req, res, next) { + routeCalled++ + res.status(200).end('SUCCESS') + }) + + app.use(function (err, req, res, next) { + assert.equal(err.code, 'LIMIT_FILE_SIZE') + + errorCalled++ + res.status(500).end('ERROR') + }) + + app.listen(port, function () { + var res, body + var req = form.submit('http://localhost:' + port + '/profile') + var pending = 2 + + function validate () { + assert.equal(routeCalled, 0) + assert.equal(errorCalled, 1) + assert.equal(body.toString(), 'ERROR') + assert.equal(res.statusCode, 500) + + done() + } + + req.on('response', function (_res) { + res = _res + + res.pipe(concat({ encoding: 'buffer' }, function (_body) { + body = _body + + if (--pending === 0) validate() + })) + }) + + req.on('finish', function () { + if (--pending === 0) validate() + }) + }) + }) +}) From dfa2095f76966eb7ea2cee6b446f2e7de710fb48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20Unneb=C3=A4ck?= Date: Thu, 6 Aug 2015 21:03:53 +0200 Subject: [PATCH 2/2] lib: fix handling of file size limits --- lib/make-middleware.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/make-middleware.js b/lib/make-middleware.js index d5612f34..e13f1d2a 100644 --- a/lib/make-middleware.js +++ b/lib/make-middleware.js @@ -98,6 +98,7 @@ function makeMiddleware (setup) { if (err) return abortWithError(err) if (!includeFile) return fileStream.resume() + var aborting = false pendingWrites.increment() Object.defineProperty(file, 'stream', { @@ -112,11 +113,16 @@ function makeMiddleware (setup) { }) fileStream.on('limit', function () { - pendingWrites.decrement() + aborting = true abortWithCode('LIMIT_FILE_SIZE', fieldname) }) storage._handleFile(req, file, function (err, info) { + if (aborting) { + uploadedFiles.push(extend(file, info)) + return pendingWrites.decrement() + } + if (err) { pendingWrites.decrement() return abortWithError(err)