diff --git a/lib/url.js b/lib/url.js index dd82b831e5..71c9f9d14a 100644 --- a/lib/url.js +++ b/lib/url.js @@ -74,7 +74,12 @@ function url_parse (url, parseQueryString) { if (qm !== -1) { out.search = rest.substr(qm); out.query = rest.substr(qm+1); - if (parseQueryString) out.query = (querystring || querystring = require("querystring")).parse(out.query); + if (parseQueryString) { + if (!querystring) { + querystring = require("querystring"); + } + out.query = querystring.parse(out.query); + } rest = rest.slice(0, qm); } if (rest) out.pathname = rest; diff --git a/test/mjsunit/test-url.js b/test/mjsunit/test-url.js index dfdd28777c..1ec6b7100f 100644 --- a/test/mjsunit/test-url.js +++ b/test/mjsunit/test-url.js @@ -176,6 +176,27 @@ for (var u in parseTests) { assert.equal(expected, actual, "format("+u+") == "+u+"\nactual:"+actual); } +var parseTestsWithQueryString = { + "/foo/bar?baz=quux#frag" : { + "href": "/foo/bar?baz=quux#frag", + "hash": "#frag", + "search": "?baz=quux", + "query": { + "baz": "quux" + }, + "pathname": "/foo/bar" + }, +}; +for (var u in parseTestsWithQueryString) { + var actual = url.parse(u,true); + var expected = parseTestsWithQueryString[u]; + for (var i in expected) { + var e = JSON.stringify(expected[i]), + a = JSON.stringify(actual[i]); + assert.equal(e, a, "parse(" + u + ")."+i+" == "+e+"\nactual: "+a); + } +} + // some extra formatting tests, just to verify that it'll format slightly wonky content to a valid url. var formatTests = { "http://a.com/a/b/c?s#h" : {