test-headerparser.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. var assert = require('assert'),
  2. path = require('path');
  3. var HeaderParser = require('../lib/HeaderParser');
  4. var DCRLF = '\r\n\r\n',
  5. MAXED_BUFFER = new Buffer(128 * 1024);
  6. MAXED_BUFFER.fill(0x41); // 'A'
  7. var group = path.basename(__filename, '.js') + '/';
  8. [
  9. { source: DCRLF,
  10. expected: {},
  11. what: 'No header'
  12. },
  13. { source: ['Content-Type:\t text/plain',
  14. 'Content-Length:0'
  15. ].join('\r\n') + DCRLF,
  16. expected: {'content-type': [' text/plain'], 'content-length': ['0']},
  17. what: 'Value spacing'
  18. },
  19. { source: ['Content-Type:\r\n text/plain',
  20. 'Foo:\r\n bar\r\n baz',
  21. ].join('\r\n') + DCRLF,
  22. expected: {'content-type': [' text/plain'], 'foo': [' bar baz']},
  23. what: 'Folded values'
  24. },
  25. { source: ['Content-Type:',
  26. 'Foo: ',
  27. ].join('\r\n') + DCRLF,
  28. expected: {'content-type': [''], 'foo': ['']},
  29. what: 'Empty values'
  30. },
  31. { source: MAXED_BUFFER.toString('ascii') + DCRLF,
  32. expected: {},
  33. what: 'Max header size (single chunk)'
  34. },
  35. { source: ['ABCDEFGHIJ', MAXED_BUFFER.toString('ascii'), DCRLF],
  36. expected: {},
  37. what: 'Max header size (multiple chunks #1)'
  38. },
  39. { source: [MAXED_BUFFER.toString('ascii'), MAXED_BUFFER.toString('ascii'), DCRLF],
  40. expected: {},
  41. what: 'Max header size (multiple chunk #2)'
  42. },
  43. ].forEach(function(v) {
  44. var parser = new HeaderParser(),
  45. fired = false;
  46. parser.on('header', function(header) {
  47. assert(!fired, makeMsg(v.what, 'Header event fired more than once'));
  48. fired = true;
  49. assert.deepEqual(header,
  50. v.expected,
  51. makeMsg(v.what, 'Parsed result mismatch'));
  52. });
  53. if (!Array.isArray(v.source))
  54. v.source = [v.source];
  55. v.source.forEach(function(s) {
  56. parser.push(s);
  57. });
  58. assert(fired, makeMsg(v.what, 'Did not receive header from parser'));
  59. });
  60. function makeMsg(what, msg) {
  61. return '[' + group + what + ']: ' + msg;
  62. }