Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] test: express integration #173

Merged
merged 2 commits into from
Aug 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/make-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
67 changes: 67 additions & 0 deletions test/express-integration.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})
})