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 <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
  • Loading branch information
addaleax authored and gibfahn committed Dec 20, 2017
1 parent 545f5a8 commit 7fbbed4
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 7fbbed4

Please sign in to comment.