-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathbusboy-options.js
More file actions
131 lines (110 loc) · 3.68 KB
/
Copy pathbusboy-options.js
File metadata and controls
131 lines (110 loc) · 3.68 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
/* eslint-env mocha */
var assert = require('assert')
var stream = require('stream')
var multer = require('../')
// Multipart body with one text field whose value is "åäö" encoded as latin1
// (bytes e5 e4 f6) and no charset declared for the part.
function latin1FieldRequest () {
var boundary = 'AaB03x'
var body = Buffer.concat([
Buffer.from('--' + boundary + '\r\nContent-Disposition: form-data; name="name"\r\n\r\n'),
Buffer.from([0xe5, 0xe4, 0xf6]),
Buffer.from('\r\n--' + boundary + '--\r\n')
])
var req = new stream.PassThrough()
req.headers = {
'content-type': 'multipart/form-data; boundary=' + boundary,
'content-length': body.length
}
req.end(body)
return req
}
function fileRequest () {
var boundary = 'AaB03x'
var body = [
'--' + boundary,
'Content-Disposition: form-data; name="file"; filename="a.txt"',
'Content-Type: text/plain',
'',
'file content',
'--' + boundary + '--',
''
].join('\r\n')
var req = new stream.PassThrough()
req.headers = {
'content-type': 'multipart/form-data; boundary=' + boundary,
'content-length': body.length
}
req.end(body)
return req
}
// Storage engine that records the file stream's highWaterMark.
function recordingStorage (record) {
return {
_handleFile: function (req, file, cb) {
record.readableHighWaterMark = file.stream.readableHighWaterMark
file.stream.resume()
file.stream.on('end', function () { cb(null, {}) })
},
_removeFile: function (req, file, cb) { cb(null) }
}
}
describe('busboy options', function () {
it('should decode text fields as utf8 by default', function (done) {
var upload = multer()
var req = latin1FieldRequest()
upload.none()(req, null, function (err) {
assert.ifError(err)
// latin1 bytes are not valid utf8, so the value is decoded with replacement characters
assert.notStrictEqual(req.body.name, 'åäö')
assert.ok(req.body.name.indexOf('\ufffd') !== -1)
done()
})
})
it('should decode text fields with the given defCharset', function (done) {
var upload = multer({ defCharset: 'latin1' })
var req = latin1FieldRequest()
upload.none()(req, null, function (err) {
assert.ifError(err)
assert.strictEqual(req.body.name, 'åäö')
done()
})
})
it('should apply defCharset to .any() as well', function (done) {
var upload = multer({ defCharset: 'latin1' })
var req = latin1FieldRequest()
upload.any()(req, null, function (err) {
assert.ifError(err)
assert.strictEqual(req.body.name, 'åäö')
done()
})
})
it('should keep the default file stream highWaterMark when fileHwm is not set', function (done) {
var record = {}
var upload = multer({ storage: recordingStorage(record) })
upload.single('file')(fileRequest(), null, function (err) {
assert.ifError(err)
assert.strictEqual(record.readableHighWaterMark, new stream.Readable().readableHighWaterMark)
done()
})
})
it('should pass fileHwm to the file streams', function (done) {
var record = {}
var upload = multer({ storage: recordingStorage(record), fileHwm: 1024 })
upload.single('file')(fileRequest(), null, function (err) {
assert.ifError(err)
assert.strictEqual(record.readableHighWaterMark, 1024)
done()
})
})
it('should accept highWaterMark for the parser stream', function (done) {
var record = {}
var upload = multer({ storage: recordingStorage(record), highWaterMark: 1024 })
var req = fileRequest()
upload.single('file')(req, null, function (err) {
assert.ifError(err)
assert.strictEqual(req.file.originalname, 'a.txt')
done()
})
})
})