UNPKG

2.37 kBJavaScriptView Raw
1'use strict';
2
3const assert = require('assert');
4const { randomFillSync } = require('crypto');
5const { inspect } = require('util');
6
7const busboy = require('..');
8
9const { mustCall } = require('./common.js');
10
11const BOUNDARY = 'u2KxIV5yF1y+xUspOQCCZopaVgeV6Jxihv35XQJmuTx8X3sh';
12
13function formDataSection(key, value) {
14 return Buffer.from(
15 `\r\n--${BOUNDARY}`
16 + `\r\nContent-Disposition: form-data; name="${key}"`
17 + `\r\n\r\n${value}`
18 );
19}
20
21function formDataFile(key, filename, contentType) {
22 const buf = Buffer.allocUnsafe(100000);
23 return Buffer.concat([
24 Buffer.from(`\r\n--${BOUNDARY}\r\n`),
25 Buffer.from(`Content-Disposition: form-data; name="${key}"`
26 + `; filename="${filename}"\r\n`),
27 Buffer.from(`Content-Type: ${contentType}\r\n\r\n`),
28 randomFillSync(buf)
29 ]);
30}
31
32const reqChunks = [
33 Buffer.concat([
34 formDataFile('file', 'file.bin', 'application/octet-stream'),
35 formDataSection('foo', 'foo value'),
36 ]),
37 formDataSection('bar', 'bar value'),
38 Buffer.from(`\r\n--${BOUNDARY}--\r\n`)
39];
40const bb = busboy({
41 headers: {
42 'content-type': `multipart/form-data; boundary=${BOUNDARY}`
43 }
44});
45const expected = [
46 { type: 'file',
47 name: 'file',
48 info: {
49 filename: 'file.bin',
50 encoding: '7bit',
51 mimeType: 'application/octet-stream',
52 },
53 },
54 { type: 'field',
55 name: 'foo',
56 val: 'foo value',
57 info: {
58 nameTruncated: false,
59 valueTruncated: false,
60 encoding: '7bit',
61 mimeType: 'text/plain',
62 },
63 },
64 { type: 'field',
65 name: 'bar',
66 val: 'bar value',
67 info: {
68 nameTruncated: false,
69 valueTruncated: false,
70 encoding: '7bit',
71 mimeType: 'text/plain',
72 },
73 },
74];
75const results = [];
76
77bb.on('field', (name, val, info) => {
78 results.push({ type: 'field', name, val, info });
79});
80
81bb.on('file', (name, stream, info) => {
82 results.push({ type: 'file', name, info });
83 // Simulate a pipe where the destination is pausing (perhaps due to waiting
84 // for file system write to finish)
85 setTimeout(() => {
86 stream.resume();
87 }, 10);
88});
89
90bb.on('close', mustCall(() => {
91 assert.deepStrictEqual(
92 results,
93 expected,
94 'Results mismatch.\n'
95 + `Parsed: ${inspect(results)}\n`
96 + `Expected: ${inspect(expected)}`
97 );
98}));
99
100for (const chunk of reqChunks)
101 bb.write(chunk);
102bb.end();