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