forked from expressjs/multer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file-filter.js
56 lines (45 loc) · 1.49 KB
/
file-filter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* eslint-env mocha */
var assert = require('assert')
var util = require('./_util')
var multer = require('../')
var FormData = require('form-data')
function withFilter (fileFilter) {
return multer({ fileFilter: fileFilter })
}
function skipSpecificFile (req, file, cb) {
cb(null, file.fieldname !== 'notme')
}
function reportFakeError (req, file, cb) {
cb(new Error('Fake error'))
}
describe('File Filter', function () {
it('should skip some files', function (done) {
var form = new FormData()
var upload = withFilter(skipSpecificFile)
var parser = upload.fields([
{ name: 'notme', maxCount: 1 },
{ name: 'butme', maxCount: 1 }
])
form.append('notme', util.file('tiny0.dat'))
form.append('butme', util.file('tiny1.dat'))
util.submitForm(parser, form, function (err, req) {
assert.ifError(err)
assert.equal(req.files['notme'], undefined)
assert.equal(req.files['butme'][0].fieldname, 'butme')
assert.equal(req.files['butme'][0].originalname, 'tiny1.dat')
assert.equal(req.files['butme'][0].size, 7)
assert.equal(req.files['butme'][0].buffer.length, 7)
done()
})
})
it('should report errors from fileFilter', function (done) {
var form = new FormData()
var upload = withFilter(reportFakeError)
var parser = upload.single('test')
form.append('test', util.file('tiny0.dat'))
util.submitForm(parser, form, function (err, req) {
assert.equal(err.message, 'Fake error')
done()
})
})
})