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:
parent
ec6c063279
commit
c84ca17305
@ -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();
|
||||
});
|
||||
});
|
||||
}));
|
||||
}));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user