2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2010-12-04 23:20:34 +00:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2010-12-05 22:33:52 +00:00
|
|
|
var util = require('util');
|
|
|
|
var net = require('net');
|
|
|
|
var http = require('http');
|
2010-04-30 13:33:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
var requests_recv = 0;
|
|
|
|
var requests_sent = 0;
|
2010-05-02 01:01:06 +00:00
|
|
|
var request_upgradeHead = null;
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2010-12-05 22:33:52 +00:00
|
|
|
function createTestServer() {
|
2010-04-30 13:33:11 +00:00
|
|
|
return new testServer();
|
2010-12-05 22:33:52 +00:00
|
|
|
}
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2010-12-05 22:33:52 +00:00
|
|
|
function testServer() {
|
2010-04-30 13:33:11 +00:00
|
|
|
var server = this;
|
2010-12-05 22:33:52 +00:00
|
|
|
http.Server.call(server, function() {});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
server.on('connection', function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
requests_recv++;
|
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
server.on('request', function(req, res) {
|
2010-12-05 22:33:52 +00:00
|
|
|
res.writeHead(200, {'Content-Type': 'text/plain'});
|
|
|
|
res.write('okay');
|
2010-04-30 13:33:11 +00:00
|
|
|
res.end();
|
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
server.on('upgrade', function(req, socket, upgradeHead) {
|
2010-12-05 22:33:52 +00:00
|
|
|
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
|
|
|
|
'Upgrade: WebSocket\r\n' +
|
|
|
|
'Connection: Upgrade\r\n' +
|
|
|
|
'\r\n\r\n');
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2013-06-13 00:45:30 +00:00
|
|
|
request_upgradeHead = upgradeHead;
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2013-07-26 02:33:15 +00:00
|
|
|
socket.on('data', function(d) {
|
|
|
|
var data = d.toString('utf8');
|
2010-12-05 22:33:52 +00:00
|
|
|
if (data == 'kill') {
|
2010-04-30 13:33:11 +00:00
|
|
|
socket.end();
|
|
|
|
} else {
|
2010-12-05 22:33:52 +00:00
|
|
|
socket.write(data, 'utf8');
|
2010-04-30 13:33:11 +00:00
|
|
|
}
|
2013-07-26 02:33:15 +00:00
|
|
|
});
|
2010-04-30 13:33:11 +00:00
|
|
|
});
|
2010-12-05 22:33:52 +00:00
|
|
|
}
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2010-10-11 21:04:09 +00:00
|
|
|
util.inherits(testServer, http.Server);
|
2010-04-30 13:33:11 +00:00
|
|
|
|
|
|
|
|
2010-12-05 22:33:52 +00:00
|
|
|
function writeReq(socket, data, encoding) {
|
2010-04-30 13:33:11 +00:00
|
|
|
requests_sent++;
|
|
|
|
socket.write(data);
|
2010-12-05 22:33:52 +00:00
|
|
|
}
|
2010-04-30 13:33:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------
|
|
|
|
connection: Upgrade with listener
|
|
|
|
-----------------------------------------------*/
|
2010-12-05 22:33:52 +00:00
|
|
|
function test_upgrade_with_listener(_server) {
|
2010-07-15 18:47:25 +00:00
|
|
|
var conn = net.createConnection(common.PORT);
|
2010-12-05 22:33:52 +00:00
|
|
|
conn.setEncoding('utf8');
|
2010-04-30 13:33:11 +00:00
|
|
|
var state = 0;
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('connect', function() {
|
2010-12-05 22:33:52 +00:00
|
|
|
writeReq(conn,
|
|
|
|
'GET / HTTP/1.1\r\n' +
|
|
|
|
'Upgrade: WebSocket\r\n' +
|
|
|
|
'Connection: Upgrade\r\n' +
|
|
|
|
'\r\n' +
|
|
|
|
'WjN}|M(6');
|
2010-04-30 13:33:11 +00:00
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('data', function(data) {
|
2010-04-30 13:33:11 +00:00
|
|
|
state++;
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2010-06-16 01:19:25 +00:00
|
|
|
assert.equal('string', typeof data);
|
|
|
|
|
2010-12-05 22:33:52 +00:00
|
|
|
if (state == 1) {
|
|
|
|
assert.equal('HTTP/1.1 101', data.substr(0, 12));
|
|
|
|
assert.equal('WjN}|M(6', request_upgradeHead.toString('utf8'));
|
|
|
|
conn.write('test', 'utf8');
|
|
|
|
} else if (state == 2) {
|
|
|
|
assert.equal('test', data);
|
|
|
|
conn.write('kill', 'utf8');
|
2010-04-30 13:33:11 +00:00
|
|
|
}
|
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('end', function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
assert.equal(2, state);
|
|
|
|
conn.end();
|
2010-12-05 22:33:52 +00:00
|
|
|
_server.removeAllListeners('upgrade');
|
2010-04-30 13:33:11 +00:00
|
|
|
test_upgrade_no_listener();
|
|
|
|
});
|
2010-12-05 22:33:52 +00:00
|
|
|
}
|
2010-04-30 13:33:11 +00:00
|
|
|
|
|
|
|
/*-----------------------------------------------
|
|
|
|
connection: Upgrade, no listener
|
|
|
|
-----------------------------------------------*/
|
|
|
|
var test_upgrade_no_listener_ended = false;
|
|
|
|
|
2010-12-05 22:33:52 +00:00
|
|
|
function test_upgrade_no_listener() {
|
2010-07-15 18:47:25 +00:00
|
|
|
var conn = net.createConnection(common.PORT);
|
2010-12-05 22:33:52 +00:00
|
|
|
conn.setEncoding('utf8');
|
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('connect', function() {
|
2010-12-05 22:33:52 +00:00
|
|
|
writeReq(conn,
|
|
|
|
'GET / HTTP/1.1\r\n' +
|
|
|
|
'Upgrade: WebSocket\r\n' +
|
|
|
|
'Connection: Upgrade\r\n' +
|
|
|
|
'\r\n');
|
2010-04-30 13:33:11 +00:00
|
|
|
});
|
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('end', function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
test_upgrade_no_listener_ended = true;
|
|
|
|
conn.end();
|
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('close', function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
test_standard_http();
|
|
|
|
});
|
2010-12-05 22:33:52 +00:00
|
|
|
}
|
2010-04-30 13:33:11 +00:00
|
|
|
|
|
|
|
/*-----------------------------------------------
|
|
|
|
connection: normal
|
|
|
|
-----------------------------------------------*/
|
2010-12-05 22:33:52 +00:00
|
|
|
function test_standard_http() {
|
2010-07-15 18:47:25 +00:00
|
|
|
var conn = net.createConnection(common.PORT);
|
2010-12-05 22:33:52 +00:00
|
|
|
conn.setEncoding('utf8');
|
2010-06-16 01:19:25 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('connect', function() {
|
2010-12-05 22:33:52 +00:00
|
|
|
writeReq(conn, 'GET / HTTP/1.1\r\n\r\n');
|
2010-04-30 13:33:11 +00:00
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2013-04-08 12:40:31 +00:00
|
|
|
conn.once('data', function(data) {
|
2010-12-05 22:33:52 +00:00
|
|
|
assert.equal('string', typeof data);
|
|
|
|
assert.equal('HTTP/1.1 200', data.substr(0, 12));
|
2010-04-30 13:33:11 +00:00
|
|
|
conn.end();
|
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2011-10-14 23:08:36 +00:00
|
|
|
conn.on('close', function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
server.close();
|
|
|
|
});
|
2010-12-05 22:33:52 +00:00
|
|
|
}
|
2010-04-30 13:33:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
var server = createTestServer();
|
2010-06-15 19:38:22 +00:00
|
|
|
|
2010-12-05 22:33:52 +00:00
|
|
|
server.listen(common.PORT, function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
// All tests get chained after this:
|
|
|
|
test_upgrade_with_listener(server);
|
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2010-04-30 13:33:11 +00:00
|
|
|
|
|
|
|
/*-----------------------------------------------
|
|
|
|
Fin.
|
|
|
|
-----------------------------------------------*/
|
2011-10-14 23:08:36 +00:00
|
|
|
process.on('exit', function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
assert.equal(3, requests_recv);
|
|
|
|
assert.equal(3, requests_sent);
|
|
|
|
assert.ok(test_upgrade_no_listener_ended);
|
2010-05-02 01:01:06 +00:00
|
|
|
});
|