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