Browse Source

Fix bug in the url module's url_parse method if 'parseQueryString' is true

v0.7.4-release
Benjamin Thomas 15 years ago
committed by Ryan Dahl
parent
commit
947c577c0d
  1. 7
      lib/url.js
  2. 21
      test/mjsunit/test-url.js

7
lib/url.js

@ -74,7 +74,12 @@ function url_parse (url, parseQueryString) {
if (qm !== -1) { if (qm !== -1) {
out.search = rest.substr(qm); out.search = rest.substr(qm);
out.query = rest.substr(qm+1); 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); rest = rest.slice(0, qm);
} }
if (rest) out.pathname = rest; if (rest) out.pathname = rest;

21
test/mjsunit/test-url.js

@ -176,6 +176,27 @@ for (var u in parseTests) {
assert.equal(expected, actual, "format("+u+") == "+u+"\nactual:"+actual); 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. // some extra formatting tests, just to verify that it'll format slightly wonky content to a valid url.
var formatTests = { var formatTests = {
"http://a.com/a/b/c?s#h" : { "http://a.com/a/b/c?s#h" : {

Loading…
Cancel
Save