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