-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathexpress-integration.js
More file actions
201 lines (160 loc) · 4.98 KB
/
express-integration.js
File metadata and controls
201 lines (160 loc) · 4.98 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* eslint-env mocha */
var assert = require('assert')
var http = require('http')
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 () {
var app
before(function (done) {
app = express()
app.listen(port, done)
})
function submitForm (form, path, cb) {
var req = form.submit('http://localhost:' + port + path)
req.on('error', cb)
req.on('response', function (res) {
res.on('error', cb)
res.pipe(concat({ encoding: 'buffer' }, function (body) {
cb(null, res, body)
}))
})
}
it('should work with express error handling', function (done) {
var limits = { fileSize: 200 }
var upload = multer({ limits: limits })
var router = new express.Router()
var form = new FormData()
var routeCalled = 0
var errorCalled = 0
form.append('avatar', util.file('large.jpg'))
router.post('/profile', upload.single('avatar'), function (req, res, next) {
routeCalled++
res.status(200).end('SUCCESS')
})
router.use(function (err, req, res, next) {
assert.strictEqual(err.code, 'LIMIT_FILE_SIZE')
errorCalled++
res.status(500).end('ERROR')
})
app.use('/t1', router)
submitForm(form, '/t1/profile', function (err, res, body) {
assert.ifError(err)
assert.strictEqual(routeCalled, 0)
assert.strictEqual(errorCalled, 1)
assert.strictEqual(body.toString(), 'ERROR')
assert.strictEqual(res.statusCode, 500)
done()
})
})
it('should work when receiving error from fileFilter', function (done) {
function fileFilter (req, file, cb) {
if (file.mimetype !== 'image/png') {
return cb(new Error('Only PNG files are allowed'))
}
}
var upload = multer({ fileFilter: fileFilter })
var router = new express.Router()
var form = new FormData()
var routeCalled = 0
var errorCalled = 0
form.append('avatar', util.file('small0.dat'))
router.post('/profile', upload.single('avatar'), function (req, res, next) {
routeCalled++
res.status(200).end('SUCCESS')
})
router.use(function (err, req, res, next) {
assert.strictEqual(err.message, 'Only PNG files are allowed')
errorCalled++
res.status(500).end('ERROR')
})
app.use('/t2', router)
submitForm(form, '/t2/profile', function (err, res, body) {
assert.ifError(err)
assert.strictEqual(routeCalled, 0)
assert.strictEqual(errorCalled, 1)
assert.strictEqual(body.toString(), 'ERROR')
assert.strictEqual(res.statusCode, 500)
done()
})
})
it('should not crash on malformed request', function (done) {
var upload = multer()
app.post('/upload', upload.single('file'), function (req, res) {
res.status(500).end('Request should not be processed')
})
app.use(function (err, req, res, next) {
assert.strictEqual(err.message, 'Unexpected end of form')
res.status(200).end('Correct error')
})
var boundary = 'AaB03x'
var body = [
'--' + boundary,
'Content-Disposition: form-data; name="file"; filename="test.txt"',
'Content-Type: text/plain',
'',
'test without end boundary'
].join('\r\n')
var options = {
hostname: 'localhost',
port,
path: '/upload',
method: 'POST',
headers: {
'content-type': 'multipart/form-data; boundary=' + boundary,
'content-length': body.length
}
}
var req = http.request(options, (res) => {
assert.strictEqual(res.statusCode, 200)
done()
})
req.on('error', (err) => {
done(err)
})
req.write(body)
req.end()
})
it('should not crash on malformed request that causes two errors to be emitted by busboy', function (done) {
var upload = multer()
app.post('/upload2', upload.single('file'), function (req, res) {
res.status(500).end('Request should not be processed')
})
app.use(function (err, req, res, next) {
assert.strictEqual(err.message, 'Malformed part header')
res.status(200).end('Correct error')
})
var boundary = 'AaB03x'
// this payload causes two errors to be emitted by busboy: `Malformed part header` and `Unexpected end of form`
var body = [
'--' + boundary,
'Content-Disposition: form-data; name="file"; filename="test.txt"',
'Content-Type: text/plain',
'',
'--' + boundary + '--',
''
].join('\r\n')
var options = {
hostname: 'localhost',
port,
path: '/upload2',
method: 'POST',
headers: {
'content-type': 'multipart/form-data; boundary=' + boundary,
'content-length': body.length
}
}
var req = http.request(options, (res) => {
assert.strictEqual(res.statusCode, 200)
done()
})
req.on('error', (err) => {
done(err)
})
req.write(body)
req.end()
})
})