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.

148 lines
4.3 KiB

  1. var Dicer = require('..');
  2. var assert = require('assert'),
  3. fs = require('fs'),
  4. path = require('path'),
  5. inspect = require('util').inspect;
  6. var FIXTURES_ROOT = __dirname + '/fixtures/';
  7. var t = 0,
  8. group = path.basename(__filename, '.js') + '/';
  9. var tests = [
  10. { source: 'many',
  11. opts: { boundary: '----WebKitFormBoundaryWLHCs9qmcJJoyjKR' },
  12. chsize: 16,
  13. nparts: 7,
  14. what: 'Extra trailer data pushed after finished'
  15. },
  16. ];
  17. function next() {
  18. if (t === tests.length)
  19. return;
  20. var v = tests[t],
  21. fixtureBase = FIXTURES_ROOT + v.source,
  22. fd,
  23. n = 0,
  24. buffer = Buffer.allocUnsafe(v.chsize),
  25. state = { parts: [] };
  26. fd = fs.openSync(fixtureBase + '/original', 'r');
  27. var dicer = new Dicer(v.opts),
  28. error,
  29. partErrors = 0,
  30. finishes = 0;
  31. dicer.on('part', function(p) {
  32. var part = {
  33. body: undefined,
  34. bodylen: 0,
  35. error: undefined,
  36. header: undefined
  37. };
  38. p.on('header', function(h) {
  39. part.header = h;
  40. }).on('data', function(data) {
  41. // make a copy because we are using readSync which re-uses a buffer ...
  42. var copy = Buffer.allocUnsafe(data.length);
  43. data.copy(copy);
  44. data = copy;
  45. if (!part.body)
  46. part.body = [ data ];
  47. else
  48. part.body.push(data);
  49. part.bodylen += data.length;
  50. }).on('error', function(err) {
  51. part.error = err;
  52. ++partErrors;
  53. }).on('end', function() {
  54. if (part.body)
  55. part.body = Buffer.concat(part.body, part.bodylen);
  56. state.parts.push(part);
  57. });
  58. }).on('error', function(err) {
  59. error = err;
  60. }).on('finish', function() {
  61. assert(finishes++ === 0, makeMsg(v.what, 'finish emitted multiple times'));
  62. if (v.dicerError)
  63. assert(error !== undefined, makeMsg(v.what, 'Expected error'));
  64. else
  65. assert(error === undefined, makeMsg(v.what, 'Unexpected error'));
  66. if (v.events && v.events.indexOf('part') > -1) {
  67. assert.equal(state.parts.length,
  68. v.nparts,
  69. makeMsg(v.what,
  70. 'Part count mismatch:\nActual: '
  71. + state.parts.length
  72. + '\nExpected: '
  73. + v.nparts));
  74. if (!v.npartErrors)
  75. v.npartErrors = 0;
  76. assert.equal(partErrors,
  77. v.npartErrors,
  78. makeMsg(v.what,
  79. 'Part errors mismatch:\nActual: '
  80. + partErrors
  81. + '\nExpected: '
  82. + v.npartErrors));
  83. for (var i = 0, header, body; i < v.nparts; ++i) {
  84. if (fs.existsSync(fixtureBase + '/part' + (i+1))) {
  85. body = fs.readFileSync(fixtureBase + '/part' + (i+1));
  86. if (body.length === 0)
  87. body = undefined;
  88. } else
  89. body = undefined;
  90. assert.deepEqual(state.parts[i].body,
  91. body,
  92. makeMsg(v.what,
  93. 'Part #' + (i+1) + ' body mismatch'));
  94. if (fs.existsSync(fixtureBase + '/part' + (i+1) + '.header')) {
  95. header = fs.readFileSync(fixtureBase
  96. + '/part' + (i+1) + '.header', 'binary');
  97. header = JSON.parse(header);
  98. } else
  99. header = undefined;
  100. assert.deepEqual(state.parts[i].header,
  101. header,
  102. makeMsg(v.what,
  103. 'Part #' + (i+1)
  104. + ' parsed header mismatch:\nActual: '
  105. + inspect(state.parts[i].header)
  106. + '\nExpected: '
  107. + inspect(header)));
  108. }
  109. }
  110. ++t;
  111. next();
  112. });
  113. while (true) {
  114. n = fs.readSync(fd, buffer, 0, buffer.length, null);
  115. if (n === 0) {
  116. setTimeout(function() {
  117. dicer.write('\r\n\r\n\r\n');
  118. dicer.end();
  119. }, 50);
  120. break;
  121. }
  122. dicer.write(n === buffer.length ? buffer : buffer.slice(0, n));
  123. }
  124. fs.closeSync(fd);
  125. }
  126. next();
  127. function makeMsg(what, msg) {
  128. return '[' + group + what + ']: ' + msg;
  129. }
  130. process.on('exit', function() {
  131. assert(t === tests.length,
  132. makeMsg('_exit', 'Only ran ' + t + '/' + tests.length + ' tests'));
  133. });