From ac673df8334979ff1a1ad9298948140a9de692b9 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Wed, 19 May 2021 16:36:50 +0800 Subject: [PATCH] http: refactor to make servername option normalization testable Refs: https://coverage.nodejs.org/coverage-36bb31be5f0b85a0/lib/_http_agent.js.html#L316 --- lib/_http_agent.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/_http_agent.js b/lib/_http_agent.js index 8bc74c9cd43327..13bd5e9832d36e 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -252,8 +252,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */, if (options.socketPath) options.path = options.socketPath; - if (!options.servername && options.servername !== '') - options.servername = calculateServerName(options, req); + normalizeServerName(options, req); const name = this.getName(options); if (!this.sockets[name]) { @@ -313,8 +312,7 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) { if (options.socketPath) options.path = options.socketPath; - if (!options.servername && options.servername !== '') - options.servername = calculateServerName(options, req); + normalizeServerName(options, req); const name = this.getName(options); options._agentKey = name; @@ -340,6 +338,11 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) { oncreate(null, newSocket); }; +function normalizeServerName(options, req) { + if (!options.servername && options.servername !== '') + options.servername = calculateServerName(options, req); +} + function calculateServerName(options, req) { let servername = options.host; const hostHeader = req.getHeader('host');