From b705b73e46193c7691be40b732330a49affacedb Mon Sep 17 00:00:00 2001 From: Gabriel Wicke Date: Thu, 25 Sep 2014 09:59:18 -0700 Subject: [PATCH] url: make query() consistent Match the behavior of the slow path by setting url.query to an empty object when the url contains no query, but query parsing is requested. Also add a test for this case, and update the documents to clearly reflect this behavior. Fixes: https://github.com/joyent/node/issues/8332 Reviewed-by: Trevor Norris --- doc/api/url.markdown | 7 ++++--- lib/url.js | 3 +++ test/simple/test-url.js | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/doc/api/url.markdown b/doc/api/url.markdown index e6a43f72bcf..6b8ff9d5076 100644 --- a/doc/api/url.markdown +++ b/doc/api/url.markdown @@ -69,9 +69,10 @@ The following methods are provided by the URL module: Take a URL string, and return an object. -Pass `true` as the second argument to also parse -the query string using the `querystring` module. -Defaults to `false`. +Pass `true` as the second argument to also parse the query string using the +`querystring` module. If `true` then the `query` property will always be +assigned an object, and the `search` property will always be a (possibly +empty) string. Defaults to `false`. Pass `true` as the third argument to treat `//foo/bar` as `{ host: 'foo', pathname: '/bar' }` rather than diff --git a/lib/url.js b/lib/url.js index 4c0ef0102fa..6463424207d 100644 --- a/lib/url.js +++ b/lib/url.js @@ -136,6 +136,9 @@ Url.prototype.parse = function(url, parseQueryString, slashesDenoteHost) { } else { this.query = this.search.substr(1); } + } else if (parseQueryString) { + this.search = ''; + this.query = {}; } return this; } diff --git a/test/simple/test-url.js b/test/simple/test-url.js index 3271b0ba21a..e0a1b872d28 100644 --- a/test/simple/test-url.js +++ b/test/simple/test-url.js @@ -868,6 +868,20 @@ var parseTestsWithQueryString = { 'pathname': '/', 'path': '/' }, + '/example': { + protocol: null, + slashes: null, + auth: null, + host: null, + port: null, + hostname: null, + hash: null, + search: '', + query: {}, + pathname: '/example', + path: '/example', + href: '/example' + }, '/example?query=value':{ protocol: null, slashes: null,