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.

87 lines
2.0 KiB

  1. var Dicer = require('..');
  2. var assert = require('assert');
  3. var CRLF = '\r\n';
  4. var boundary = 'boundary';
  5. var writeSep = '--' + boundary;
  6. var writePart = [
  7. writeSep,
  8. 'Content-Type: text/plain',
  9. 'Content-Length: 0'
  10. ].join(CRLF)
  11. + CRLF + CRLF
  12. + 'some data' + CRLF;
  13. var writeEnd = '--' + CRLF;
  14. var firedEnd = false;
  15. var firedFinish = false;
  16. var dicer = new Dicer({boundary: boundary});
  17. dicer.on('part', partListener);
  18. dicer.on('finish', finishListener);
  19. dicer.write(writePart+writeSep);
  20. function partListener(partReadStream) {
  21. partReadStream.on('data', function(){});
  22. partReadStream.on('end', partEndListener);
  23. }
  24. function partEndListener() {
  25. firedEnd = true;
  26. setImmediate(afterEnd);
  27. }
  28. function afterEnd() {
  29. dicer.end(writeEnd);
  30. setImmediate(afterWrite);
  31. }
  32. function finishListener() {
  33. assert(firedEnd, 'Failed to end before finishing');
  34. firedFinish = true;
  35. test2();
  36. }
  37. function afterWrite() {
  38. assert(firedFinish, 'Failed to finish');
  39. }
  40. var isPausePush = true;
  41. var firedPauseCallback = false;
  42. var firedPauseFinish = false;
  43. var dicer2 = null;
  44. function test2() {
  45. dicer2 = new Dicer({boundary: boundary});
  46. dicer2.on('part', pausePartListener);
  47. dicer2.on('finish', pauseFinish);
  48. dicer2.write(writePart+writeSep, 'utf8', pausePartCallback);
  49. setImmediate(pauseAfterWrite);
  50. }
  51. function pausePartListener(partReadStream) {
  52. partReadStream.on('data', function(){});
  53. partReadStream.on('end', function(){});
  54. var realPush = partReadStream.push;
  55. partReadStream.push = function fakePush() {
  56. realPush.apply(partReadStream, arguments);
  57. if (!isPausePush)
  58. return true;
  59. isPausePush = false;
  60. return false;
  61. };
  62. }
  63. function pauseAfterWrite() {
  64. dicer2.end(writeEnd);
  65. setImmediate(pauseAfterEnd);
  66. }
  67. function pauseAfterEnd() {
  68. assert(firedPauseCallback, 'Failed to call callback after pause');
  69. assert(firedPauseFinish, 'Failed to finish after pause');
  70. }
  71. function pauseFinish() {
  72. firedPauseFinish = true;
  73. }
  74. function pausePartCallback() {
  75. firedPauseCallback = true;
  76. }