Skip to content

Commit

Permalink
test: refactor test-http-default-port
Browse files Browse the repository at this point in the history
- 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]>
  • Loading branch information
addaleax authored and MylesBorins committed Dec 12, 2017
1 parent ccffbd9 commit f45ef44
Showing 1 changed file with 14 additions and 41 deletions.
55 changes: 14 additions & 41 deletions test/parallel/test-http-default-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,54 +34,27 @@ const options = {
key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem')
};
let gotHttpsResp = false;
let gotHttpResp = false;

process.on('exit', function() {
if (common.hasCrypto) {
assert(gotHttpsResp);
}
assert(gotHttpResp);
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) {
for (const { mod, createServer } of [
{ mod: http, createServer: http.createServer },
{ mod: https, createServer: https.createServer.bind(null, options) }
]) {
const server = createServer(common.mustCall((req, res) => {
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.end('ok');
this.close();
}).listen(0, function() {
https.globalAgent.defaultPort = this.address().port;
https.get({
server.close();
})).listen(0, common.mustCall(() => {
mod.globalAgent.defaultPort = server.address().port;
mod.get({
host: 'localhost',
rejectUnauthorized: false,
headers: {
'x-port': this.address().port
'x-port': server.address().port
}
}, function(res) {
gotHttpsResp = true;
}, common.mustCall((res) => {
res.resume();
});
});
}));
}));
}

0 comments on commit f45ef44

Please sign in to comment.