test: refactor test-http-default-port

- Remove redundant `hasCrypto` checks
- Use `common.mustCall()`
- Use arrow functions
- Deduplicate HTTP & HTTPS code

PR-URL: https://github.com/nodejs/node/pull/17562
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
This commit is contained in:
Anna Henningsen 2017-12-09 05:56:22 +01:00 committed by Anatoli Papirovski
parent ec6c063279
commit c84ca17305
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0

View File

@ -34,54 +34,27 @@ const options = {
key: fixtures.readKey('agent1-key.pem'), key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem') cert: fixtures.readKey('agent1-cert.pem')
}; };
let gotHttpsResp = false;
let gotHttpResp = false;
process.on('exit', function() { for (const { mod, createServer } of [
if (common.hasCrypto) { { mod: http, createServer: http.createServer },
assert(gotHttpsResp); { mod: https, createServer: https.createServer.bind(null, options) }
} ]) {
assert(gotHttpResp); const server = createServer(common.mustCall((req, res) => {
console.log('ok');
});
http.createServer(function(req, res) {
assert.strictEqual(req.headers.host, hostExpect);
assert.strictEqual(req.headers['x-port'], this.address().port.toString());
res.writeHead(200);
res.end('ok');
this.close();
}).listen(0, function() {
http.globalAgent.defaultPort = this.address().port;
http.get({
host: 'localhost',
headers: {
'x-port': this.address().port
}
}, function(res) {
gotHttpResp = true;
res.resume();
});
});
if (common.hasCrypto) {
https.createServer(options, function(req, res) {
assert.strictEqual(req.headers.host, hostExpect); assert.strictEqual(req.headers.host, hostExpect);
assert.strictEqual(req.headers['x-port'], this.address().port.toString()); assert.strictEqual(req.headers['x-port'], `${server.address().port}`);
res.writeHead(200); res.writeHead(200);
res.end('ok'); res.end('ok');
this.close(); server.close();
}).listen(0, function() { })).listen(0, common.mustCall(() => {
https.globalAgent.defaultPort = this.address().port; mod.globalAgent.defaultPort = server.address().port;
https.get({ mod.get({
host: 'localhost', host: 'localhost',
rejectUnauthorized: false, rejectUnauthorized: false,
headers: { headers: {
'x-port': this.address().port 'x-port': server.address().port
} }
}, function(res) { }, common.mustCall((res) => {
gotHttpsResp = true;
res.resume(); res.resume();
}); }));
}); }));
} }