summaryrefslogtreecommitdiff
path: root/node_modules/busboy/test/test-types-multipart-charsets.js
blob: ed9c38aeb6c1f3f85c9eb0efa071349504dbfde6 (plain) (blame)
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
'use strict';

const assert = require('assert');
const { inspect } = require('util');

const { mustCall } = require(`${__dirname}/common.js`);

const busboy = require('..');

const input = Buffer.from([
  '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
 'Content-Disposition: form-data; '
   + 'name="upload_file_0"; filename="テスト.dat"',
 'Content-Type: application/octet-stream',
 '',
 'A'.repeat(1023),
 '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
].join('\r\n'));
const boundary = '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k';
const expected = [
  { type: 'file',
    name: 'upload_file_0',
    data: Buffer.from('A'.repeat(1023)),
    info: {
      filename: 'テスト.dat',
      encoding: '7bit',
      mimeType: 'application/octet-stream',
    },
    limited: false,
  },
];
const bb = busboy({
  defParamCharset: 'utf8',
  headers: {
    'content-type': `multipart/form-data; boundary=${boundary}`,
  }
});
const results = [];

bb.on('field', (name, val, info) => {
  results.push({ type: 'field', name, val, info });
});

bb.on('file', (name, stream, info) => {
  const data = [];
  let nb = 0;
  const file = {
    type: 'file',
    name,
    data: null,
    info,
    limited: false,
  };
  results.push(file);
  stream.on('data', (d) => {
    data.push(d);
    nb += d.length;
  }).on('limit', () => {
    file.limited = true;
  }).on('close', () => {
    file.data = Buffer.concat(data, nb);
    assert.strictEqual(stream.truncated, file.limited);
  }).once('error', (err) => {
    file.err = err.message;
  });
});

bb.on('error', (err) => {
  results.push({ error: err.message });
});

bb.on('partsLimit', () => {
  results.push('partsLimit');
});

bb.on('filesLimit', () => {
  results.push('filesLimit');
});

bb.on('fieldsLimit', () => {
  results.push('fieldsLimit');
});

bb.on('close', mustCall(() => {
  assert.deepStrictEqual(
    results,
    expected,
    'Results mismatch.\n'
      + `Parsed: ${inspect(results)}\n`
      + `Expected: ${inspect(expected)}`
  );
}));

bb.end(input);