test.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. const assert = require('assert');
  3. const StreamSearch = require('../lib/sbmh.js');
  4. [
  5. {
  6. needle: '\r\n',
  7. chunks: [
  8. 'foo',
  9. ' bar',
  10. '\r',
  11. '\n',
  12. 'baz, hello\r',
  13. '\n world.',
  14. '\r\n Node.JS rules!!\r\n\r\n',
  15. ],
  16. expect: [
  17. [false, 'foo'],
  18. [false, ' bar'],
  19. [ true, null],
  20. [false, 'baz, hello'],
  21. [ true, null],
  22. [false, ' world.'],
  23. [ true, null],
  24. [ true, ' Node.JS rules!!'],
  25. [ true, ''],
  26. ],
  27. },
  28. {
  29. needle: '---foobarbaz',
  30. chunks: [
  31. '---foobarbaz',
  32. 'asdf',
  33. '\r\n',
  34. '---foobarba',
  35. '---foobar',
  36. 'ba',
  37. '\r\n---foobarbaz--\r\n',
  38. ],
  39. expect: [
  40. [ true, null],
  41. [false, 'asdf'],
  42. [false, '\r\n'],
  43. [false, '---foobarba'],
  44. [false, '---foobarba'],
  45. [ true, '\r\n'],
  46. [false, '--\r\n'],
  47. ],
  48. },
  49. ].forEach((test, i) => {
  50. console.log(`Running test #${i + 1}`);
  51. const { needle, chunks, expect } = test;
  52. const results = [];
  53. const ss = new StreamSearch(Buffer.from(needle),
  54. (isMatch, data, start, end) => {
  55. if (data)
  56. data = data.toString('latin1', start, end);
  57. else
  58. data = null;
  59. results.push([isMatch, data]);
  60. });
  61. for (const chunk of chunks)
  62. ss.push(Buffer.from(chunk));
  63. assert.deepStrictEqual(results, expect);
  64. });