Skip to content

Commit

Permalink
Make url.query consistent between slow & fast path
Browse files Browse the repository at this point in the history
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.

issue: nodejs#8332
  • Loading branch information
gwicke committed Oct 1, 2014
1 parent 7c04197 commit d543681
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
7 changes: 4 additions & 3 deletions doc/api/url.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 14 additions & 0 deletions test/simple/test-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d543681

Please sign in to comment.