From 59c605703f74c10ec86d9d7e8abfdbf20f4e8ab0 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Sat, 2 Apr 2016 00:49:02 +0200 Subject: [PATCH] url: fix format when query is not an object url.format uses query if search is falsy. Currently query is only used if it is an object. Query will now be inserted if its value is truthy. Fixes: #6004 --- lib/url.js | 4 +++- test/parallel/test-url.js | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/url.js b/lib/url.js index bb11144f7a33bc..3c3e91d2a99189 100644 --- a/lib/url.js +++ b/lib/url.js @@ -548,7 +548,7 @@ Url.prototype.format = function() { var pathname = this.pathname || ''; var hash = this.hash || ''; var host = false; - var query = ''; + var query; if (this.host) { host = auth + this.host; @@ -563,6 +563,8 @@ Url.prototype.format = function() { if (this.query !== null && typeof this.query === 'object') query = querystring.stringify(this.query); + else + query = this.query; var search = this.search || (query && ('?' + query)) || ''; diff --git a/test/parallel/test-url.js b/test/parallel/test-url.js index 760303e98bb6bb..1e3f923e2741c6 100644 --- a/test/parallel/test-url.js +++ b/test/parallel/test-url.js @@ -1586,3 +1586,6 @@ for (let i = 0; i < throws.length; i++) { } assert(url.format('') === ''); assert(url.format({}) === ''); + +// https://github.com/nodejs/node/issues/6004 +assert.equal(url.format({pathname:'/foo', query: 'bar=baz'}), '/foo?bar=baz');