summaryrefslogtreecommitdiffstats
path: root/fastify-busboy/test/multipart-stream-pause.test.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-21 20:56:19 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-21 20:56:19 +0000
commit0b6210cd37b68b94252cb798598b12974a20e1c1 (patch)
treee371686554a877842d95aa94f100bee552ff2a8e /fastify-busboy/test/multipart-stream-pause.test.js
parentInitial commit. (diff)
downloadnode-undici-0b6210cd37b68b94252cb798598b12974a20e1c1.tar.xz
node-undici-0b6210cd37b68b94252cb798598b12974a20e1c1.zip
Adding upstream version 5.28.2+dfsg1+~cs23.11.12.3.upstream/5.28.2+dfsg1+_cs23.11.12.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'fastify-busboy/test/multipart-stream-pause.test.js')
-rw-r--r--fastify-busboy/test/multipart-stream-pause.test.js82
1 files changed, 82 insertions, 0 deletions
diff --git a/fastify-busboy/test/multipart-stream-pause.test.js b/fastify-busboy/test/multipart-stream-pause.test.js
new file mode 100644
index 0000000..856cf71
--- /dev/null
+++ b/fastify-busboy/test/multipart-stream-pause.test.js
@@ -0,0 +1,82 @@
+'use strict'
+
+const { inspect } = require('util')
+const { test } = require('tap')
+
+const Busboy = require('..')
+
+const BOUNDARY = 'u2KxIV5yF1y+xUspOQCCZopaVgeV6Jxihv35XQJmuTx8X3sh'
+
+function formDataSection (key, value) {
+ return Buffer.from('\r\n--' + BOUNDARY +
+ '\r\nContent-Disposition: form-data; name="' +
+ key + '"\r\n\r\n' + value)
+}
+function formDataFile (key, filename, contentType) {
+ return Buffer.concat([
+ Buffer.from('\r\n--' + BOUNDARY + '\r\n'),
+ Buffer.from('Content-Disposition: form-data; name="' +
+ key + '"; filename="' + filename + '"\r\n'),
+ Buffer.from('Content-Type: ' + contentType + '\r\n\r\n'),
+ Buffer.allocUnsafe(100000)
+ ])
+}
+
+test('multipart-stream-pause - processes stream correctly', t => {
+ t.plan(6)
+ const reqChunks = [
+ Buffer.concat([
+ formDataFile('file', 'file.bin', 'application/octet-stream'),
+ formDataSection('foo', 'foo value')
+ ]),
+ formDataSection('bar', 'bar value'),
+ Buffer.from('\r\n--' + BOUNDARY + '--\r\n')
+ ]
+ const busboy = new Busboy({
+ headers: {
+ 'content-type': 'multipart/form-data; boundary=' + BOUNDARY
+ }
+ })
+ let finishes = 0
+ const results = []
+ const expected = [
+ ['file', 'file', 'file.bin', '7bit', 'application/octet-stream'],
+ ['field', 'foo', 'foo value', false, false, '7bit', 'text/plain'],
+ ['field', 'bar', 'bar value', false, false, '7bit', 'text/plain']
+ ]
+
+ busboy.on('field', function (key, val, keyTrunc, valTrunc, encoding, contype) {
+ results.push(['field', key, val, keyTrunc, valTrunc, encoding, contype])
+ })
+ busboy.on('file', function (fieldname, stream, filename, encoding, mimeType) {
+ results.push(['file', fieldname, filename, encoding, mimeType])
+ // Simulate a pipe where the destination is pausing (perhaps due to waiting
+ // for file system write to finish)
+ setTimeout(function () {
+ stream.resume()
+ }, 10)
+ })
+ busboy.on('finish', function () {
+ t.ok(finishes++ === 0, 'finish emitted multiple times')
+ t.strictSame(results.length,
+ expected.length,
+ 'Parsed result count mismatch. Saw ' +
+ results.length +
+ '. Expected: ' + expected.length)
+
+ results.forEach(function (result, i) {
+ t.strictSame(result,
+ expected[i],
+ 'Result mismatch:\nParsed: ' + inspect(result) +
+ '\nExpected: ' + inspect(expected[i]))
+ })
+ t.pass()
+ }).on('error', function (err) {
+ t.error(err)
+ })
+
+ reqChunks.forEach(function (buf) {
+ busboy.write(buf)
+ })
+ busboy.end()
+})