Repository navigation
Expand file tree
/
Copy pathtypes-multipart-epilogue.test.js
More file actions
67 lines (55 loc) · 1.83 KB
/
Copy pathtypes-multipart-epilogue.test.js
File metadata and controls
67 lines (55 loc) · 1.83 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
'use strict'
const { test } = require('node:test')
const { setImmediate: nextTick } = require('node:timers/promises')
const Busboy = require('..')
function multipartBody (boundary, extra) {
return '--' + boundary + '\r\n' +
'Content-Disposition: form-data; name="field"\r\n' +
'\r\n' +
'value\r\n' +
'--' + boundary + '--\r\n' +
(extra || '')
}
test('emits finish when an RFC 2046 epilogue arrives in a later write', async (t) => {
const boundary = 'boundary'
const busboy = new Busboy({
headers: { 'content-type': 'multipart/form-data; boundary=' + boundary }
})
const fields = []
busboy.on('field', (name, value) => fields.push([name, value]))
const finished = new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(new Error('finish was never emitted'))
}, 1000)
busboy.once('finish', () => {
clearTimeout(timer)
resolve()
})
busboy.once('error', (err) => {
clearTimeout(timer)
reject(err)
})
})
busboy.write(Buffer.from(multipartBody(boundary), 'utf8'))
await nextTick()
busboy.write(Buffer.from('epilogue', 'utf8'))
busboy.end()
await finished
t.assert.deepStrictEqual(fields, [['field', 'value']])
})
test('still emits finish when the epilogue is in the same chunk as the closing delimiter', async (t) => {
const boundary = 'boundary'
const busboy = new Busboy({
headers: { 'content-type': 'multipart/form-data; boundary=' + boundary }
})
const fields = []
busboy.on('field', (name, value) => fields.push([name, value]))
const finished = new Promise((resolve, reject) => {
busboy.once('finish', resolve)
busboy.once('error', reject)
})
busboy.write(Buffer.from(multipartBody(boundary, 'epilogue'), 'utf8'))
busboy.end()
await finished
t.assert.deepStrictEqual(fields, [['field', 'value']])
})