decoder-test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. var assert = require('assert');
  2. var hpack = require('../');
  3. describe('hpack/decoder', function() {
  4. var decoder;
  5. beforeEach(function() {
  6. decoder = hpack.decoder.create();
  7. });
  8. describe('bit', function() {
  9. it('should decode number bit-by-bit', function() {
  10. decoder.push([ 0b11101010, 0b10101111 ]);
  11. var out = '';
  12. for (var i = 0; i < 16; i++)
  13. out += decoder.decodeBit();
  14. assert.equal(out, '11101010' + '10101111');
  15. });
  16. });
  17. describe('integer', function() {
  18. it('should decode 10 in prefix-5 (C.1.1)', function() {
  19. decoder.push([ 0b11101010 ]);
  20. decoder.skipBits(3);
  21. assert.equal(decoder.decodeInt(), 10);
  22. });
  23. it('should decode 1337 in prefix-5 (C.1.2)', function() {
  24. decoder.push([ 0b11111111, 0b10011010, 0b00001010 ]);
  25. decoder.skipBits(3);
  26. assert.equal(decoder.decodeInt(), 1337);
  27. });
  28. it('should decode 42 at octect boundary (C.1.3)', function() {
  29. decoder.push([ 0b00101010 ]);
  30. assert.equal(decoder.decodeInt(8), 42);
  31. });
  32. it('should throw on empty input', function() {
  33. assert.throws(function() {
  34. decoder.decodeInt();
  35. });
  36. });
  37. it('should throw on incomplete int', function() {
  38. decoder.push([ 0b11111111, 0b10011010 ]);
  39. decoder.skipBits(3);
  40. assert.throws(function() {
  41. decoder.decodeInt();
  42. });
  43. });
  44. it('should throw on overflowing int', function() {
  45. decoder.push([
  46. 0b11111111,
  47. 0b10011010,
  48. 0b10011010,
  49. 0b10011010,
  50. 0b10011010,
  51. 0b10011010
  52. ]);
  53. decoder.skipBits(3);
  54. assert.throws(function() {
  55. decoder.decodeInt();
  56. });
  57. });
  58. });
  59. describe('string', function() {
  60. it('should decode literal from (C.2.1)', function() {
  61. decoder.push([ 0x0a ]);
  62. decoder.push(new Buffer('custom-key'));
  63. assert.equal(decoder.decodeStr().toString(), 'custom-key');
  64. });
  65. it('should decode literal from (C.4.1)', function() {
  66. decoder.push(new Buffer(
  67. '8c' +
  68. 'f1e3 c2e5 f23a 6ba0 ab90 f4ff'.replace(/ /g, ''),
  69. 'hex'));
  70. assert.equal(new Buffer(decoder.decodeStr()).toString(),
  71. 'www.example.com');
  72. });
  73. it('should decode literal from (C.4.2)', function() {
  74. decoder.push(new Buffer(
  75. '86' +
  76. 'a8eb 1064 9cbf'.replace(/ /g, ''),
  77. 'hex'));
  78. assert.equal(new Buffer(decoder.decodeStr()).toString(), 'no-cache');
  79. });
  80. it('should decode literal from (C.4.3) #1', function() {
  81. decoder.push(new Buffer(
  82. '88' +
  83. '25a8 49e9 5ba9 7d7f'.replace(/ /g, ''),
  84. 'hex'));
  85. assert.equal(new Buffer(decoder.decodeStr()).toString(), 'custom-key');
  86. });
  87. it('should decode literal from (C.4.3) #2', function() {
  88. decoder.push(new Buffer(
  89. '89' +
  90. '25a8 49e9 5bb8 e8b4 bf'.replace(/ /g, ''),
  91. 'hex'));
  92. assert.equal(new Buffer(decoder.decodeStr()).toString(), 'custom-value');
  93. });
  94. it('should decode literal from (C.6.1) #1', function() {
  95. decoder.push(new Buffer(
  96. ('96' +
  97. 'd07a be94 1054 d444 a820 0595 040b 8166' +
  98. 'e082 a62d 1bff').replace(/ /g, ''),
  99. 'hex'));
  100. assert.equal(new Buffer(decoder.decodeStr()).toString(),
  101. 'Mon, 21 Oct 2013 20:13:21 GMT');
  102. });
  103. it('should decode literal from (C.6.1) #2', function() {
  104. decoder.push(new Buffer(
  105. ('91' +
  106. '9d29 ad17 1863 c78f 0b97 c8e9 ae82 ae43' +
  107. 'd3').replace(/ /g, ''),
  108. 'hex'));
  109. assert.equal(new Buffer(decoder.decodeStr()).toString(),
  110. 'https://www.example.com');
  111. });
  112. it('should decode many 5 bit chars', function() {
  113. // e = 00101, 0x294A5294A5 = 00101 x 8
  114. decoder.push(new Buffer('85294A5294A5', 'hex'));
  115. assert.equal(new Buffer(decoder.decodeStr()).toString(), 'eeeeeeee');
  116. });
  117. it('should decode many 5 bit chars with 3-bit EOS', function() {
  118. // e = 00101, EOS=111,
  119. // 0x294A5294A52F = 00101 x 9 + 111
  120. decoder.push(new Buffer('86294A5294A52F', 'hex'));
  121. assert.equal(new Buffer(decoder.decodeStr()).toString(), 'eeeeeeeee');
  122. });
  123. it('should decode many 5 bit chars with 2-bit EOS', function() {
  124. // e = 00101, EOS=11,
  125. // 0x294A5297 = 00101 x 6 + 11
  126. decoder.push(new Buffer('84294A5297', 'hex'));
  127. assert.equal(new Buffer(decoder.decodeStr()).toString(), 'eeeeee');
  128. });
  129. it('should decode many multi-octet chars', function() {
  130. decoder.push(new Buffer(
  131. '97ffffb1ffff63fffec7fffd8ffffb1ffff63fffec7fffd8',
  132. 'hex'));
  133. assert.deepEqual(decoder.decodeStr(), [
  134. 1, 1, 1, 1, 1, 1, 1, 1
  135. ]);
  136. });
  137. it('should fail on 8 bit EOS', function() {
  138. // e = 00101, 0x294A5294A5 = 00101 x 8
  139. decoder.push(new Buffer('86294A5294A5ff', 'hex'));
  140. assert.throws(function() {
  141. decoder.decodeStr();
  142. });
  143. });
  144. it('should fail on invalid 2-bit EOS', function() {
  145. // e = 00101, EOS=10,
  146. // 0x294A5297 = 00101 x 6 + 11
  147. decoder.push(new Buffer('84294A5296', 'hex'));
  148. assert.throws(function() {
  149. decoder.decodeStr();
  150. });
  151. });
  152. });
  153. });