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

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

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

mercurial