Skip to content

Commit 5fc93ee

Browse files
edsadrevanlucas
authored andcommittedJan 4, 2017
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: #10397 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 78e8aa8 commit 5fc93ee

File tree

1 file changed

+33
-45
lines changed

1 file changed

+33
-45
lines changed
 

‎test/parallel/test-http-connect.js

+33-45
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,73 @@
11
'use strict';
22
const common = require('../common');
3-
var assert = require('assert');
4-
var http = require('http');
3+
const assert = require('assert');
4+
const http = require('http');
55

6-
var serverGotConnect = false;
7-
var clientGotConnect = false;
6+
const server = http.createServer(common.fail);
87

9-
var server = http.createServer(common.fail);
10-
server.on('connect', function(req, socket, firstBodyChunk) {
11-
assert.equal(req.method, 'CONNECT');
12-
assert.equal(req.url, 'google.com:443');
13-
console.error('Server got CONNECT request');
14-
serverGotConnect = true;
8+
server.on('connect', common.mustCall((req, socket, firstBodyChunk) => {
9+
assert.strictEqual(req.method, 'CONNECT');
10+
assert.strictEqual(req.url, 'google.com:443');
1511

1612
socket.write('HTTP/1.1 200 Connection established\r\n\r\n');
1713

18-
var data = firstBodyChunk.toString();
19-
socket.on('data', function(buf) {
14+
let data = firstBodyChunk.toString();
15+
socket.on('data', (buf) => {
2016
data += buf.toString();
2117
});
22-
socket.on('end', function() {
18+
19+
socket.on('end', common.mustCall(() => {
2320
socket.end(data);
24-
});
25-
});
26-
server.listen(0, function() {
27-
var req = http.request({
21+
}));
22+
}));
23+
24+
server.listen(0, common.mustCall(function() {
25+
const req = http.request({
2826
port: this.address().port,
2927
method: 'CONNECT',
3028
path: 'google.com:443'
3129
}, common.fail);
3230

33-
var clientRequestClosed = false;
34-
req.on('close', function() {
35-
clientRequestClosed = true;
36-
});
37-
38-
req.on('connect', function(res, socket, firstBodyChunk) {
39-
console.error('Client got CONNECT request');
40-
clientGotConnect = true;
31+
req.on('close', common.mustCall(() => {}));
4132

33+
req.on('connect', common.mustCall((res, socket, firstBodyChunk) => {
4234
// Make sure this request got removed from the pool.
43-
var name = 'localhost:' + server.address().port;
35+
const name = 'localhost:' + server.address().port;
4436
assert(!http.globalAgent.sockets.hasOwnProperty(name));
4537
assert(!http.globalAgent.requests.hasOwnProperty(name));
4638

4739
// Make sure this socket has detached.
4840
assert(!socket.ondata);
4941
assert(!socket.onend);
50-
assert.equal(socket.listeners('connect').length, 0);
51-
assert.equal(socket.listeners('data').length, 0);
42+
assert.strictEqual(socket.listeners('connect').length, 0);
43+
assert.strictEqual(socket.listeners('data').length, 0);
5244

5345
// the stream.Duplex onend listener
5446
// allow 0 here, so that i can run the same test on streams1 impl
5547
assert(socket.listeners('end').length <= 1);
5648

57-
assert.equal(socket.listeners('free').length, 0);
58-
assert.equal(socket.listeners('close').length, 0);
59-
assert.equal(socket.listeners('error').length, 0);
60-
assert.equal(socket.listeners('agentRemove').length, 0);
49+
assert.strictEqual(socket.listeners('free').length, 0);
50+
assert.strictEqual(socket.listeners('close').length, 0);
51+
assert.strictEqual(socket.listeners('error').length, 0);
52+
assert.strictEqual(socket.listeners('agentRemove').length, 0);
6153

62-
var data = firstBodyChunk.toString();
63-
socket.on('data', function(buf) {
54+
let data = firstBodyChunk.toString();
55+
socket.on('data', (buf) => {
6456
data += buf.toString();
6557
});
66-
socket.on('end', function() {
67-
assert.equal(data, 'HeadBody');
68-
assert(clientRequestClosed);
58+
59+
socket.on('end', common.mustCall(() => {
60+
assert.strictEqual(data, 'HeadBody');
6961
server.close();
70-
});
62+
}));
63+
7164
socket.write('Body');
7265
socket.end();
73-
});
66+
}));
7467

7568
// It is legal for the client to send some data intended for the server
7669
// before the "200 Connection established" (or any other success or
7770
// error code) is received.
7871
req.write('Head');
7972
req.end();
80-
});
81-
82-
process.on('exit', function() {
83-
assert.ok(serverGotConnect);
84-
assert.ok(clientGotConnect);
85-
});
73+
}));

0 commit comments

Comments
 (0)
Please sign in to comment.