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.

166 lines
4.4 KiB

4 years ago
  1. var assert = require('assert');
  2. var hpack = require('../');
  3. describe('hpack/encoder', function() {
  4. var encoder;
  5. beforeEach(function() {
  6. encoder = hpack.encoder.create();
  7. });
  8. function expect(arr, enc) {
  9. function isNumber(num) {
  10. return typeof num === 'number';
  11. }
  12. var out = Buffer.concat(encoder.render()).toString('hex');
  13. if (Array.isArray(arr) && !arr.every(isNumber)) {
  14. arr = arr.map(function(item) {
  15. return new Buffer(item, enc);
  16. });
  17. arr = Buffer.concat(arr);
  18. } else {
  19. arr = new Buffer(arr, enc);
  20. }
  21. var actual = arr.toString('hex');
  22. assert.equal(out, actual);
  23. }
  24. describe('bit', function() {
  25. it('should encode number bit-by-bit', function() {
  26. [ 1, 1, 1, 0, 1, 0, 1, 0,
  27. 1, 0, 1, 0, 1, 1, 1, 1 ].forEach(function(bit) {
  28. encoder.encodeBit(bit);
  29. });
  30. expect([
  31. 0b11101010,
  32. 0b10101111
  33. ]);
  34. });
  35. it('should encode number using multiple bits', function() {
  36. for (var i = 0; i < 8; i++)
  37. encoder.encodeBits(0b1011010, 7);
  38. expect([
  39. 0b10110101,
  40. 0b01101010,
  41. 0b11010101,
  42. 0b10101011,
  43. 0b01010110,
  44. 0b10101101,
  45. 0b01011010
  46. ]);
  47. });
  48. });
  49. describe('integer', function() {
  50. it('should encode 10 in prefix-5 (C.1.1)', function() {
  51. encoder.skipBits(3);
  52. encoder.encodeInt(10);
  53. expect([ 0x0a ]);
  54. });
  55. it('should decode 1337 in prefix-5 (C.1.2)', function() {
  56. encoder.skipBits(3);
  57. encoder.encodeInt(1337);
  58. expect([
  59. 0b00011111,
  60. 0b10011010,
  61. 0b00001010
  62. ]);
  63. });
  64. it('should decode 42 at octect boundary (C.1.3)', function() {
  65. encoder.encodeInt(42);
  66. expect([ 0b00101010 ]);
  67. });
  68. it('should regression 6-bit test', function() {
  69. encoder.skipBits(2);
  70. encoder.encodeInt(63);
  71. expect([ 0b00111111, 0b00000000 ]);
  72. });
  73. });
  74. describe('string', function() {
  75. it('should encode literal from (C.2.1)', function() {
  76. encoder.encodeStr(new Buffer('custom-key'), false);
  77. expect([ [ 0x0a ], 'custom-key' ]);
  78. });
  79. it('should encode literal from (C.4.1)', function() {
  80. encoder.encodeStr(new Buffer('www.example.com'), true);
  81. expect('8c' +
  82. 'f1e3 c2e5 f23a 6ba0 ab90 f4ff'.replace(/ /g, ''),
  83. 'hex');
  84. });
  85. it('should decode literal from (C.4.2)', function() {
  86. encoder.encodeStr(new Buffer('no-cache'), true);
  87. expect(
  88. '86' +
  89. 'a8eb 1064 9cbf'.replace(/ /g, ''),
  90. 'hex');
  91. });
  92. it('should encode literal from (C.4.3) #1', function() {
  93. encoder.encodeStr(new Buffer('custom-key'), true);
  94. expect(
  95. '88' +
  96. '25a8 49e9 5ba9 7d7f'.replace(/ /g, ''),
  97. 'hex');
  98. });
  99. it('should encode literal from (C.4.3) #2', function() {
  100. encoder.encodeStr(new Buffer('custom-value'), true);
  101. expect(
  102. '89' +
  103. '25a8 49e9 5bb8 e8b4 bf'.replace(/ /g, ''),
  104. 'hex');
  105. });
  106. it('should encode literal from (C.6.1) #1', function() {
  107. encoder.encodeStr(new Buffer('Mon, 21 Oct 2013 20:13:21 GMT'), true);
  108. expect(
  109. ('96' +
  110. 'd07a be94 1054 d444 a820 0595 040b 8166' +
  111. 'e082 a62d 1bff').replace(/ /g, ''),
  112. 'hex');
  113. });
  114. it('should encode literal from (C.6.1) #2', function() {
  115. encoder.encodeStr(new Buffer('https://www.example.com'), true);
  116. expect(
  117. ('91' +
  118. '9d29 ad17 1863 c78f 0b97 c8e9 ae82 ae43' +
  119. 'd3').replace(/ /g, ''),
  120. 'hex');
  121. });
  122. it('should encode many 5 bit chars', function() {
  123. encoder.encodeStr(new Buffer('eeeeeeee'), true);
  124. // e = 00101, 0x294A5294A5 = 00101 x 8
  125. expect('85294A5294A5', 'hex');
  126. });
  127. it('should encode many 5 bit chars with 3-bit EOS', function() {
  128. // e = 00101, EOS=111,
  129. // 0x294A5294A52F = 00101 x 9 + 111
  130. encoder.encodeStr(new Buffer('eeeeeeeee'), true);
  131. expect('86294A5294A52F', 'hex');
  132. });
  133. it('should decode many 5 bit chars with 2-bit EOS', function() {
  134. // e = 00101, EOS=11,
  135. // 0x294A5297 = 00101 x 6 + 11
  136. encoder.encodeStr(new Buffer('eeeeee'), true);
  137. expect('84294A5297', 'hex');
  138. });
  139. it('should decode many multi-octet chars', function() {
  140. encoder.encodeStr([ 1, 1, 1, 1, 1, 1, 1, 1 ], true);
  141. expect('97ffffb1ffff63fffec7fffd8ffffb1ffff63fffec7fffd8',
  142. 'hex');
  143. });
  144. });
  145. });