You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.2 KiB

  1. var Busboy = require('..');
  2. var path = require('path'),
  3. inspect = require('util').inspect,
  4. assert = require('assert');
  5. var EMPTY_FN = function() {};
  6. var t = 0,
  7. group = path.basename(__filename, '.js') + '/';
  8. var tests = [
  9. { source: ['foo'],
  10. expected: [['foo', '', false, false]],
  11. what: 'Unassigned value'
  12. },
  13. { source: ['foo=bar'],
  14. expected: [['foo', 'bar', false, false]],
  15. what: 'Assigned value'
  16. },
  17. { source: ['foo&bar=baz'],
  18. expected: [['foo', '', false, false],
  19. ['bar', 'baz', false, false]],
  20. what: 'Unassigned and assigned value'
  21. },
  22. { source: ['foo=bar&baz'],
  23. expected: [['foo', 'bar', false, false],
  24. ['baz', '', false, false]],
  25. what: 'Assigned and unassigned value'
  26. },
  27. { source: ['foo=bar&baz=bla'],
  28. expected: [['foo', 'bar', false, false],
  29. ['baz', 'bla', false, false]],
  30. what: 'Two assigned values'
  31. },
  32. { source: ['foo&bar'],
  33. expected: [['foo', '', false, false],
  34. ['bar', '', false, false]],
  35. what: 'Two unassigned values'
  36. },
  37. { source: ['foo&bar&'],
  38. expected: [['foo', '', false, false],
  39. ['bar', '', false, false]],
  40. what: 'Two unassigned values and ampersand'
  41. },
  42. { source: ['foo=bar+baz%2Bquux'],
  43. expected: [['foo', 'bar baz+quux', false, false]],
  44. what: 'Assigned value with (plus) space'
  45. },
  46. { source: ['foo=bar%20baz%21'],
  47. expected: [['foo', 'bar baz!', false, false]],
  48. what: 'Assigned value with encoded bytes'
  49. },
  50. { source: ['foo%20bar=baz%20bla%21'],
  51. expected: [['foo bar', 'baz bla!', false, false]],
  52. what: 'Assigned value with encoded bytes #2'
  53. },
  54. { source: ['foo=bar%20baz%21&num=1000'],
  55. expected: [['foo', 'bar baz!', false, false],
  56. ['num', '1000', false, false]],
  57. what: 'Two assigned values, one with encoded bytes'
  58. },
  59. { source: ['foo=bar&baz=bla'],
  60. expected: [],
  61. what: 'Limits: zero fields',
  62. limits: { fields: 0 }
  63. },
  64. { source: ['foo=bar&baz=bla'],
  65. expected: [['foo', 'bar', false, false]],
  66. what: 'Limits: one field',
  67. limits: { fields: 1 }
  68. },
  69. { source: ['foo=bar&baz=bla'],
  70. expected: [['foo', 'bar', false, false],
  71. ['baz', 'bla', false, false]],
  72. what: 'Limits: field part lengths match limits',
  73. limits: { fieldNameSize: 3, fieldSize: 3 }
  74. },
  75. { source: ['foo=bar&baz=bla'],
  76. expected: [['fo', 'bar', true, false],
  77. ['ba', 'bla', true, false]],
  78. what: 'Limits: truncated field name',
  79. limits: { fieldNameSize: 2 }
  80. },
  81. { source: ['foo=bar&baz=bla'],
  82. expected: [['foo', 'ba', false, true],
  83. ['baz', 'bl', false, true]],
  84. what: 'Limits: truncated field value',
  85. limits: { fieldSize: 2 }
  86. },
  87. { source: ['foo=bar&baz=bla'],
  88. expected: [['fo', 'ba', true, true],
  89. ['ba', 'bl', true, true]],
  90. what: 'Limits: truncated field name and value',
  91. limits: { fieldNameSize: 2, fieldSize: 2 }
  92. },
  93. { source: ['foo=bar&baz=bla'],
  94. expected: [['fo', '', true, true],
  95. ['ba', '', true, true]],
  96. what: 'Limits: truncated field name and zero value limit',
  97. limits: { fieldNameSize: 2, fieldSize: 0 }
  98. },
  99. { source: ['foo=bar&baz=bla'],
  100. expected: [['', '', true, true],
  101. ['', '', true, true]],
  102. what: 'Limits: truncated zero field name and zero value limit',
  103. limits: { fieldNameSize: 0, fieldSize: 0 }
  104. },
  105. { source: ['&'],
  106. expected: [],
  107. what: 'Ampersand'
  108. },
  109. { source: ['&&&&&'],
  110. expected: [],
  111. what: 'Many ampersands'
  112. },
  113. { source: ['='],
  114. expected: [['', '', false, false]],
  115. what: 'Assigned value, empty name and value'
  116. },
  117. { source: [''],
  118. expected: [],
  119. what: 'Nothing'
  120. },
  121. ];
  122. function next() {
  123. if (t === tests.length)
  124. return;
  125. var v = tests[t];
  126. var busboy = new Busboy({
  127. limits: v.limits,
  128. headers: {
  129. 'content-type': 'application/x-www-form-urlencoded; charset=utf-8'
  130. }
  131. }),
  132. finishes = 0,
  133. results = [];
  134. busboy.on('field', function(key, val, keyTrunc, valTrunc) {
  135. results.push([key, val, keyTrunc, valTrunc]);
  136. });
  137. busboy.on('file', function() {
  138. throw new Error(makeMsg(v.what, 'Unexpected file'));
  139. });
  140. busboy.on('finish', function() {
  141. assert(finishes++ === 0, makeMsg(v.what, 'finish emitted multiple times'));
  142. assert.deepEqual(results.length,
  143. v.expected.length,
  144. makeMsg(v.what, 'Parsed result count mismatch. Saw '
  145. + results.length
  146. + '. Expected: ' + v.expected.length));
  147. var i = 0;
  148. results.forEach(function(result) {
  149. assert.deepEqual(result,
  150. v.expected[i],
  151. makeMsg(v.what,
  152. 'Result mismatch:\nParsed: ' + inspect(result)
  153. + '\nExpected: ' + inspect(v.expected[i]))
  154. );
  155. ++i;
  156. });
  157. ++t;
  158. next();
  159. });
  160. v.source.forEach(function(s) {
  161. busboy.write(Buffer.from(s, 'utf8'), EMPTY_FN);
  162. });
  163. busboy.end();
  164. }
  165. next();
  166. function makeMsg(what, msg) {
  167. return '[' + group + what + ']: ' + msg;
  168. }
  169. process.on('exit', function() {
  170. assert(t === tests.length, makeMsg('_exit', 'Only finished ' + t + '/' + tests.length + ' tests'));
  171. });