From 6f726cf8c7415d58acb5f6b8493fe53f272b1dd4 Mon Sep 17 00:00:00 2001 From: Jeremy Martin Date: Mon, 20 Dec 2010 16:21:02 -0500 Subject: [PATCH] url.parse(url, true) defaults query field to {} --- lib/querystring.js | 2 +- lib/url.js | 3 +++ test/simple/test-url.js | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/querystring.js b/lib/querystring.js index eeeada0467..87c4391a55 100644 --- a/lib/querystring.js +++ b/lib/querystring.js @@ -136,7 +136,7 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq) { eq = eq || '='; var obj = {}; - if (typeof qs !== 'string') { + if (typeof qs !== 'string' || qs.length === 0) { return obj; } diff --git a/lib/url.js b/lib/url.js index a0f274edea..b8c1105a9f 100644 --- a/lib/url.js +++ b/lib/url.js @@ -98,6 +98,9 @@ function urlParse(url, parseQueryString, slashesDenoteHost) { out.query = querystring.parse(out.query); } rest = rest.slice(0, qm); + } else if (parseQueryString) { + // no query string, but parseQueryString still requested + out.query = {}; } if (rest) out.pathname = rest; diff --git a/test/simple/test-url.js b/test/simple/test-url.js index c59e92bec3..91509b2baa 100644 --- a/test/simple/test-url.js +++ b/test/simple/test-url.js @@ -192,6 +192,23 @@ var parseTestsWithQueryString = { 'baz': 'quux' }, 'pathname': '/foo/bar' + }, + 'http://example.com' : { + 'href': 'http://example.com', + 'protocol': 'http:', + 'slashes': true, + 'host': 'example.com', + 'hostname': 'example.com', + 'query': {} + }, + 'http://example.com?' : { + 'href': 'http://example.com?', + 'protocol': 'http:', + 'slashes': true, + 'host': 'example.com', + 'hostname': 'example.com', + 'search': '?', + 'query': {} } }; for (var u in parseTestsWithQueryString) {