testing/xpcshell/node-spdy/test/unit/server-test.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/xpcshell/node-spdy/test/unit/server-test.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,147 @@
     1.4 +var assert = require('assert'),
     1.5 +    spdy = require('../../'),
     1.6 +    keys = require('../fixtures/keys'),
     1.7 +    https = require('https'),
     1.8 +    tls = require('tls'),request
     1.9 +    Buffer = require('buffer').Buffer;
    1.10 +
    1.11 +suite('A SPDY Server', function() {
    1.12 +  var server;
    1.13 +  setup(function(done) {
    1.14 +    server = spdy.createServer(keys, function(req, res) {
    1.15 +      res.end('ok');
    1.16 +    });
    1.17 +
    1.18 +    server.listen(8081, done);
    1.19 +  });
    1.20 +
    1.21 +  teardown(function(done) {
    1.22 +    server.once('close', done);
    1.23 +    server.close();
    1.24 +  });
    1.25 +
    1.26 +  test('should respond on regular https requests', function(done) {
    1.27 +    https.request({
    1.28 +      host: 'localhost',
    1.29 +      port: 8081,
    1.30 +      path: '/',
    1.31 +      method: 'GET',
    1.32 +      agent: false,
    1.33 +      rejectUnauthorized: false
    1.34 +    }, function(res) {
    1.35 +      assert.equal(res.statusCode, 200);
    1.36 +      done();
    1.37 +    }).end();
    1.38 +  });
    1.39 +
    1.40 +  test('should respond on spdy requests', function(done) {
    1.41 +    var socket = tls.connect(
    1.42 +      8081,
    1.43 +      'localhost',
    1.44 +      { NPNProtocols: ['spdy/2'], rejectUnauthorized: false },
    1.45 +      function() {
    1.46 +        var deflate = spdy.utils.createDeflate(2),
    1.47 +            chunks = [],
    1.48 +            length = 0;
    1.49 +
    1.50 +        deflate.on('data', function(chunk) {
    1.51 +          chunks.push(chunk);
    1.52 +          length += chunk.length;
    1.53 +        });
    1.54 +
    1.55 +        // Deflate headers
    1.56 +        deflate.write(new Buffer([
    1.57 +          0x00, 0x04, // method, url, version = 3 fields
    1.58 +          0x00, 0x06,
    1.59 +          0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64, // method
    1.60 +          0x00, 0x03,
    1.61 +          0x47, 0x45, 0x54, // get
    1.62 +          0x00, 0x03,
    1.63 +          0x75, 0x72, 0x6c, // url
    1.64 +          0x00, 0x01,
    1.65 +          0x2f, // '/'
    1.66 +          0x00, 0x07,
    1.67 +          0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, // version
    1.68 +          0x00, 0x08,
    1.69 +          0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, // HTTP/1.1
    1.70 +          0x00, 0x04,
    1.71 +          0x68, 0x6f, 0x73, 0x74, // host
    1.72 +          0x00, 0x09,
    1.73 +          0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74 //localhost
    1.74 +        ]));
    1.75 +
    1.76 +        deflate.flush(function() {
    1.77 +          // StreamID + Associated StreamID
    1.78 +          length += 10;
    1.79 +
    1.80 +          // Headers
    1.81 +          socket.write(new Buffer([
    1.82 +            0x80, 0x02, 0x00, 0x01, // Control, Version, SYN_STREAM
    1.83 +            0x00, 0x00, 0x00, length, // Flags, length (1 byte in this case)
    1.84 +            0x00, 0x00, 0x00, 0x01, // StreamID
    1.85 +            0x00, 0x00, 0x00, 0x00, // Associated StreamID
    1.86 +            0x00, 0x00 // Priority + Unused
    1.87 +          ]));
    1.88 +
    1.89 +          // Write compressed headers
    1.90 +          chunks.forEach(function(chunk) {
    1.91 +            socket.write(chunk);
    1.92 +          });
    1.93 +        });
    1.94 +
    1.95 +        var response = new Buffer(85),
    1.96 +            offset = 0;
    1.97 +
    1.98 +        socket.on('data', function(chunk) {
    1.99 +          assert.ok(offset + chunk.length <= 85);
   1.100 +
   1.101 +          chunk.copy(response, offset);
   1.102 +          offset += chunk.length;
   1.103 +
   1.104 +          if (offset === 85) {
   1.105 +            var frames = [];
   1.106 +
   1.107 +            offset = 0;
   1.108 +            while (offset < response.length) {
   1.109 +              var len = (response.readUInt32BE(offset + 4) & 0x00ffffff) + 8;
   1.110 +              frames.push(response.slice(offset, offset + len));
   1.111 +
   1.112 +              offset += len;
   1.113 +            }
   1.114 +
   1.115 +            // SYN_STREAM frame
   1.116 +            assert.ok(frames.some(function(frame) {
   1.117 +              return frame[0] === 0x80 && // Control frame
   1.118 +                     frame[1] === 0x02 && // Version
   1.119 +                     frame.readUInt16BE(2) === 0x0002 && // SYN_STREAM
   1.120 +                     frame.readUInt32BE(8) === 0x0001; // StreamID
   1.121 +            }));
   1.122 +
   1.123 +            // Data frames
   1.124 +            assert.ok(frames.some(function(frame) {
   1.125 +              return frame[0] === 0x00 && // Data frame
   1.126 +                     frame.readUInt32BE(0) === 0x0001 && // StreamID
   1.127 +                     frame.slice(8).toString() === 'ok';
   1.128 +            }));
   1.129 +
   1.130 +            socket.destroy();
   1.131 +          }
   1.132 +        });
   1.133 +
   1.134 +        socket.on('close', function() {
   1.135 +          done();
   1.136 +        });
   1.137 +      }
   1.138 +    );
   1.139 +
   1.140 +    socket.on('error', function(err) {
   1.141 +      console.error('Socket error: ' + err);
   1.142 +    });
   1.143 +
   1.144 +    server.on('request', function(req, res) {
   1.145 +      assert.equal(req.url, '/');
   1.146 +      assert.equal(req.method, 'GET');
   1.147 +      res.end('ok');
   1.148 +    });
   1.149 +  });
   1.150 +});

mercurial