test: refactor the code in test-http-connect

* use common.mustCall to control the functions execution automatically
* use let and const instead of var
* use assert.strictEqual instead of assert.equal
* use arrow functions
* remove console.error and unnecessary variables

PR-URL: https://github.com/nodejs/node/pull/10397
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Adrian Estrada 2016-12-26 09:43:06 -05:00 committed by Luigi Pinca
parent ca82f72b31
commit 72a8488e8e

View File

@ -1,85 +1,73 @@
'use strict'; 'use strict';
const common = require('../common'); const common = require('../common');
var assert = require('assert'); const assert = require('assert');
var http = require('http'); const http = require('http');
var serverGotConnect = false; const server = http.createServer(common.fail);
var clientGotConnect = false;
var server = http.createServer(common.fail); server.on('connect', common.mustCall((req, socket, firstBodyChunk) => {
server.on('connect', function(req, socket, firstBodyChunk) { assert.strictEqual(req.method, 'CONNECT');
assert.equal(req.method, 'CONNECT'); assert.strictEqual(req.url, 'google.com:443');
assert.equal(req.url, 'google.com:443');
console.error('Server got CONNECT request');
serverGotConnect = true;
socket.write('HTTP/1.1 200 Connection established\r\n\r\n'); socket.write('HTTP/1.1 200 Connection established\r\n\r\n');
var data = firstBodyChunk.toString(); let data = firstBodyChunk.toString();
socket.on('data', function(buf) { socket.on('data', (buf) => {
data += buf.toString(); data += buf.toString();
}); });
socket.on('end', function() {
socket.on('end', common.mustCall(() => {
socket.end(data); socket.end(data);
}); }));
}); }));
server.listen(0, function() {
var req = http.request({ server.listen(0, common.mustCall(function() {
const req = http.request({
port: this.address().port, port: this.address().port,
method: 'CONNECT', method: 'CONNECT',
path: 'google.com:443' path: 'google.com:443'
}, common.fail); }, common.fail);
var clientRequestClosed = false; req.on('close', common.mustCall(() => {}));
req.on('close', function() {
clientRequestClosed = true;
});
req.on('connect', function(res, socket, firstBodyChunk) {
console.error('Client got CONNECT request');
clientGotConnect = true;
req.on('connect', common.mustCall((res, socket, firstBodyChunk) => {
// Make sure this request got removed from the pool. // Make sure this request got removed from the pool.
var name = 'localhost:' + server.address().port; const name = 'localhost:' + server.address().port;
assert(!http.globalAgent.sockets.hasOwnProperty(name)); assert(!http.globalAgent.sockets.hasOwnProperty(name));
assert(!http.globalAgent.requests.hasOwnProperty(name)); assert(!http.globalAgent.requests.hasOwnProperty(name));
// Make sure this socket has detached. // Make sure this socket has detached.
assert(!socket.ondata); assert(!socket.ondata);
assert(!socket.onend); assert(!socket.onend);
assert.equal(socket.listeners('connect').length, 0); assert.strictEqual(socket.listeners('connect').length, 0);
assert.equal(socket.listeners('data').length, 0); assert.strictEqual(socket.listeners('data').length, 0);
// the stream.Duplex onend listener // the stream.Duplex onend listener
// allow 0 here, so that i can run the same test on streams1 impl // allow 0 here, so that i can run the same test on streams1 impl
assert(socket.listeners('end').length <= 1); assert(socket.listeners('end').length <= 1);
assert.equal(socket.listeners('free').length, 0); assert.strictEqual(socket.listeners('free').length, 0);
assert.equal(socket.listeners('close').length, 0); assert.strictEqual(socket.listeners('close').length, 0);
assert.equal(socket.listeners('error').length, 0); assert.strictEqual(socket.listeners('error').length, 0);
assert.equal(socket.listeners('agentRemove').length, 0); assert.strictEqual(socket.listeners('agentRemove').length, 0);
var data = firstBodyChunk.toString(); let data = firstBodyChunk.toString();
socket.on('data', function(buf) { socket.on('data', (buf) => {
data += buf.toString(); data += buf.toString();
}); });
socket.on('end', function() {
assert.equal(data, 'HeadBody'); socket.on('end', common.mustCall(() => {
assert(clientRequestClosed); assert.strictEqual(data, 'HeadBody');
server.close(); server.close();
}); }));
socket.write('Body'); socket.write('Body');
socket.end(); socket.end();
}); }));
// It is legal for the client to send some data intended for the server // It is legal for the client to send some data intended for the server
// before the "200 Connection established" (or any other success or // before the "200 Connection established" (or any other success or
// error code) is received. // error code) is received.
req.write('Head'); req.write('Head');
req.end(); req.end();
}); }));
process.on('exit', function() {
assert.ok(serverGotConnect);
assert.ok(clientGotConnect);
});