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.

136 lines
3.7 KiB

4 years ago
  1. var assert = require('assert');
  2. var hpack = require('../');
  3. var fixtures = require('./fixtures');
  4. describe('hpack/compressor', function() {
  5. var comp;
  6. beforeEach(function() {
  7. comp = hpack.compressor.create({
  8. table: {
  9. maxSize: 1024
  10. }
  11. });
  12. });
  13. function expect(arr, enc) {
  14. function isNumber(num) {
  15. return typeof num === 'number';
  16. }
  17. var out = comp.read().toString('hex');
  18. if (Array.isArray(arr) && !arr.every(isNumber)) {
  19. arr = arr.map(function(item) {
  20. return new Buffer(item, enc);
  21. });
  22. arr = Buffer.concat(arr);
  23. } else {
  24. arr = new Buffer(arr, enc);
  25. }
  26. var actual = arr.toString('hex');
  27. assert.equal(out, actual);
  28. }
  29. describe('indexed field', function() {
  30. it('should lookup entry from static table', function() {
  31. comp.write([{ name: ':method', value: 'GET' }]);
  32. expect([ 0b10000000 | 2 ]);
  33. });
  34. it('should fetch entry from the end of the static table', function() {
  35. comp.write([{ name: 'www-authenticate', value: '' }]);
  36. expect([ 0b10000000 | 61 ]);
  37. });
  38. });
  39. describe('literal field', function() {
  40. it('should lookup name in the table (incremental)', function() {
  41. comp.write([{ name: 'host', value: 'localhost' }]);
  42. expect('6686a0e41d139d09', 'hex');
  43. comp.write([{ name: 'host', value: 'localhost' }]);
  44. expect([ 0b10000000 | 62 ]);
  45. });
  46. it('should lookup name in the table (not-incremental)', function() {
  47. comp.write([{ name: 'host', value: 'localhost', incremental: false }]);
  48. expect('0f1786a0e41d139d09', 'hex');
  49. // Should not use the table
  50. comp.write([{ name: 'host', value: 'localhost' }]);
  51. expect('6686a0e41d139d09', 'hex');
  52. });
  53. it('should evict header field from the table', function() {
  54. for (var i = 0; i < 1000; i++) {
  55. comp.write([{ name: 'host', value: 'localhost' + i }]);
  56. comp.read();
  57. }
  58. assert(comp._table.size < comp._table.maxSize);
  59. assert.equal(comp._table.dynamic.length, 21);
  60. });
  61. });
  62. describe('update size', function() {
  63. it('should evict header field from the table', function() {
  64. comp.write([{ name: 'host', value: 'localhost' }]);
  65. expect('6686a0e41d139d09', 'hex');
  66. comp.reset();
  67. // update=0, update=maxSize
  68. expect('203fe107', 'hex');
  69. comp.write([{ name: 'host', value: 'localhost' }]);
  70. expect('6686a0e41d139d09', 'hex');
  71. });
  72. it('should send dynamic update if size >= protocolMaxSize', function() {
  73. comp.updateTableSize(Infinity);
  74. // update=maxSize
  75. expect('3fe107', 'hex');
  76. });
  77. });
  78. describe('spec examples', function() {
  79. beforeEach(function() {
  80. comp = hpack.compressor.create({
  81. table: {
  82. maxSize: 256
  83. }
  84. });
  85. });
  86. var tests = fixtures.specExamples;
  87. tests.forEach(function(test, i) {
  88. var prev = tests[i - 1];
  89. it('should give expected output on ' + test.id, function() {
  90. var startFrom = test.continuation ? prev.comp : comp;
  91. if (!startFrom)
  92. throw new Error('Previous test failed');
  93. comp = startFrom;
  94. comp.write(test.output.map(function(pair) {
  95. return { name: pair[0], value: pair[1], huffman: test.huffman };
  96. }));
  97. expect(test.input.replace(/ /g, ''), 'hex');
  98. // Verify table contents
  99. assert.deepEqual(comp._table.dynamic.map(function(header) {
  100. return [ header.name, header.value, header.totalSize ];
  101. }).reverse(), test.table);
  102. // Verify table size
  103. var expectedSize = test.table.reduce(function(acc, item) {
  104. return acc + item[2];
  105. }, 0);
  106. assert.equal(comp._table.size, expectedSize);
  107. test.comp = comp;
  108. });
  109. });
  110. });
  111. });