Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | var expect = require('chai').expect; |
michael@0 | 2 | var util = require('./util'); |
michael@0 | 3 | |
michael@0 | 4 | var compressor = require('../lib/compressor'); |
michael@0 | 5 | var HeaderTable = compressor.HeaderTable; |
michael@0 | 6 | var HuffmanTable = compressor.HuffmanTable; |
michael@0 | 7 | var HeaderSetCompressor = compressor.HeaderSetCompressor; |
michael@0 | 8 | var HeaderSetDecompressor = compressor.HeaderSetDecompressor; |
michael@0 | 9 | var Compressor = compressor.Compressor; |
michael@0 | 10 | var Decompressor = compressor.Decompressor; |
michael@0 | 11 | |
michael@0 | 12 | var test_integers = [{ |
michael@0 | 13 | N: 5, |
michael@0 | 14 | I: 10, |
michael@0 | 15 | buffer: new Buffer([10]) |
michael@0 | 16 | }, { |
michael@0 | 17 | N: 0, |
michael@0 | 18 | I: 10, |
michael@0 | 19 | buffer: new Buffer([10]) |
michael@0 | 20 | }, { |
michael@0 | 21 | N: 5, |
michael@0 | 22 | I: 1337, |
michael@0 | 23 | buffer: new Buffer([31, 128 + 26, 10]) |
michael@0 | 24 | }, { |
michael@0 | 25 | N: 0, |
michael@0 | 26 | I: 1337, |
michael@0 | 27 | buffer: new Buffer([128 + 57, 10]) |
michael@0 | 28 | }]; |
michael@0 | 29 | |
michael@0 | 30 | var test_strings = [{ |
michael@0 | 31 | string: 'www.foo.com', |
michael@0 | 32 | buffer: new Buffer('88db6d898b5a44b74f', 'hex') |
michael@0 | 33 | }, { |
michael@0 | 34 | string: 'éáűőúöüó€', |
michael@0 | 35 | buffer: new Buffer('13C3A9C3A1C5B1C591C3BAC3B6C3BCC3B3E282AC', 'hex') |
michael@0 | 36 | }]; |
michael@0 | 37 | |
michael@0 | 38 | test_huffman_request = { |
michael@0 | 39 | 'GET': 'f77778ff', |
michael@0 | 40 | 'http': 'ce3177', |
michael@0 | 41 | '/': '0f', |
michael@0 | 42 | 'www.foo.com': 'db6d898b5a44b74f', |
michael@0 | 43 | 'https': 'ce31743f', |
michael@0 | 44 | 'www.bar.com': 'db6d897a1e44b74f', |
michael@0 | 45 | 'no-cache': '63654a1398ff', |
michael@0 | 46 | '/custom-path.css': '04eb08b7495c88e644c21f', |
michael@0 | 47 | 'custom-key': '4eb08b749790fa7f', |
michael@0 | 48 | 'custom-value': '4eb08b74979a17a8ff' |
michael@0 | 49 | }; |
michael@0 | 50 | |
michael@0 | 51 | test_huffman_response = { |
michael@0 | 52 | '302': '98a7', |
michael@0 | 53 | 'private': '73d5cd111f', |
michael@0 | 54 | 'Mon, 21 OCt 2013 20:13:21 GMT': 'ef6b3a7a0e6e8fa7647a0e534dd072fb0d37b0e6e8f777f8ff', |
michael@0 | 55 | ': https://www.bar.com': 'f6746718ba1ec00db6d897a1e44b74', |
michael@0 | 56 | '200': '394b', |
michael@0 | 57 | 'Mon, 21 OCt 2013 20:13:22 GMT': 'ef6b3a7a0e6e8fa7647a0e534dd072fb0d37b0e7e8f777f8ff', |
michael@0 | 58 | 'https://www.bar.com': 'ce31743d801b6db12f43c896e9', |
michael@0 | 59 | 'gzip': 'cbd54e', |
michael@0 | 60 | 'foo=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\ |
michael@0 | 61 | AAAAAAAAAAAAAAAAAAAAAAAAAALASDJKHQKBZXOQWEOPIUAXQWEOIUAXLJKHQWOEIUAL\ |
michael@0 | 62 | QWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234LASDJKHQKBZXOQWEOPIUAXQWEOIUAXLJKH\ |
michael@0 | 63 | QWOEIUALQWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234LASDJKHQKBZXOQWEOPIUAXQWEO\ |
michael@0 | 64 | IUAXLJKHQWOEIUALQWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234LASDJKHQKBZXOQWEOP\ |
michael@0 | 65 | IUAXQWEOIUAXLJKHQWOEIUALQWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234ZZZZZZZZZZ\ |
michael@0 | 66 | ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ1234 m\ |
michael@0 | 67 | ax-age=3600; version=1': 'c5adb77efdfbf7efdfbf7efdfbf7efdfbf7efdfbf7\ |
michael@0 | 68 | efdfbf7efdfbf7efdfbf7efdfbf7efdfbf7efdfbf7efdfbf7efdfbf7efdfbf7efdfb\ |
michael@0 | 69 | f7efdfbf7efdfbf7efdfbfe5bfc3b7e3fdfbfedfdf5ff9fbfa7dbf5ddf4fafc3f1bf\ |
michael@0 | 70 | f7f6fd777d3e1f8dffbf97c7fbf7fdbf5f4eef87e37fcbedfaeefa7c3f1bff7f2fb7\ |
michael@0 | 71 | 77e37fefe5f2fefe3bfc3b7edfaeefa7e3e1bff7f331e69fe5bfc3b7e3fdfbfedfdf\ |
michael@0 | 72 | 5ff9fbfa7dbf5ddf4fafc3f1bff7f6fd777d3e1f8dffbf97c7fbf7fdbf5f4eef87e3\ |
michael@0 | 73 | 7fcbedfaeefa7c3f1bff7f2fb777e37fefe5f2fefe3bfc3b7edfaeefa7e3e1bff7f3\ |
michael@0 | 74 | 31e69fe5bfc3b7e3fdfbfedfdf5ff9fbfa7dbf5ddf4fafc3f1bff7f6fd777d3e1f8d\ |
michael@0 | 75 | ffbf97c7fbf7fdbf5f4eef87e37fcbedfaeefa7c3f1bff7f2fb777e37fefe5f2fefe\ |
michael@0 | 76 | 3bfc3b7edfaeefa7e3e1bff7f331e69fe5bfc3b7e3fdfbfedfdf5ff9fbfa7dbf5ddf\ |
michael@0 | 77 | 4fafc3f1bff7f6fd777d3e1f8dffbf97c7fbf7fdbf5f4eef87e37fcbedfaeefa7c3f\ |
michael@0 | 78 | 1bff7f2fb777e37fefe5f2fefe3bfc3b7edfaeefa7e3e1bff7f331e69ffcff3fcff3\ |
michael@0 | 79 | fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcf\ |
michael@0 | 80 | f3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3f\ |
michael@0 | 81 | cff3fcff3fcff3fcff3fcff3fcff3fcff0c79a7e8d11e72a321b66a4a5eae8e62f82\ |
michael@0 | 82 | 9acb4d', |
michael@0 | 83 | 'foo=ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ\ |
michael@0 | 84 | ZZZZZZZZZZZZZZZZZZZZZZZZZZLASDJKHQKBZXOQWEOPIUAXQWEOIUAXLJKHQWOEIUAL\ |
michael@0 | 85 | QWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234LASDJKHQKBZXOQWEOPIUAXQWEOIUAXLJKH\ |
michael@0 | 86 | QWOEIUALQWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234LASDJKHQKBZXOQWEOPIUAXQWEO\ |
michael@0 | 87 | IUAXLJKHQWOEIUALQWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234LASDJKHQKBZXOQWEOP\ |
michael@0 | 88 | IUAXQWEOIUAXLJKHQWOEIUALQWEOIUAXLQEUAXLLKJASDQWEOUIAXN1234AAAAAAAAAA\ |
michael@0 | 89 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA1234 m\ |
michael@0 | 90 | ax-age=3600; version=1': 'c5adb7fcff3fcff3fcff3fcff3fcff3fcff3fcff3f\ |
michael@0 | 91 | cff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff\ |
michael@0 | 92 | 3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fcff3fc\ |
michael@0 | 93 | ff3fcff3e5bfc3b7e3fdfbfedfdf5ff9fbfa7dbf5ddf4fafc3f1bff7f6fd777d3e1f\ |
michael@0 | 94 | 8dffbf97c7fbf7fdbf5f4eef87e37fcbedfaeefa7c3f1bff7f2fb777e37fefe5f2fe\ |
michael@0 | 95 | fe3bfc3b7edfaeefa7e3e1bff7f331e69fe5bfc3b7e3fdfbfedfdf5ff9fbfa7dbf5d\ |
michael@0 | 96 | df4fafc3f1bff7f6fd777d3e1f8dffbf97c7fbf7fdbf5f4eef87e37fcbedfaeefa7c\ |
michael@0 | 97 | 3f1bff7f2fb777e37fefe5f2fefe3bfc3b7edfaeefa7e3e1bff7f331e69fe5bfc3b7\ |
michael@0 | 98 | e3fdfbfedfdf5ff9fbfa7dbf5ddf4fafc3f1bff7f6fd777d3e1f8dffbf97c7fbf7fd\ |
michael@0 | 99 | bf5f4eef87e37fcbedfaeefa7c3f1bff7f2fb777e37fefe5f2fefe3bfc3b7edfaeef\ |
michael@0 | 100 | a7e3e1bff7f331e69fe5bfc3b7e3fdfbfedfdf5ff9fbfa7dbf5ddf4fafc3f1bff7f6\ |
michael@0 | 101 | fd777d3e1f8dffbf97c7fbf7fdbf5f4eef87e37fcbedfaeefa7c3f1bff7f2fb777e3\ |
michael@0 | 102 | 7fefe5f2fefe3bfc3b7edfaeefa7e3e1bff7f331e69f7efdfbf7efdfbf7efdfbf7ef\ |
michael@0 | 103 | dfbf7efdfbf7efdfbf7efdfbf7efdfbf7efdfbf7efdfbf7efdfbf7efdfbf7efdfbf7\ |
michael@0 | 104 | efdfbf7efdfbf7efdfbf7efdfbf7efdfbcc79a7e8d11e72a321b66a4a5eae8e62f82\ |
michael@0 | 105 | 9acb4d' |
michael@0 | 106 | }; |
michael@0 | 107 | |
michael@0 | 108 | var test_headers = [{ |
michael@0 | 109 | header: { |
michael@0 | 110 | name: 1, |
michael@0 | 111 | value: 'GET', |
michael@0 | 112 | index: true |
michael@0 | 113 | }, |
michael@0 | 114 | buffer: new Buffer('02' + '03474554', 'hex') |
michael@0 | 115 | }, { |
michael@0 | 116 | header: { |
michael@0 | 117 | name: 6, |
michael@0 | 118 | value: 'http', |
michael@0 | 119 | index: true |
michael@0 | 120 | }, |
michael@0 | 121 | buffer: new Buffer('07' + '83ce3177', 'hex') |
michael@0 | 122 | }, { |
michael@0 | 123 | header: { |
michael@0 | 124 | name: 5, |
michael@0 | 125 | value: '/', |
michael@0 | 126 | index: true |
michael@0 | 127 | }, |
michael@0 | 128 | buffer: new Buffer('06' + '012f', 'hex') |
michael@0 | 129 | }, { |
michael@0 | 130 | header: { |
michael@0 | 131 | name: 3, |
michael@0 | 132 | value: 'www.foo.com', |
michael@0 | 133 | index: true |
michael@0 | 134 | }, |
michael@0 | 135 | buffer: new Buffer('04' + '88db6d898b5a44b74f', 'hex') |
michael@0 | 136 | }, { |
michael@0 | 137 | header: { |
michael@0 | 138 | name: 2, |
michael@0 | 139 | value: 'https', |
michael@0 | 140 | index: true |
michael@0 | 141 | }, |
michael@0 | 142 | buffer: new Buffer('03' + '84ce31743f', 'hex') |
michael@0 | 143 | }, { |
michael@0 | 144 | header: { |
michael@0 | 145 | name: 1, |
michael@0 | 146 | value: 'www.bar.com', |
michael@0 | 147 | index: true |
michael@0 | 148 | }, |
michael@0 | 149 | buffer: new Buffer('02' + '88db6d897a1e44b74f', 'hex') |
michael@0 | 150 | }, { |
michael@0 | 151 | header: { |
michael@0 | 152 | name: 28, |
michael@0 | 153 | value: 'no-cache', |
michael@0 | 154 | index: true |
michael@0 | 155 | }, |
michael@0 | 156 | buffer: new Buffer('1d' + '8663654a1398ff', 'hex') |
michael@0 | 157 | }, { |
michael@0 | 158 | header: { |
michael@0 | 159 | name: 3, |
michael@0 | 160 | value: 3, |
michael@0 | 161 | index: false |
michael@0 | 162 | }, |
michael@0 | 163 | buffer: new Buffer('84', 'hex') |
michael@0 | 164 | }, { |
michael@0 | 165 | header: { |
michael@0 | 166 | name: 5, |
michael@0 | 167 | value: 5, |
michael@0 | 168 | index: false |
michael@0 | 169 | }, |
michael@0 | 170 | buffer: new Buffer('86', 'hex') |
michael@0 | 171 | }, { |
michael@0 | 172 | header: { |
michael@0 | 173 | name: 4, |
michael@0 | 174 | value: '/custom-path.css', |
michael@0 | 175 | index: true |
michael@0 | 176 | }, |
michael@0 | 177 | buffer: new Buffer('05' + '8b04eb08b7495c88e644c21f', 'hex') |
michael@0 | 178 | }, { |
michael@0 | 179 | header: { |
michael@0 | 180 | name: 'custom-key', |
michael@0 | 181 | value: 'custom-value', |
michael@0 | 182 | index: true |
michael@0 | 183 | }, |
michael@0 | 184 | buffer: new Buffer('00' + '884eb08b749790fa7f' + '894eb08b74979a17a8ff', 'hex') |
michael@0 | 185 | }, { |
michael@0 | 186 | header: { |
michael@0 | 187 | name: 2, |
michael@0 | 188 | value: 2, |
michael@0 | 189 | index: false |
michael@0 | 190 | }, |
michael@0 | 191 | buffer: new Buffer('83', 'hex') |
michael@0 | 192 | }, { |
michael@0 | 193 | header: { |
michael@0 | 194 | name: 6, |
michael@0 | 195 | value: 6, |
michael@0 | 196 | index: false |
michael@0 | 197 | }, |
michael@0 | 198 | buffer: new Buffer('87', 'hex') |
michael@0 | 199 | }, { |
michael@0 | 200 | header: { |
michael@0 | 201 | name: -1, |
michael@0 | 202 | value: -1, |
michael@0 | 203 | index: true |
michael@0 | 204 | }, |
michael@0 | 205 | buffer: new Buffer('8080', 'hex') |
michael@0 | 206 | }]; |
michael@0 | 207 | |
michael@0 | 208 | var test_header_sets = [{ |
michael@0 | 209 | headers: { |
michael@0 | 210 | ':method': 'GET', |
michael@0 | 211 | ':scheme': 'http', |
michael@0 | 212 | ':path': '/', |
michael@0 | 213 | ':authority': 'www.foo.com' |
michael@0 | 214 | }, |
michael@0 | 215 | buffer: util.concat(test_headers.slice(0, 4).map(function(test) { return test.buffer; })) |
michael@0 | 216 | }, { |
michael@0 | 217 | headers: { |
michael@0 | 218 | ':method': 'GET', |
michael@0 | 219 | ':scheme': 'https', |
michael@0 | 220 | ':path': '/', |
michael@0 | 221 | ':authority': 'www.bar.com', |
michael@0 | 222 | 'cache-control': 'no-cache' |
michael@0 | 223 | }, |
michael@0 | 224 | buffer: util.concat(test_headers.slice(4, 9).map(function(test) { return test.buffer; })) |
michael@0 | 225 | }, { |
michael@0 | 226 | headers: { |
michael@0 | 227 | ':method': 'GET', |
michael@0 | 228 | ':scheme': 'https', |
michael@0 | 229 | ':path': '/custom-path.css', |
michael@0 | 230 | ':authority': 'www.bar.com', |
michael@0 | 231 | 'custom-key': 'custom-value' |
michael@0 | 232 | }, |
michael@0 | 233 | buffer: util.concat(test_headers.slice(9, 13).map(function(test) { return test.buffer; })) |
michael@0 | 234 | }, { |
michael@0 | 235 | headers: { |
michael@0 | 236 | ':method': 'GET', |
michael@0 | 237 | ':scheme': 'https', |
michael@0 | 238 | ':path': '/custom-path.css', |
michael@0 | 239 | ':authority': ['www.foo.com', 'www.bar.com'], |
michael@0 | 240 | 'custom-key': 'custom-value' |
michael@0 | 241 | }, |
michael@0 | 242 | buffer: test_headers[3].buffer |
michael@0 | 243 | }, { |
michael@0 | 244 | headers: {}, |
michael@0 | 245 | buffer: test_headers[13].buffer |
michael@0 | 246 | }, { |
michael@0 | 247 | headers: { |
michael@0 | 248 | ':status': '200', |
michael@0 | 249 | 'user-agent': 'my-user-agent', |
michael@0 | 250 | 'cookie': 'first; second; third; third; fourth', |
michael@0 | 251 | 'multiple': ['first', 'second', 'third', 'third; fourth'], |
michael@0 | 252 | 'verylong': (new Buffer(9000)).toString('hex') |
michael@0 | 253 | } |
michael@0 | 254 | }]; |
michael@0 | 255 | |
michael@0 | 256 | describe('compressor.js', function() { |
michael@0 | 257 | describe('HeaderTable', function() { |
michael@0 | 258 | }); |
michael@0 | 259 | |
michael@0 | 260 | describe('HuffmanTable', function() { |
michael@0 | 261 | describe('method encode(buffer)', function() { |
michael@0 | 262 | it('should return the Huffman encoded version of the input buffer', function() { |
michael@0 | 263 | var table = HuffmanTable.huffmanTable; |
michael@0 | 264 | for (var decoded in test_huffman_request) { |
michael@0 | 265 | var encoded = test_huffman_request[decoded]; |
michael@0 | 266 | expect(table.encode(new Buffer(decoded)).toString('hex')).to.equal(encoded); |
michael@0 | 267 | } |
michael@0 | 268 | table = HuffmanTable.huffmanTable; |
michael@0 | 269 | for (decoded in test_huffman_response) { |
michael@0 | 270 | encoded = test_huffman_response[decoded]; |
michael@0 | 271 | expect(table.encode(new Buffer(decoded)).toString('hex')).to.equal(encoded); |
michael@0 | 272 | } |
michael@0 | 273 | }); |
michael@0 | 274 | }) |
michael@0 | 275 | describe('method decode(buffer)', function() { |
michael@0 | 276 | it('should return the Huffman decoded version of the input buffer', function() { |
michael@0 | 277 | var table = HuffmanTable.huffmanTable; |
michael@0 | 278 | for (var decoded in test_huffman_request) { |
michael@0 | 279 | var encoded = test_huffman_request[decoded]; |
michael@0 | 280 | expect(table.decode(new Buffer(encoded, 'hex')).toString()).to.equal(decoded) |
michael@0 | 281 | } |
michael@0 | 282 | table = HuffmanTable.huffmanTable; |
michael@0 | 283 | for (decoded in test_huffman_response) { |
michael@0 | 284 | encoded = test_huffman_response[decoded]; |
michael@0 | 285 | expect(table.decode(new Buffer(encoded, 'hex')).toString()).to.equal(decoded) |
michael@0 | 286 | } |
michael@0 | 287 | }); |
michael@0 | 288 | }) |
michael@0 | 289 | }); |
michael@0 | 290 | |
michael@0 | 291 | describe('HeaderSetCompressor', function() { |
michael@0 | 292 | describe('static method .integer(I, N)', function() { |
michael@0 | 293 | it('should return an array of buffers that represent the N-prefix coded form of the integer I', function() { |
michael@0 | 294 | for (var i = 0; i < test_integers.length; i++) { |
michael@0 | 295 | var test = test_integers[i]; |
michael@0 | 296 | test.buffer.cursor = 0; |
michael@0 | 297 | expect(util.concat(HeaderSetCompressor.integer(test.I, test.N))).to.deep.equal(test.buffer); |
michael@0 | 298 | } |
michael@0 | 299 | }); |
michael@0 | 300 | }); |
michael@0 | 301 | describe('static method .string(string)', function() { |
michael@0 | 302 | it('should return an array of buffers that represent the encoded form of the string', function() { |
michael@0 | 303 | var table = HuffmanTable.huffmanTable; |
michael@0 | 304 | for (var i = 0; i < test_strings.length; i++) { |
michael@0 | 305 | var test = test_strings[i]; |
michael@0 | 306 | expect(util.concat(HeaderSetCompressor.string(test.string, table))).to.deep.equal(test.buffer); |
michael@0 | 307 | } |
michael@0 | 308 | }); |
michael@0 | 309 | }); |
michael@0 | 310 | describe('static method .header({ name, value, index })', function() { |
michael@0 | 311 | it('should return an array of buffers that represent the encoded form of the header', function() { |
michael@0 | 312 | var table = HuffmanTable.huffmanTable; |
michael@0 | 313 | for (var i = 0; i < test_headers.length; i++) { |
michael@0 | 314 | var test = test_headers[i]; |
michael@0 | 315 | expect(util.concat(HeaderSetCompressor.header(test.header, table))).to.deep.equal(test.buffer); |
michael@0 | 316 | } |
michael@0 | 317 | }); |
michael@0 | 318 | }); |
michael@0 | 319 | }); |
michael@0 | 320 | |
michael@0 | 321 | describe('HeaderSetDecompressor', function() { |
michael@0 | 322 | describe('static method .integer(buffer, N)', function() { |
michael@0 | 323 | it('should return the parsed N-prefix coded number and increase the cursor property of buffer', function() { |
michael@0 | 324 | for (var i = 0; i < test_integers.length; i++) { |
michael@0 | 325 | var test = test_integers[i]; |
michael@0 | 326 | test.buffer.cursor = 0; |
michael@0 | 327 | expect(HeaderSetDecompressor.integer(test.buffer, test.N)).to.equal(test.I); |
michael@0 | 328 | expect(test.buffer.cursor).to.equal(test.buffer.length); |
michael@0 | 329 | } |
michael@0 | 330 | }); |
michael@0 | 331 | }); |
michael@0 | 332 | describe('static method .string(buffer)', function() { |
michael@0 | 333 | it('should return the parsed string and increase the cursor property of buffer', function() { |
michael@0 | 334 | var table = HuffmanTable.huffmanTable; |
michael@0 | 335 | for (var i = 0; i < test_strings.length; i++) { |
michael@0 | 336 | var test = test_strings[i]; |
michael@0 | 337 | test.buffer.cursor = 0; |
michael@0 | 338 | expect(HeaderSetDecompressor.string(test.buffer, table)).to.equal(test.string); |
michael@0 | 339 | expect(test.buffer.cursor).to.equal(test.buffer.length); |
michael@0 | 340 | } |
michael@0 | 341 | }); |
michael@0 | 342 | }); |
michael@0 | 343 | describe('static method .header(buffer)', function() { |
michael@0 | 344 | it('should return the parsed header and increase the cursor property of buffer', function() { |
michael@0 | 345 | var table = HuffmanTable.huffmanTable; |
michael@0 | 346 | for (var i = 0; i < test_headers.length; i++) { |
michael@0 | 347 | var test = test_headers[i]; |
michael@0 | 348 | test.buffer.cursor = 0; |
michael@0 | 349 | expect(HeaderSetDecompressor.header(test.buffer, table)).to.deep.equal(test.header); |
michael@0 | 350 | expect(test.buffer.cursor).to.equal(test.buffer.length); |
michael@0 | 351 | } |
michael@0 | 352 | }); |
michael@0 | 353 | }); |
michael@0 | 354 | }); |
michael@0 | 355 | describe('Decompressor', function() { |
michael@0 | 356 | describe('method decompress(buffer)', function() { |
michael@0 | 357 | it('should return the parsed header set in { name1: value1, name2: [value2, value3], ... } format', function() { |
michael@0 | 358 | var decompressor = new Decompressor(util.log, 'REQUEST'); |
michael@0 | 359 | for (var i = 0; i < 5; i++) { |
michael@0 | 360 | var header_set = test_header_sets[i]; |
michael@0 | 361 | expect(decompressor.decompress(header_set.buffer)).to.deep.equal(header_set.headers); |
michael@0 | 362 | } |
michael@0 | 363 | }); |
michael@0 | 364 | }); |
michael@0 | 365 | describe('transform stream', function() { |
michael@0 | 366 | it('should emit an error event if a series of header frames is interleaved with other frames', function() { |
michael@0 | 367 | var decompressor = new Decompressor(util.log, 'REQUEST'); |
michael@0 | 368 | var error_occured = false; |
michael@0 | 369 | decompressor.on('error', function() { |
michael@0 | 370 | error_occured = true; |
michael@0 | 371 | }); |
michael@0 | 372 | decompressor.write({ |
michael@0 | 373 | type: 'HEADERS', |
michael@0 | 374 | flags: { |
michael@0 | 375 | END_HEADERS: false |
michael@0 | 376 | }, |
michael@0 | 377 | data: new Buffer(5) |
michael@0 | 378 | }); |
michael@0 | 379 | decompressor.write({ |
michael@0 | 380 | type: 'DATA', |
michael@0 | 381 | flags: {}, |
michael@0 | 382 | data: new Buffer(5) |
michael@0 | 383 | }); |
michael@0 | 384 | expect(error_occured).to.be.equal(true); |
michael@0 | 385 | }); |
michael@0 | 386 | }); |
michael@0 | 387 | }); |
michael@0 | 388 | |
michael@0 | 389 | describe('invariant', function() { |
michael@0 | 390 | describe('decompressor.decompress(compressor.compress(headerset)) === headerset', function() { |
michael@0 | 391 | it('should be true for any header set if the states are synchronized', function() { |
michael@0 | 392 | var compressor = new Compressor(util.log, 'REQUEST'); |
michael@0 | 393 | var decompressor = new Decompressor(util.log, 'REQUEST'); |
michael@0 | 394 | var n = test_header_sets.length; |
michael@0 | 395 | for (var i = 0; i < 10; i++) { |
michael@0 | 396 | var headers = test_header_sets[i%n].headers; |
michael@0 | 397 | var compressed = compressor.compress(headers); |
michael@0 | 398 | var decompressed = decompressor.decompress(compressed); |
michael@0 | 399 | expect(decompressed).to.deep.equal(headers); |
michael@0 | 400 | expect(compressor._table).to.deep.equal(decompressor._table); |
michael@0 | 401 | } |
michael@0 | 402 | }); |
michael@0 | 403 | }); |
michael@0 | 404 | describe('source.pipe(compressor).pipe(decompressor).pipe(destination)', function() { |
michael@0 | 405 | it('should behave like source.pipe(destination) for a stream of frames', function(done) { |
michael@0 | 406 | var compressor = new Compressor(util.log, 'RESPONSE'); |
michael@0 | 407 | var decompressor = new Decompressor(util.log, 'RESPONSE'); |
michael@0 | 408 | var n = test_header_sets.length; |
michael@0 | 409 | compressor.pipe(decompressor); |
michael@0 | 410 | for (var i = 0; i < 10; i++) { |
michael@0 | 411 | compressor.write({ |
michael@0 | 412 | type: i%2 ? 'HEADERS' : 'PUSH_PROMISE', |
michael@0 | 413 | flags: {}, |
michael@0 | 414 | headers: test_header_sets[i%n].headers |
michael@0 | 415 | }); |
michael@0 | 416 | } |
michael@0 | 417 | setTimeout(function() { |
michael@0 | 418 | for (var j = 0; j < 10; j++) { |
michael@0 | 419 | expect(decompressor.read().headers).to.deep.equal(test_header_sets[j%n].headers); |
michael@0 | 420 | } |
michael@0 | 421 | done(); |
michael@0 | 422 | }, 10); |
michael@0 | 423 | }); |
michael@0 | 424 | }); |
michael@0 | 425 | describe('huffmanTable.decompress(huffmanTable.compress(buffer)) === buffer', function() { |
michael@0 | 426 | it('should be true for any buffer', function() { |
michael@0 | 427 | for (var i = 0; i < 10; i++) { |
michael@0 | 428 | var buffer = []; |
michael@0 | 429 | while (Math.random() > 0.1) { |
michael@0 | 430 | buffer.push(Math.floor(Math.random() * 256)) |
michael@0 | 431 | } |
michael@0 | 432 | buffer = new Buffer(buffer); |
michael@0 | 433 | var table = HuffmanTable.huffmanTable; |
michael@0 | 434 | var result = table.decode(table.encode(buffer)); |
michael@0 | 435 | expect(result).to.deep.equal(buffer); |
michael@0 | 436 | } |
michael@0 | 437 | }) |
michael@0 | 438 | }) |
michael@0 | 439 | }); |
michael@0 | 440 | }); |