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({}),