2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2015-05-19 11:00:06 +00:00
|
|
|
'use strict';
|
2016-05-29 07:06:56 +00:00
|
|
|
require('../common');
|
2016-12-30 23:38:06 +00:00
|
|
|
const assert = require('assert');
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2016-12-30 23:38:06 +00:00
|
|
|
const util = require('util');
|
|
|
|
const net = require('net');
|
|
|
|
const http = require('http');
|
2010-04-30 13:33:11 +00:00
|
|
|
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
let requests_recv = 0;
|
|
|
|
let requests_sent = 0;
|
|
|
|
let 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() {
|
2016-05-29 07:06:56 +00:00
|
|
|
http.Server.call(this, function() {});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2016-05-29 07:06:56 +00:00
|
|
|
this.on('connection', function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
requests_recv++;
|
|
|
|
});
|
2010-05-02 01:01:06 +00:00
|
|
|
|
2016-05-29 07:06:56 +00:00
|
|
|
this.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
|
|
|
|
2016-05-29 07:06:56 +00:00
|
|
|
this.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) {
|
2017-01-08 13:19:00 +00:00
|
|
|
const data = d.toString('utf8');
|
2016-08-17 23:14:43 +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
|
|
|
|
-----------------------------------------------*/
|
2016-05-29 07:06:56 +00:00
|
|
|
function test_upgrade_with_listener() {
|
2017-01-08 13:19:00 +00:00
|
|
|
const conn = net.createConnection(server.address().port);
|
2010-12-05 22:33:52 +00:00
|
|
|
conn.setEncoding('utf8');
|
2017-01-08 13:19:00 +00:00
|
|
|
let 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
|
|
|
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual('string', typeof data);
|
2010-06-16 01:19:25 +00:00
|
|
|
|
2016-08-17 23:14:43 +00:00
|
|
|
if (state === 1) {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual('HTTP/1.1 101', data.substr(0, 12));
|
|
|
|
assert.strictEqual('WjN}|M(6', request_upgradeHead.toString('utf8'));
|
2010-12-05 22:33:52 +00:00
|
|
|
conn.write('test', 'utf8');
|
2016-08-17 23:14:43 +00:00
|
|
|
} else if (state === 2) {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual('test', data);
|
2010-12-05 22:33:52 +00:00
|
|
|
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() {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(2, state);
|
2010-04-30 13:33:11 +00:00
|
|
|
conn.end();
|
2016-05-29 07:06:56 +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
|
|
|
|
-----------------------------------------------*/
|
2017-01-08 13:19:00 +00:00
|
|
|
let test_upgrade_no_listener_ended = false;
|
2010-04-30 13:33:11 +00:00
|
|
|
|
2010-12-05 22:33:52 +00:00
|
|
|
function test_upgrade_no_listener() {
|
2017-01-08 13:19:00 +00:00
|
|
|
const conn = net.createConnection(server.address().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() {
|
2017-01-08 13:19:00 +00:00
|
|
|
const conn = net.createConnection(server.address().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) {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual('string', typeof data);
|
|
|
|
assert.strictEqual('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
|
|
|
|
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const server = createTestServer();
|
2010-06-15 19:38:22 +00:00
|
|
|
|
2016-05-29 07:06:56 +00:00
|
|
|
server.listen(0, function() {
|
2010-04-30 13:33:11 +00:00
|
|
|
// All tests get chained after this:
|
2016-05-29 07:06:56 +00:00
|
|
|
test_upgrade_with_listener();
|
2010-04-30 13:33:11 +00:00
|
|
|
});
|
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() {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(3, requests_recv);
|
|
|
|
assert.strictEqual(3, requests_sent);
|
2010-04-30 13:33:11 +00:00
|
|
|
assert.ok(test_upgrade_no_listener_ended);
|
2010-05-02 01:01:06 +00:00
|
|
|
});
|