Skip to content

Commit f45ef44

Browse files
addaleaxMylesBorins
authored andcommitted
test: refactor test-http-default-port
- Remove redundant `hasCrypto` checks - Use `common.mustCall()` - Use arrow functions - Deduplicate HTTP & HTTPS code PR-URL: #17562 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]>
1 parent ccffbd9 commit f45ef44

File tree

1 file changed

+14
-41
lines changed

1 file changed

+14
-41
lines changed

test/parallel/test-http-default-port.js

+14-41
Original file line numberDiff line numberDiff line change
@@ -34,54 +34,27 @@ const options = {
3434
key: fixtures.readKey('agent1-key.pem'),
3535
cert: fixtures.readKey('agent1-cert.pem')
3636
};
37-
let gotHttpsResp = false;
38-
let gotHttpResp = false;
3937

40-
process.on('exit', function() {
41-
if (common.hasCrypto) {
42-
assert(gotHttpsResp);
43-
}
44-
assert(gotHttpResp);
45-
console.log('ok');
46-
});
47-
48-
http.createServer(function(req, res) {
49-
assert.strictEqual(req.headers.host, hostExpect);
50-
assert.strictEqual(req.headers['x-port'], this.address().port.toString());
51-
res.writeHead(200);
52-
res.end('ok');
53-
this.close();
54-
}).listen(0, function() {
55-
http.globalAgent.defaultPort = this.address().port;
56-
http.get({
57-
host: 'localhost',
58-
headers: {
59-
'x-port': this.address().port
60-
}
61-
}, function(res) {
62-
gotHttpResp = true;
63-
res.resume();
64-
});
65-
});
66-
67-
if (common.hasCrypto) {
68-
https.createServer(options, function(req, res) {
38+
for (const { mod, createServer } of [
39+
{ mod: http, createServer: http.createServer },
40+
{ mod: https, createServer: https.createServer.bind(null, options) }
41+
]) {
42+
const server = createServer(common.mustCall((req, res) => {
6943
assert.strictEqual(req.headers.host, hostExpect);
70-
assert.strictEqual(req.headers['x-port'], this.address().port.toString());
44+
assert.strictEqual(req.headers['x-port'], `${server.address().port}`);
7145
res.writeHead(200);
7246
res.end('ok');
73-
this.close();
74-
}).listen(0, function() {
75-
https.globalAgent.defaultPort = this.address().port;
76-
https.get({
47+
server.close();
48+
})).listen(0, common.mustCall(() => {
49+
mod.globalAgent.defaultPort = server.address().port;
50+
mod.get({
7751
host: 'localhost',
7852
rejectUnauthorized: false,
7953
headers: {
80-
'x-port': this.address().port
54+
'x-port': server.address().port
8155
}
82-
}, function(res) {
83-
gotHttpsResp = true;
56+
}, common.mustCall((res) => {
8457
res.resume();
85-
});
86-
});
58+
}));
59+
}));
8760
}

0 commit comments

Comments
 (0)