Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | var assert = require('assert'), |
michael@0 | 2 | spdy = require('../../'), |
michael@0 | 3 | Buffer = require('buffer').Buffer; |
michael@0 | 4 | |
michael@0 | 5 | suite('A Parser of SPDY module', function() { |
michael@0 | 6 | var parser; |
michael@0 | 7 | |
michael@0 | 8 | [2,3].forEach(function(version) { |
michael@0 | 9 | suite('version ' + version, function() { |
michael@0 | 10 | setup(function() { |
michael@0 | 11 | var deflate = spdy.utils.createDeflate(version), |
michael@0 | 12 | inflate = spdy.utils.createInflate(version); |
michael@0 | 13 | |
michael@0 | 14 | parser = new spdy.parser.create({ |
michael@0 | 15 | socket: { |
michael@0 | 16 | setNoDelay: function() {} |
michael@0 | 17 | }, |
michael@0 | 18 | write: function() {} |
michael@0 | 19 | }, deflate, inflate); |
michael@0 | 20 | |
michael@0 | 21 | parser.createFramer(version); |
michael@0 | 22 | }); |
michael@0 | 23 | |
michael@0 | 24 | test('should wait for headers initially', function() { |
michael@0 | 25 | assert.equal(parser.waiting, 8); |
michael@0 | 26 | }); |
michael@0 | 27 | |
michael@0 | 28 | test('should update buffered property once given < 8 bytes', function() { |
michael@0 | 29 | parser.write(new Buffer(5)); |
michael@0 | 30 | assert.equal(parser.buffered, 5); |
michael@0 | 31 | }); |
michael@0 | 32 | |
michael@0 | 33 | test('given SYN_STREAM header should start waiting for body', function() { |
michael@0 | 34 | parser.write(new Buffer([ |
michael@0 | 35 | 0x80, 0x02, 0x00, 0x01, // Control frame, version, type (SYN_STREAM) |
michael@0 | 36 | 0x00, 0x00, 0x12, 0x34 |
michael@0 | 37 | ])); |
michael@0 | 38 | |
michael@0 | 39 | assert.equal(parser.waiting, 0x1234); |
michael@0 | 40 | assert.equal(parser.state.type, 'frame-body'); |
michael@0 | 41 | assert.ok(parser.state.header.control); |
michael@0 | 42 | assert.equal(parser.state.header.flags, 0); |
michael@0 | 43 | assert.equal(parser.state.header.length, 0x1234); |
michael@0 | 44 | }); |
michael@0 | 45 | |
michael@0 | 46 | test('given DATA header should start waiting for body', function() { |
michael@0 | 47 | parser.write(new Buffer([ |
michael@0 | 48 | 0x00, 0x00, 0x00, 0x01, // Data frame, stream ID |
michael@0 | 49 | 0x00, 0x00, 0x12, 0x34 |
michael@0 | 50 | ])); |
michael@0 | 51 | |
michael@0 | 52 | assert.equal(parser.waiting, 0x1234); |
michael@0 | 53 | assert.equal(parser.state.type, 'frame-body'); |
michael@0 | 54 | assert.ok(!parser.state.header.control); |
michael@0 | 55 | assert.equal(parser.state.header.id, 1); |
michael@0 | 56 | assert.equal(parser.state.header.flags, 0); |
michael@0 | 57 | assert.equal(parser.state.header.length, 0x1234); |
michael@0 | 58 | }); |
michael@0 | 59 | |
michael@0 | 60 | test('given chunked header should not fail', function() { |
michael@0 | 61 | parser.write(new Buffer([ |
michael@0 | 62 | 0x80, 0x02, 0x00, 0x01 // Control frame, version, type (SYN_STREAM) |
michael@0 | 63 | ])); |
michael@0 | 64 | assert.equal(parser.buffered, 4); |
michael@0 | 65 | |
michael@0 | 66 | parser.write(new Buffer([ |
michael@0 | 67 | 0x00, 0x00, 0x12, 0x34 |
michael@0 | 68 | ])); |
michael@0 | 69 | assert.equal(parser.buffered, 0); |
michael@0 | 70 | |
michael@0 | 71 | assert.equal(parser.waiting, 0x1234); |
michael@0 | 72 | assert.equal(parser.state.type, 'frame-body'); |
michael@0 | 73 | assert.ok(parser.state.header.control); |
michael@0 | 74 | assert.equal(parser.state.header.flags, 0); |
michael@0 | 75 | assert.equal(parser.state.header.length, 0x1234); |
michael@0 | 76 | }); |
michael@0 | 77 | |
michael@0 | 78 | test('given header and body should emit `frame`', function(done) { |
michael@0 | 79 | parser.on('frame', function(frame) { |
michael@0 | 80 | assert.ok(frame.type === 'DATA'); |
michael@0 | 81 | assert.equal(frame.id, 1); |
michael@0 | 82 | assert.equal(frame.data.length, 4); |
michael@0 | 83 | assert.equal(frame.data[0], 0x01); |
michael@0 | 84 | assert.equal(frame.data[1], 0x02); |
michael@0 | 85 | assert.equal(frame.data[2], 0x03); |
michael@0 | 86 | assert.equal(frame.data[3], 0x04); |
michael@0 | 87 | done(); |
michael@0 | 88 | }); |
michael@0 | 89 | |
michael@0 | 90 | parser.write(new Buffer([ |
michael@0 | 91 | 0x00, 0x00, 0x00, 0x01, // Data frame, stream ID |
michael@0 | 92 | 0x00, 0x00, 0x00, 0x04, |
michael@0 | 93 | 0x01, 0x02, 0x03, 0x04 // Body |
michael@0 | 94 | ])); |
michael@0 | 95 | |
michael@0 | 96 | // Waits for next frame |
michael@0 | 97 | assert.equal(parser.waiting, 8); |
michael@0 | 98 | assert.equal(parser.state.type, 'frame-head'); |
michael@0 | 99 | }); |
michael@0 | 100 | }); |
michael@0 | 101 | }); |
michael@0 | 102 | }); |