| 1 |
'use strict' |
| 2 |
|
| 3 |
const Dicer = require('../deps/dicer/lib/Dicer') |
| 4 |
const fs = require('node:fs') |
| 5 |
const path = require('node:path') |
| 6 |
const inspect = require('node:util').inspect |
| 7 |
const { test } = require('node:test') |
| 8 |
|
| 9 |
const FIXTURES_ROOT = path.join(__dirname, 'fixtures/') |
| 10 |
|
| 11 |
test('dicer-multipart', async t => { |
| 12 |
const tests = |
| 13 |
[ |
| 14 |
{ |
| 15 |
source: 'nested', |
| 16 |
opts: { boundary: 'AaB03x' }, |
| 17 |
chsize: 32, |
| 18 |
nparts: 2, |
| 19 |
what: 'One nested multipart', |
| 20 |
plan: 10 |
| 21 |
}, |
| 22 |
{ |
| 23 |
source: 'many', |
| 24 |
opts: { boundary: '----WebKitFormBoundaryWLHCs9qmcJJoyjKR' }, |
| 25 |
chsize: 16, |
| 26 |
nparts: 7, |
| 27 |
what: 'Many parts', |
| 28 |
plan: 20 |
| 29 |
}, |
| 30 |
{ |
| 31 |
source: 'many-wrongboundary', |
| 32 |
opts: { boundary: 'LOLOLOL' }, |
| 33 |
chsize: 8, |
| 34 |
nparts: 0, |
| 35 |
dicerError: true, |
| 36 |
what: 'Many parts, wrong boundary', |
| 37 |
plan: 6 |
| 38 |
}, |
| 39 |
{ |
| 40 |
source: 'many-noend', |
| 41 |
opts: { boundary: '----WebKitFormBoundaryWLHCs9qmcJJoyjKR' }, |
| 42 |
chsize: 16, |
| 43 |
nparts: 7, |
| 44 |
npartErrors: 1, |
| 45 |
dicerError: true, |
| 46 |
what: 'Many parts, end boundary missing, 1 file open', |
| 47 |
plan: 20 |
| 48 |
}, |
| 49 |
{ |
| 50 |
source: 'nested-full', |
| 51 |
opts: { boundary: 'AaB03x', headerFirst: true }, |
| 52 |
chsize: 32, |
| 53 |
nparts: 2, |
| 54 |
what: 'One nested multipart with preceding header', |
| 55 |
plan: 10 |
| 56 |
}, |
| 57 |
{ |
| 58 |
source: 'nested-full', |
| 59 |
opts: { headerFirst: true }, |
| 60 |
chsize: 32, |
| 61 |
nparts: 2, |
| 62 |
setBoundary: 'AaB03x', |
| 63 |
what: 'One nested multipart with preceding header, using setBoundary', |
| 64 |
plan: 10 |
| 65 |
} |
| 66 |
] |
| 67 |
|
| 68 |
t.plan(tests.length) |
| 69 |
|
| 70 |
for (const v of tests) { |
| 71 |
await t.test(v.what, async t => { |
| 72 |
t.plan(v.plan) |
| 73 |
await new Promise((resolve) => { |
| 74 |
const fixtureBase = FIXTURES_ROOT + v.source |
| 75 |
const state = { parts: [], preamble: undefined } |
| 76 |
|
| 77 |
const dicer = new Dicer(v.opts) |
| 78 |
let error |
| 79 |
let partErrors = 0 |
| 80 |
let finishes = 0 |
| 81 |
|
| 82 |
dicer.on('preamble', function (p) { |
| 83 |
const preamble = { |
| 84 |
body: undefined, |
| 85 |
bodylen: 0, |
| 86 |
error: undefined, |
| 87 |
header: undefined |
| 88 |
} |
| 89 |
|
| 90 |
p.on('header', function (h) { |
| 91 |
preamble.header = h |
| 92 |
if (v.setBoundary) { dicer.setBoundary(v.setBoundary) } |
| 93 |
}).on('data', function (data) { |
| 94 |
|
| 95 |
const copy = Buffer.allocUnsafe(data.length) |
| 96 |
data.copy(copy) |
| 97 |
data = copy |
| 98 |
if (!preamble.body) { preamble.body = [data] } else { preamble.body.push(data) } |
| 99 |
preamble.bodylen += data.length |
| 100 |
}).on('error', function (err) { |
| 101 |
preamble.error = err |
| 102 |
}).on('end', function () { |
| 103 |
if (preamble.body) { preamble.body = Buffer.concat(preamble.body, preamble.bodylen) } |
| 104 |
if (preamble.body || preamble.header) { state.preamble = preamble } |
| 105 |
}) |
| 106 |
}) |
| 107 |
dicer.on('part', function (p) { |
| 108 |
const part = { |
| 109 |
body: undefined, |
| 110 |
bodylen: 0, |
| 111 |
error: undefined, |
| 112 |
header: undefined |
| 113 |
} |
| 114 |
|
| 115 |
p.on('header', function (h) { |
| 116 |
part.header = h |
| 117 |
}).on('data', function (data) { |
| 118 |
if (!part.body) { part.body = [data] } else { part.body.push(data) } |
| 119 |
part.bodylen += data.length |
| 120 |
}).on('error', function (err) { |
| 121 |
part.error = err |
| 122 |
++partErrors |
| 123 |
}).on('end', function () { |
| 124 |
if (part.body) { part.body = Buffer.concat(part.body, part.bodylen) } |
| 125 |
state.parts.push(part) |
| 126 |
}) |
| 127 |
}).on('error', function (err) { |
| 128 |
error = err |
| 129 |
}).on('finish', function () { |
| 130 |
t.assert.strictEqual(finishes++ === 0, true, makeMsg(v.what, 'finish emitted multiple times')) |
| 131 |
|
| 132 |
if (v.dicerError) { t.assert.strictEqual(error !== undefined, true, makeMsg(v.what, 'Expected error')) } else { t.assert.strictEqual(error === undefined, true, makeMsg(v.what, 'Unexpected error: ' + error)) } |
| 133 |
|
| 134 |
let preamble |
| 135 |
if (fs.existsSync(fixtureBase + '/preamble')) { |
| 136 |
const prebody = fs.readFileSync(fixtureBase + '/preamble') |
| 137 |
if (prebody.length) { |
| 138 |
preamble = { |
| 139 |
body: prebody, |
| 140 |
bodylen: prebody.length, |
| 141 |
error: undefined, |
| 142 |
header: undefined |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
if (fs.existsSync(fixtureBase + '/preamble.header')) { |
| 147 |
const prehead = JSON.parse(fs.readFileSync(fixtureBase + |
| 148 |
'/preamble.header', 'binary')) |
| 149 |
if (!preamble) { |
| 150 |
preamble = { |
| 151 |
body: undefined, |
| 152 |
bodylen: 0, |
| 153 |
error: undefined, |
| 154 |
header: prehead |
| 155 |
} |
| 156 |
} else { preamble.header = prehead } |
| 157 |
} |
| 158 |
if (fs.existsSync(fixtureBase + '/preamble.error')) { |
| 159 |
const err = new Error(fs.readFileSync(fixtureBase + |
| 160 |
'/preamble.error', 'binary')) |
| 161 |
if (!preamble) { |
| 162 |
preamble = { |
| 163 |
body: undefined, |
| 164 |
bodylen: 0, |
| 165 |
error: err, |
| 166 |
header: undefined |
| 167 |
} |
| 168 |
} else { preamble.error = err } |
| 169 |
} |
| 170 |
|
| 171 |
t.assert.deepEqual(state.preamble, |
| 172 |
preamble, |
| 173 |
makeMsg(v.what, |
| 174 |
'Preamble mismatch:\nActual:' + |
| 175 |
inspect(state.preamble) + |
| 176 |
'\nExpected: ' + |
| 177 |
inspect(preamble))) |
| 178 |
|
| 179 |
t.assert.equal(state.parts.length, |
| 180 |
v.nparts, |
| 181 |
makeMsg(v.what, |
| 182 |
'Part count mismatch:\nActual: ' + |
| 183 |
state.parts.length + |
| 184 |
'\nExpected: ' + |
| 185 |
v.nparts)) |
| 186 |
|
| 187 |
if (!v.npartErrors) { v.npartErrors = 0 } |
| 188 |
t.assert.equal(partErrors, |
| 189 |
v.npartErrors, |
| 190 |
makeMsg(v.what, |
| 191 |
'Part errors mismatch:\nActual: ' + |
| 192 |
partErrors + |
| 193 |
'\nExpected: ' + |
| 194 |
v.npartErrors)) |
| 195 |
|
| 196 |
for (let i = 0, header, body; i < v.nparts; ++i) { |
| 197 |
if (fs.existsSync(fixtureBase + '/part' + (i + 1))) { |
| 198 |
body = fs.readFileSync(fixtureBase + '/part' + (i + 1)) |
| 199 |
if (body.length === 0) { body = undefined } |
| 200 |
} else { body = undefined } |
| 201 |
t.assert.deepEqual(state.parts[i].body, |
| 202 |
body, |
| 203 |
makeMsg(v.what, |
| 204 |
'Part #' + (i + 1) + ' body mismatch')) |
| 205 |
if (fs.existsSync(fixtureBase + '/part' + (i + 1) + '.header')) { |
| 206 |
header = fs.readFileSync(fixtureBase + |
| 207 |
'/part' + (i + 1) + '.header', 'binary') |
| 208 |
header = JSON.parse(header) |
| 209 |
} else { header = undefined } |
| 210 |
t.assert.deepEqual(state.parts[i].header, |
| 211 |
header, |
| 212 |
makeMsg(v.what, |
| 213 |
'Part #' + (i + 1) + |
| 214 |
' parsed header mismatch:\nActual: ' + |
| 215 |
inspect(state.parts[i].header) + |
| 216 |
'\nExpected: ' + |
| 217 |
inspect(header))) |
| 218 |
} |
| 219 |
t.assert.ok('pass') |
| 220 |
resolve() |
| 221 |
}) |
| 222 |
|
| 223 |
fs.createReadStream(fixtureBase + '/original').pipe(dicer) |
| 224 |
}) |
| 225 |
}) |
| 226 |
} |
| 227 |
}) |
| 228 |
|
| 229 |
function makeMsg (what, msg) { |
| 230 |
return what + ': ' + msg |
| 231 |
} |