2017-01-03 13:16:48 -08: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 13:00:06 +02:00
|
|
|
'use strict';
|
2011-05-19 16:50:12 -07:00
|
|
|
// Verify that the 'upgrade' header causes an 'upgrade' event to be emitted to
|
|
|
|
// the HTTP client. This test uses a raw TCP server to better control server
|
|
|
|
// behavior.
|
|
|
|
|
2016-12-30 18:38:06 -05:00
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
2011-05-19 16:50:12 -07:00
|
|
|
|
2016-12-30 18:38:06 -05:00
|
|
|
const http = require('http');
|
|
|
|
const net = require('net');
|
2011-05-19 16:50:12 -07:00
|
|
|
|
|
|
|
// Create a TCP server
|
2020-01-07 10:21:19 +00:00
|
|
|
const server = net.createServer(function(c) {
|
2011-10-15 01:08:36 +02:00
|
|
|
c.on('data', function(d) {
|
2011-05-19 16:50:12 -07:00
|
|
|
c.write('HTTP/1.1 101\r\n');
|
|
|
|
c.write('hello: world\r\n');
|
|
|
|
c.write('connection: upgrade\r\n');
|
|
|
|
c.write('upgrade: websocket\r\n');
|
|
|
|
c.write('\r\n');
|
|
|
|
c.write('nurtzo');
|
|
|
|
});
|
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
c.on('end', function() {
|
2011-05-19 16:50:12 -07:00
|
|
|
c.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-07 10:21:19 +00:00
|
|
|
server.listen(0, '127.0.0.1', common.mustCall(function() {
|
2011-05-19 16:50:12 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const options = {
|
2016-05-29 03:06:56 -04:00
|
|
|
port: this.address().port,
|
2011-05-19 16:50:12 -07:00
|
|
|
host: '127.0.0.1',
|
|
|
|
headers: {
|
2015-12-17 17:23:46 -05:00
|
|
|
'connection': 'upgrade',
|
2011-05-19 16:50:12 -07:00
|
|
|
'upgrade': 'websocket'
|
|
|
|
}
|
|
|
|
};
|
2017-04-28 04:06:42 +03:00
|
|
|
const name = `${options.host}:${options.port}`;
|
2011-05-19 16:50:12 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const req = http.request(options);
|
2011-05-19 16:50:12 -07:00
|
|
|
req.end();
|
|
|
|
|
2021-10-24 13:34:45 +02:00
|
|
|
req.on('socket', common.mustCall(function() {
|
|
|
|
assert.strictEqual(req.agent.totalSocketCount, 1);
|
|
|
|
}));
|
|
|
|
|
2016-07-15 15:43:24 -04:00
|
|
|
req.on('upgrade', common.mustCall(function(res, socket, upgradeHead) {
|
2021-10-24 13:34:45 +02:00
|
|
|
assert.strictEqual(req.agent.totalSocketCount, 0);
|
2017-01-08 13:19:00 +00:00
|
|
|
let recvData = upgradeHead;
|
2016-01-03 18:10:22 +01:00
|
|
|
socket.on('data', function(d) {
|
|
|
|
recvData += d;
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('close', common.mustCall(function() {
|
2017-01-08 15:36:25 +00:00
|
|
|
assert.strictEqual(recvData.toString(), 'nurtzo');
|
2016-01-03 18:10:22 +01:00
|
|
|
}));
|
2011-05-19 16:50:12 -07:00
|
|
|
|
2017-01-08 13:19:00 +00:00
|
|
|
const expectedHeaders = { 'hello': 'world',
|
|
|
|
'connection': 'upgrade',
|
|
|
|
'upgrade': 'websocket' };
|
2016-04-19 15:37:45 -07:00
|
|
|
assert.deepStrictEqual(expectedHeaders, res.headers);
|
2011-10-04 18:08:18 -04:00
|
|
|
|
2012-01-12 14:16:03 +09:00
|
|
|
// Make sure this request got removed from the pool.
|
2021-10-24 13:34:45 +02:00
|
|
|
assert(!(name in req.agent.sockets));
|
2012-01-12 14:16:03 +09:00
|
|
|
|
2016-07-15 15:43:24 -04:00
|
|
|
req.on('close', common.mustCall(function() {
|
2011-08-01 21:22:30 -07:00
|
|
|
socket.end();
|
2020-01-07 10:21:19 +00:00
|
|
|
server.close();
|
2016-07-15 15:43:24 -04:00
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}));
|