test-types-multipart-charsets.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. const assert = require('assert');
  3. const { inspect } = require('util');
  4. const { mustCall } = require(`${__dirname}/common.js`);
  5. const busboy = require('..');
  6. const input = Buffer.from([
  7. '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k',
  8. 'Content-Disposition: form-data; '
  9. + 'name="upload_file_0"; filename="テスト.dat"',
  10. 'Content-Type: application/octet-stream',
  11. '',
  12. 'A'.repeat(1023),
  13. '-----------------------------paZqsnEHRufoShdX6fh0lUhXBP4k--'
  14. ].join('\r\n'));
  15. const boundary = '---------------------------paZqsnEHRufoShdX6fh0lUhXBP4k';
  16. const expected = [
  17. { type: 'file',
  18. name: 'upload_file_0',
  19. data: Buffer.from('A'.repeat(1023)),
  20. info: {
  21. filename: 'テスト.dat',
  22. encoding: '7bit',
  23. mimeType: 'application/octet-stream',
  24. },
  25. limited: false,
  26. },
  27. ];
  28. const bb = busboy({
  29. defParamCharset: 'utf8',
  30. headers: {
  31. 'content-type': `multipart/form-data; boundary=${boundary}`,
  32. }
  33. });
  34. const results = [];
  35. bb.on('field', (name, val, info) => {
  36. results.push({ type: 'field', name, val, info });
  37. });
  38. bb.on('file', (name, stream, info) => {
  39. const data = [];
  40. let nb = 0;
  41. const file = {
  42. type: 'file',
  43. name,
  44. data: null,
  45. info,
  46. limited: false,
  47. };
  48. results.push(file);
  49. stream.on('data', (d) => {
  50. data.push(d);
  51. nb += d.length;
  52. }).on('limit', () => {
  53. file.limited = true;
  54. }).on('close', () => {
  55. file.data = Buffer.concat(data, nb);
  56. assert.strictEqual(stream.truncated, file.limited);
  57. }).once('error', (err) => {
  58. file.err = err.message;
  59. });
  60. });
  61. bb.on('error', (err) => {
  62. results.push({ error: err.message });
  63. });
  64. bb.on('partsLimit', () => {
  65. results.push('partsLimit');
  66. });
  67. bb.on('filesLimit', () => {
  68. results.push('filesLimit');
  69. });
  70. bb.on('fieldsLimit', () => {
  71. results.push('fieldsLimit');
  72. });
  73. bb.on('close', mustCall(() => {
  74. assert.deepStrictEqual(
  75. results,
  76. expected,
  77. 'Results mismatch.\n'
  78. + `Parsed: ${inspect(results)}\n`
  79. + `Expected: ${inspect(expected)}`
  80. );
  81. }));
  82. bb.end(input);