michael@0: var assert = require('assert'), michael@0: spdy = require('../../'), michael@0: Buffer = require('buffer').Buffer, michael@0: Stream = require('stream').Stream; michael@0: michael@0: suite('A Framer of SPDY module', function() { michael@0: var inflate, michael@0: deflate, michael@0: framer; michael@0: michael@0: setup(function() { michael@0: inflate = spdy.utils.zwrap(spdy.utils.createInflate(2)); michael@0: deflate = spdy.utils.zwrap(spdy.utils.createDeflate(2)); michael@0: framer = new spdy.protocol[2].Framer(deflate, inflate); michael@0: }); michael@0: michael@0: /* michael@0: deflate.on('data', function(b) {console.log(b)}); michael@0: deflate.write(new Buffer([ michael@0: 0x00, 0x02, // Number of name+value michael@0: 0, 0x04, // Name length michael@0: 0x68, 0x6f, 0x73, 0x74, // 'host' michael@0: 0, 0x09, // Value length michael@0: 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, // 'localhost' michael@0: 0, 0x06, michael@0: 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, // 'custom', michael@0: 0, 0x1, michael@0: 0x31 // '1' michael@0: ])); michael@0: deflate.flush(); michael@0: */ michael@0: michael@0: suite('frame parsing', function() { michael@0: test('given a SYN_STREAM should return correct frame', function(done) { michael@0: var body = new Buffer([ michael@0: 0x00, 0x00, 0x00, 0x01, // Stream ID michael@0: 0x00, 0x00, 0x00, 0x00, // Associated Stream ID michael@0: 0x00, 0x00, // Priority + Unused michael@0: 0x78, 0xbb, 0xdf, 0xa2, 0x51, 0xb2, // Deflated Name/Value pairs michael@0: 0x62, 0x60, 0x62, 0x60, 0x01, 0xe5, 0x12, michael@0: 0x06, 0x4e, 0x50, 0x50, 0xe6, 0x80, 0x99, michael@0: 0x6c, 0xc9, 0xa5, 0xc5, 0x25, 0xf9, 0xb9, michael@0: 0x0c, 0x8c, 0x86, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff michael@0: ]); michael@0: framer.execute({ michael@0: control: true, michael@0: type: 1, michael@0: length: body.length michael@0: }, body, function(err, frame) { michael@0: assert.ok(!err); michael@0: assert.equal(frame.type, 'SYN_STREAM'); michael@0: assert.equal(frame.id, 1); michael@0: assert.equal(frame.associated, 0); michael@0: assert.equal(frame.headers.host, 'localhost'); michael@0: assert.equal(frame.headers.custom, '1'); michael@0: done(); michael@0: }); michael@0: }); michael@0: michael@0: test('given a SYN_REPLY should return correct frame', function(done) { michael@0: var body = new Buffer([ michael@0: 0x00, 0x00, 0x00, 0x01, // Stream ID michael@0: 0x00, 0x00, // Unused michael@0: 0x78, 0xbb, 0xdf, 0xa2, 0x51, 0xb2, // Deflated Name/Value pairs michael@0: 0x62, 0x60, 0x62, 0x60, 0x01, 0xe5, 0x12, michael@0: 0x06, 0x4e, 0x50, 0x50, 0xe6, 0x80, 0x99, michael@0: 0x6c, 0xc9, 0xa5, 0xc5, 0x25, 0xf9, 0xb9, michael@0: 0x0c, 0x8c, 0x86, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff michael@0: ]); michael@0: framer.execute({ michael@0: control: true, michael@0: type: 2, michael@0: length: body.length michael@0: }, body, function(err, frame) { michael@0: assert.ok(!err); michael@0: assert.equal(frame.type, 'SYN_REPLY'); michael@0: assert.equal(frame.id, 1); michael@0: assert.equal(frame.headers.host, 'localhost'); michael@0: assert.equal(frame.headers.custom, '1'); michael@0: done(); michael@0: }); michael@0: }); michael@0: michael@0: test('given a RST_STREAM should return correct frame', function(done) { michael@0: var body = new Buffer([0, 0, 0, 1, 0, 0, 0, 2]); michael@0: framer.execute({ michael@0: control: true, michael@0: type: 3, michael@0: length: body.length michael@0: }, body, function(err, frame) { michael@0: assert.ok(!err); michael@0: assert.equal(frame.type, 'RST_STREAM'); michael@0: assert.equal(frame.id, 1); michael@0: assert.equal(frame.status, 2); michael@0: done(); michael@0: }); michael@0: }); michael@0: michael@0: test('given a NOOP frame should return correct frame', function(done) { michael@0: framer.execute({ michael@0: control: true, michael@0: type: 5, michael@0: length: 0 michael@0: }, new Buffer(0), function(err, frame) { michael@0: assert.ok(!err); michael@0: assert.equal(frame.type, 'NOOP'); michael@0: done(); michael@0: }); michael@0: }); michael@0: michael@0: test('given a PING frame should return correct frame', function(done) { michael@0: framer.execute({ michael@0: control: true, michael@0: type: 6, michael@0: length: 0 michael@0: }, new Buffer(0), function(err, frame) { michael@0: assert.ok(!err); michael@0: assert.equal(frame.type, 'PING'); michael@0: done(); michael@0: }); michael@0: }); michael@0: michael@0: test('given a GOAWAY frame should return correct frame', function(done) { michael@0: var body = new Buffer([0, 0, 0, 1]); michael@0: framer.execute({ michael@0: control: true, michael@0: type: 7, michael@0: length: body.length michael@0: }, body, function(err, frame) { michael@0: assert.ok(!err); michael@0: assert.equal(frame.type, 'GOAWAY'); michael@0: assert.equal(frame.lastId, 1); michael@0: done(); michael@0: }); michael@0: }); michael@0: }); michael@0: michael@0: suite('frame generation', function() { michael@0: test('.replyFrame() should generate correct frame', function(done) { michael@0: framer.replyFrame(1, 200, 'ok', {}, function(err, chunks) { michael@0: assert.equal(err, null); michael@0: assert.ok(chunks.length > 1); michael@0: done(); michael@0: }); michael@0: }); michael@0: michael@0: test('.streamFrame() should generate correct frame', function(done) { michael@0: framer.streamFrame(2, 1, { url : '/' }, {}, function(err, chunks) { michael@0: assert.equal(err, null); michael@0: assert.ok(chunks.length > 1); michael@0: done(); michael@0: }); michael@0: }); michael@0: michael@0: test('.dataFrame() w/o fin should generate correct frame', function() { michael@0: var frame = framer.dataFrame(1, false, new Buffer(123)); michael@0: assert.equal(frame[4], 0); michael@0: assert.ok(frame.length > 8); michael@0: }); michael@0: michael@0: test('.dataFrame() with fin should generate correct frame', function() { michael@0: var frame = framer.dataFrame(1, true, new Buffer(123)); michael@0: assert.equal(frame[4], 1); michael@0: assert.ok(frame.length > 8); michael@0: }); michael@0: michael@0: test('.pingFrame() should generate correct frame', function() { michael@0: var frame = framer.pingFrame(new Buffer([0, 1, 2, 3])); michael@0: assert.ok(frame.length > 0); michael@0: }); michael@0: michael@0: test('.rstFrame() should generate correct frame', function() { michael@0: var frame = framer.rstFrame(1, 2); michael@0: assert.ok(frame.length > 0); michael@0: michael@0: // Verify that cache works michael@0: var frame = framer.rstFrame(1, 2); michael@0: assert.ok(frame.length > 0); michael@0: }); michael@0: }); michael@0: });