2010-06-16 01:35:49 +08: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.
|
|
|
|
|
2010-12-05 07:20:34 +08:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-06-16 01:35:49 +08:00
|
|
|
|
|
|
|
var http = require('http');
|
|
|
|
var net = require('net');
|
|
|
|
|
2010-06-30 14:12:46 +08:00
|
|
|
// Create a TCP server
|
2010-06-16 01:35:49 +08:00
|
|
|
var srv = net.createServer(function(c) {
|
|
|
|
var data = '';
|
|
|
|
c.addListener('data', function(d) {
|
|
|
|
data += d.toString('utf8');
|
|
|
|
|
|
|
|
c.write('HTTP/1.1 101\r\n');
|
2010-09-20 02:53:32 +08:00
|
|
|
c.write('hello: world\r\n');
|
2010-06-16 01:35:49 +08:00
|
|
|
c.write('connection: upgrade\r\n');
|
2010-09-20 02:53:32 +08:00
|
|
|
c.write('upgrade: websocket\r\n');
|
2010-06-16 01:35:49 +08:00
|
|
|
c.write('\r\n');
|
|
|
|
c.write('nurtzo');
|
2010-09-20 02:53:32 +08:00
|
|
|
});
|
2010-06-16 01:35:49 +08:00
|
|
|
|
2010-09-20 02:53:32 +08:00
|
|
|
c.addListener('end', function() {
|
2010-06-16 01:35:49 +08:00
|
|
|
c.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var gotUpgrade = false;
|
2010-10-24 05:26:31 +08:00
|
|
|
|
|
|
|
srv.listen(common.PORT, '127.0.0.1', function () {
|
|
|
|
|
|
|
|
var hc = http.createClient(common.PORT, '127.0.0.1');
|
|
|
|
hc.addListener('upgrade', function(res, socket, upgradeHead) {
|
|
|
|
// XXX: This test isn't fantastic, as it assumes that the entire response
|
|
|
|
// from the server will arrive in a single data callback
|
|
|
|
assert.equal(upgradeHead, 'nurtzo');
|
|
|
|
|
|
|
|
console.log(res.headers);
|
2010-11-30 07:22:48 +08:00
|
|
|
var expectedHeaders = { "hello": "world",
|
|
|
|
"connection": "upgrade",
|
|
|
|
"upgrade": "websocket" };
|
2010-10-24 05:26:31 +08:00
|
|
|
assert.deepEqual(expectedHeaders, res.headers);
|
|
|
|
|
|
|
|
socket.end();
|
|
|
|
srv.close();
|
|
|
|
|
|
|
|
gotUpgrade = true;
|
|
|
|
});
|
|
|
|
hc.request('GET', '/').end();
|
2010-06-16 01:35:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
process.addListener('exit', function() {
|
|
|
|
assert.ok(gotUpgrade);
|
|
|
|
});
|