From d30f7a8465d84907dc7bdcfcfc53c2abcde37b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=8F=9C?= Date: Sun, 27 Feb 2022 23:01:01 +0800 Subject: [PATCH] http: add default argument for Agent.prototype.getName PR-URL: https://github.com/nodejs/node/pull/41906 Reviewed-By: Matteo Collina Reviewed-By: Ricky Zhou <0x19951125@gmail.com> Reviewed-By: Mestery --- doc/api/http.md | 6 +++++- lib/_http_agent.js | 2 +- lib/https.js | 2 +- test/parallel/test-http-agent-getname.js | 8 +++++++- test/parallel/test-https-agent-getname.js | 6 ++++++ 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index 10ef2b0bbb7831..0a67e82c5f4108 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -294,10 +294,14 @@ the agent when `keepAlive` is enabled. Do not modify. Sockets in the `freeSockets` list will be automatically destroyed and removed from the array on `'timeout'`. -### `agent.getName(options)` +### `agent.getName([options])` * `options` {Object} A set of options providing information for name generation diff --git a/lib/_http_agent.js b/lib/_http_agent.js index 9c15875762dd47..28759a91c4f5a1 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -217,7 +217,7 @@ Agent.defaultMaxSockets = Infinity; Agent.prototype.createConnection = net.createConnection; // Get the key for a given set of request options -Agent.prototype.getName = function getName(options) { +Agent.prototype.getName = function getName(options = {}) { let name = options.host || 'localhost'; name += ':'; diff --git a/lib/https.js b/lib/https.js index 695a9020994852..7a9a4243aaae14 100644 --- a/lib/https.js +++ b/lib/https.js @@ -203,7 +203,7 @@ Agent.prototype.createConnection = createConnection; * }} [options] * @returns {string} */ -Agent.prototype.getName = function getName(options) { +Agent.prototype.getName = function getName(options = {}) { let name = FunctionPrototypeCall(HttpAgent.prototype.getName, this, options); name += ':'; diff --git a/test/parallel/test-http-agent-getname.js b/test/parallel/test-http-agent-getname.js index ab946a4bde3ddf..14cfcfaabdf6f4 100644 --- a/test/parallel/test-http-agent-getname.js +++ b/test/parallel/test-http-agent-getname.js @@ -18,7 +18,13 @@ assert.strictEqual( 'localhost:80:192.168.1.1' ); -// empty +// empty argument +assert.strictEqual( + agent.getName(), + 'localhost::' +); + +// empty options assert.strictEqual( agent.getName({}), 'localhost::' diff --git a/test/parallel/test-https-agent-getname.js b/test/parallel/test-https-agent-getname.js index 6f8c32b299a669..2a13ab1c6f47ee 100644 --- a/test/parallel/test-https-agent-getname.js +++ b/test/parallel/test-https-agent-getname.js @@ -9,6 +9,12 @@ const https = require('https'); const agent = new https.Agent(); +// empty argument +assert.strictEqual( + agent.getName(), + 'localhost::::::::::::::::::::::' +); + // empty options assert.strictEqual( agent.getName({}),