Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
test: fix test-tls-over-http-tunnel with v0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
koichik authored and isaacs committed Feb 19, 2012
1 parent d530ee6 commit 7ae0d47
Showing 1 changed file with 34 additions and 17 deletions.
51 changes: 34 additions & 17 deletions test/simple/test-tls-over-http-tunnel.js
Expand Up @@ -64,8 +64,8 @@ var proxy = net.createServer(function(clientSocket) {
if (!serverSocket) {
// Verify the CONNECT request
assert.equal('CONNECT localhost:' + common.PORT + ' HTTP/1.1\r\n' +
'Proxy-Connections: keep-alive\r\nContent-Length:' +
' 0\r\nHost: localhost:' + proxyPort + '\r\n\r\n',
'Proxy-Connections: keep-alive\r\n' +
'Host: localhost:' + proxyPort + '\r\n\r\n',
chunk);

console.log('PROXY: got CONNECT request');
Expand Down Expand Up @@ -103,36 +103,53 @@ server.listen(common.PORT);
proxy.listen(proxyPort, function() {
console.log('CLIENT: Making CONNECT request');

http.request({
var req = http.request({
port: proxyPort,
method: 'CONNECT',
path: 'localhost:' + common.PORT,
headers: {
'Proxy-Connections': 'keep-alive',
'Content-Length': 0
'Proxy-Connections': 'keep-alive'
}
}, function(res) {
});
req.useChunkedEncodingByDefault = false; // for v0.6
req.on('response', onResponse); // for v0.6
req.on('upgrade', onUpgrade); // for v0.6
req.on('connect', onConnect); // for v0.7 or later
req.end();

function onResponse(res) {
// Very hacky. This is necessary to avoid http-parser leaks.
res.upgrade = true;
}

function onUpgrade(res, socket, head) {
// Hacky.
process.nextTick(function() {
onConnect(res, socket, head);
});
}

function onConnect(res, socket, header) {
assert.equal(200, res.statusCode);
console.log('CLIENT: got CONNECT response');

// detach the socket
res.socket.emit('agentRemove');
res.socket.removeAllListeners('data');
res.socket.removeAllListeners('close');
res.socket.removeAllListeners('error');
res.socket.removeAllListeners('drain');
res.socket.removeAllListeners('end');
res.socket.ondata = null;
res.socket.onend = null;
res.socket.ondrain = null;
socket.removeAllListeners('data');
socket.removeAllListeners('close');
socket.removeAllListeners('error');
socket.removeAllListeners('drain');
socket.removeAllListeners('end');
socket.ondata = null;
socket.onend = null;
socket.ondrain = null;

console.log('CLIENT: Making HTTPS request');

https.get({
path: '/foo',
key: key,
cert: cert,
socket: res.socket, // reuse the socket
socket: socket, // reuse the socket
agent: false
}, function(res) {
assert.equal(200, res.statusCode);
Expand All @@ -148,7 +165,7 @@ proxy.listen(proxyPort, function() {
server.close();
});
}).end();
}).end();
}
});

process.on('exit', function() {
Expand Down

0 comments on commit 7ae0d47

Please sign in to comment.