From 04c68ba54a82d73e05d4cd7a87501eb49bda51e1 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 e7049bf3d7e..09a51b42dba 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -296,10 +296,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 9c15875762d..28759a91c4f 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 695a9020994..7a9a4243aaa 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 ab946a4bde3..14cfcfaabdf 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 6f8c32b299a..2a13ab1c6f4 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({}),