From f39c7d9228d21ca8ab74e4e055d940540523774b Mon Sep 17 00:00:00 2001 From: DavidCai Date: Sat, 4 Feb 2017 21:39:20 +0800 Subject: [PATCH] url: fix error message of url.format PR-URL: https://github.com/nodejs/node/pull/11162 Reviewed-By: Joyee Cheung Reviewed-By: Anna Henningsen Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Timothy Gu Reviewed-By: Colin Ihrig Reviewed-By: Michael Dawson --- lib/url.js | 2 +- .../parallel/test-url-format-invalid-input.js | 25 +++++++++++-------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/url.js b/lib/url.js index 2b7dd6e5321977..57f04d5f3fccac 100644 --- a/lib/url.js +++ b/lib/url.js @@ -547,7 +547,7 @@ function urlFormat(obj, options) { obj = urlParse(obj); } else if (typeof obj !== 'object' || obj === null) { throw new TypeError('Parameter "urlObj" must be an object, not ' + - obj === null ? 'null' : typeof obj); + (obj === null ? 'null' : typeof obj)); } else if (!(obj instanceof Url)) { var format = obj[internalUrl.formatSymbol]; return format ? diff --git a/test/parallel/test-url-format-invalid-input.js b/test/parallel/test-url-format-invalid-input.js index 5a39cc1381d16f..908294d1c73211 100644 --- a/test/parallel/test-url-format-invalid-input.js +++ b/test/parallel/test-url-format-invalid-input.js @@ -3,17 +3,20 @@ require('../common'); const assert = require('assert'); const url = require('url'); -// https://github.com/nodejs/node/pull/1036 -const throws = [ - undefined, - null, - true, - false, - 0, - function() {} -]; -for (let i = 0; i < throws.length; i++) { - assert.throws(function() { url.format(throws[i]); }, TypeError); +const throwsObjsAndReportTypes = new Map([ + [undefined, 'undefined'], + [null, 'null'], + [true, 'boolean'], + [false, 'boolean'], + [0, 'number'], + [function() {}, 'function'], + [Symbol('foo'), 'symbol'] +]); + +for (const [obj, type] of throwsObjsAndReportTypes) { + const error = new RegExp('^TypeError: Parameter "urlObj" must be an object' + + `, not ${type}$`); + assert.throws(function() { url.format(obj); }, error); } assert.strictEqual(url.format(''), ''); assert.strictEqual(url.format({}), '');