Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 var assert = require('assert'),
2 spdy = require('../../'),
3 keys = require('../fixtures/keys'),
4 https = require('https'),
5 tls = require('tls'),request
6 Buffer = require('buffer').Buffer;
8 suite('A SPDY Server', function() {
9 var server;
10 setup(function(done) {
11 server = spdy.createServer(keys, function(req, res) {
12 res.end('ok');
13 });
15 server.listen(8081, done);
16 });
18 teardown(function(done) {
19 server.once('close', done);
20 server.close();
21 });
23 test('should respond on regular https requests', function(done) {
24 https.request({
25 host: 'localhost',
26 port: 8081,
27 path: '/',
28 method: 'GET',
29 agent: false,
30 rejectUnauthorized: false
31 }, function(res) {
32 assert.equal(res.statusCode, 200);
33 done();
34 }).end();
35 });
37 test('should respond on spdy requests', function(done) {
38 var socket = tls.connect(
39 8081,
40 'localhost',
41 { NPNProtocols: ['spdy/2'], rejectUnauthorized: false },
42 function() {
43 var deflate = spdy.utils.createDeflate(2),
44 chunks = [],
45 length = 0;
47 deflate.on('data', function(chunk) {
48 chunks.push(chunk);
49 length += chunk.length;
50 });
52 // Deflate headers
53 deflate.write(new Buffer([
54 0x00, 0x04, // method, url, version = 3 fields
55 0x00, 0x06,
56 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, // method
57 0x00, 0x03,
58 0x47, 0x45, 0x54, // get
59 0x00, 0x03,
60 0x75, 0x72, 0x6c, // url
61 0x00, 0x01,
62 0x2f, // '/'
63 0x00, 0x07,
64 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, // version
65 0x00, 0x08,
66 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, // HTTP/1.1
67 0x00, 0x04,
68 0x68, 0x6f, 0x73, 0x74, // host
69 0x00, 0x09,
70 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74 //localhost
71 ]));
73 deflate.flush(function() {
74 // StreamID + Associated StreamID
75 length += 10;
77 // Headers
78 socket.write(new Buffer([
79 0x80, 0x02, 0x00, 0x01, // Control, Version, SYN_STREAM
80 0x00, 0x00, 0x00, length, // Flags, length (1 byte in this case)
81 0x00, 0x00, 0x00, 0x01, // StreamID
82 0x00, 0x00, 0x00, 0x00, // Associated StreamID
83 0x00, 0x00 // Priority + Unused
84 ]));
86 // Write compressed headers
87 chunks.forEach(function(chunk) {
88 socket.write(chunk);
89 });
90 });
92 var response = new Buffer(85),
93 offset = 0;
95 socket.on('data', function(chunk) {
96 assert.ok(offset + chunk.length <= 85);
98 chunk.copy(response, offset);
99 offset += chunk.length;
101 if (offset === 85) {
102 var frames = [];
104 offset = 0;
105 while (offset < response.length) {
106 var len = (response.readUInt32BE(offset + 4) & 0x00ffffff) + 8;
107 frames.push(response.slice(offset, offset + len));
109 offset += len;
110 }
112 // SYN_STREAM frame
113 assert.ok(frames.some(function(frame) {
114 return frame[0] === 0x80 && // Control frame
115 frame[1] === 0x02 && // Version
116 frame.readUInt16BE(2) === 0x0002 && // SYN_STREAM
117 frame.readUInt32BE(8) === 0x0001; // StreamID
118 }));
120 // Data frames
121 assert.ok(frames.some(function(frame) {
122 return frame[0] === 0x00 && // Data frame
123 frame.readUInt32BE(0) === 0x0001 && // StreamID
124 frame.slice(8).toString() === 'ok';
125 }));
127 socket.destroy();
128 }
129 });
131 socket.on('close', function() {
132 done();
133 });
134 }
135 );
137 socket.on('error', function(err) {
138 console.error('Socket error: ' + err);
139 });
141 server.on('request', function(req, res) {
142 assert.equal(req.url, '/');
143 assert.equal(req.method, 'GET');
144 res.end('ok');
145 });
146 });
147 });