1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/xpcshell/node-spdy/test/unit/framer-test.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,181 @@ 1.4 +var assert = require('assert'), 1.5 + spdy = require('../../'), 1.6 + Buffer = require('buffer').Buffer, 1.7 + Stream = require('stream').Stream; 1.8 + 1.9 +suite('A Framer of SPDY module', function() { 1.10 + var inflate, 1.11 + deflate, 1.12 + framer; 1.13 + 1.14 + setup(function() { 1.15 + inflate = spdy.utils.zwrap(spdy.utils.createInflate(2)); 1.16 + deflate = spdy.utils.zwrap(spdy.utils.createDeflate(2)); 1.17 + framer = new spdy.protocol[2].Framer(deflate, inflate); 1.18 + }); 1.19 + 1.20 + /* 1.21 + deflate.on('data', function(b) {console.log(b)}); 1.22 + deflate.write(new Buffer([ 1.23 + 0x00, 0x02, // Number of name+value 1.24 + 0, 0x04, // Name length 1.25 + 0x68, 0x6f, 0x73, 0x74, // 'host' 1.26 + 0, 0x09, // Value length 1.27 + 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, // 'localhost' 1.28 + 0, 0x06, 1.29 + 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, // 'custom', 1.30 + 0, 0x1, 1.31 + 0x31 // '1' 1.32 + ])); 1.33 + deflate.flush(); 1.34 + */ 1.35 + 1.36 + suite('frame parsing', function() { 1.37 + test('given a SYN_STREAM should return correct frame', function(done) { 1.38 + var body = new Buffer([ 1.39 + 0x00, 0x00, 0x00, 0x01, // Stream ID 1.40 + 0x00, 0x00, 0x00, 0x00, // Associated Stream ID 1.41 + 0x00, 0x00, // Priority + Unused 1.42 + 0x78, 0xbb, 0xdf, 0xa2, 0x51, 0xb2, // Deflated Name/Value pairs 1.43 + 0x62, 0x60, 0x62, 0x60, 0x01, 0xe5, 0x12, 1.44 + 0x06, 0x4e, 0x50, 0x50, 0xe6, 0x80, 0x99, 1.45 + 0x6c, 0xc9, 0xa5, 0xc5, 0x25, 0xf9, 0xb9, 1.46 + 0x0c, 0x8c, 0x86, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff 1.47 + ]); 1.48 + framer.execute({ 1.49 + control: true, 1.50 + type: 1, 1.51 + length: body.length 1.52 + }, body, function(err, frame) { 1.53 + assert.ok(!err); 1.54 + assert.equal(frame.type, 'SYN_STREAM'); 1.55 + assert.equal(frame.id, 1); 1.56 + assert.equal(frame.associated, 0); 1.57 + assert.equal(frame.headers.host, 'localhost'); 1.58 + assert.equal(frame.headers.custom, '1'); 1.59 + done(); 1.60 + }); 1.61 + }); 1.62 + 1.63 + test('given a SYN_REPLY should return correct frame', function(done) { 1.64 + var body = new Buffer([ 1.65 + 0x00, 0x00, 0x00, 0x01, // Stream ID 1.66 + 0x00, 0x00, // Unused 1.67 + 0x78, 0xbb, 0xdf, 0xa2, 0x51, 0xb2, // Deflated Name/Value pairs 1.68 + 0x62, 0x60, 0x62, 0x60, 0x01, 0xe5, 0x12, 1.69 + 0x06, 0x4e, 0x50, 0x50, 0xe6, 0x80, 0x99, 1.70 + 0x6c, 0xc9, 0xa5, 0xc5, 0x25, 0xf9, 0xb9, 1.71 + 0x0c, 0x8c, 0x86, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff 1.72 + ]); 1.73 + framer.execute({ 1.74 + control: true, 1.75 + type: 2, 1.76 + length: body.length 1.77 + }, body, function(err, frame) { 1.78 + assert.ok(!err); 1.79 + assert.equal(frame.type, 'SYN_REPLY'); 1.80 + assert.equal(frame.id, 1); 1.81 + assert.equal(frame.headers.host, 'localhost'); 1.82 + assert.equal(frame.headers.custom, '1'); 1.83 + done(); 1.84 + }); 1.85 + }); 1.86 + 1.87 + test('given a RST_STREAM should return correct frame', function(done) { 1.88 + var body = new Buffer([0, 0, 0, 1, 0, 0, 0, 2]); 1.89 + framer.execute({ 1.90 + control: true, 1.91 + type: 3, 1.92 + length: body.length 1.93 + }, body, function(err, frame) { 1.94 + assert.ok(!err); 1.95 + assert.equal(frame.type, 'RST_STREAM'); 1.96 + assert.equal(frame.id, 1); 1.97 + assert.equal(frame.status, 2); 1.98 + done(); 1.99 + }); 1.100 + }); 1.101 + 1.102 + test('given a NOOP frame should return correct frame', function(done) { 1.103 + framer.execute({ 1.104 + control: true, 1.105 + type: 5, 1.106 + length: 0 1.107 + }, new Buffer(0), function(err, frame) { 1.108 + assert.ok(!err); 1.109 + assert.equal(frame.type, 'NOOP'); 1.110 + done(); 1.111 + }); 1.112 + }); 1.113 + 1.114 + test('given a PING frame should return correct frame', function(done) { 1.115 + framer.execute({ 1.116 + control: true, 1.117 + type: 6, 1.118 + length: 0 1.119 + }, new Buffer(0), function(err, frame) { 1.120 + assert.ok(!err); 1.121 + assert.equal(frame.type, 'PING'); 1.122 + done(); 1.123 + }); 1.124 + }); 1.125 + 1.126 + test('given a GOAWAY frame should return correct frame', function(done) { 1.127 + var body = new Buffer([0, 0, 0, 1]); 1.128 + framer.execute({ 1.129 + control: true, 1.130 + type: 7, 1.131 + length: body.length 1.132 + }, body, function(err, frame) { 1.133 + assert.ok(!err); 1.134 + assert.equal(frame.type, 'GOAWAY'); 1.135 + assert.equal(frame.lastId, 1); 1.136 + done(); 1.137 + }); 1.138 + }); 1.139 + }); 1.140 + 1.141 + suite('frame generation', function() { 1.142 + test('.replyFrame() should generate correct frame', function(done) { 1.143 + framer.replyFrame(1, 200, 'ok', {}, function(err, chunks) { 1.144 + assert.equal(err, null); 1.145 + assert.ok(chunks.length > 1); 1.146 + done(); 1.147 + }); 1.148 + }); 1.149 + 1.150 + test('.streamFrame() should generate correct frame', function(done) { 1.151 + framer.streamFrame(2, 1, { url : '/' }, {}, function(err, chunks) { 1.152 + assert.equal(err, null); 1.153 + assert.ok(chunks.length > 1); 1.154 + done(); 1.155 + }); 1.156 + }); 1.157 + 1.158 + test('.dataFrame() w/o fin should generate correct frame', function() { 1.159 + var frame = framer.dataFrame(1, false, new Buffer(123)); 1.160 + assert.equal(frame[4], 0); 1.161 + assert.ok(frame.length > 8); 1.162 + }); 1.163 + 1.164 + test('.dataFrame() with fin should generate correct frame', function() { 1.165 + var frame = framer.dataFrame(1, true, new Buffer(123)); 1.166 + assert.equal(frame[4], 1); 1.167 + assert.ok(frame.length > 8); 1.168 + }); 1.169 + 1.170 + test('.pingFrame() should generate correct frame', function() { 1.171 + var frame = framer.pingFrame(new Buffer([0, 1, 2, 3])); 1.172 + assert.ok(frame.length > 0); 1.173 + }); 1.174 + 1.175 + test('.rstFrame() should generate correct frame', function() { 1.176 + var frame = framer.rstFrame(1, 2); 1.177 + assert.ok(frame.length > 0); 1.178 + 1.179 + // Verify that cache works 1.180 + var frame = framer.rstFrame(1, 2); 1.181 + assert.ok(frame.length > 0); 1.182 + }); 1.183 + }); 1.184 +});