Bug 429128 correctly adjust mRef when changing the query part of a URL r+sr=bz a=beltzner; unit test by bz r+sr=biesi

This commit is contained in:
cbiesinger@gmail.com 2008-04-17 01:27:44 -07:00
parent 32f9de58fb
commit 6d17efda5c
2 changed files with 74 additions and 15 deletions

View File

@ -2163,7 +2163,7 @@ nsStandardURL::SetQuery(const nsACString &input)
if (shift) {
mQuery.mLen = queryLen;
mPath.mLen += shift;
ShiftFromRef(queryLen - mQuery.mLen);
ShiftFromRef(shift);
}
return NS_OK;
}

View File

@ -5,10 +5,39 @@ const nsIStandardURL = Components.interfaces.nsIStandardURL;
function symmetricEquality(expect, a, b)
{
/* Use if/else instead of |do_check_eq(expect, a.spec == b.spec)| so
that we get the specs output on the console if the check fails.
*/
if (expect) {
/* Check all the sub-pieces too, since that can help with
debugging cases when equals() returns something unexpected */
/* We don't check port in the loop, because it can be defaulted in
some cases. */
["spec", "prePath", "scheme", "userPass", "username", "password",
"hostPort", "host", "path", "filePath", "param", "query",
"ref", "directory", "fileName", "fileBaseName", "fileExtension"]
.map(function(prop) {
dump("Testing '"+ prop + "'\n");
do_check_eq(a[prop], b[prop]);
});
} else {
do_check_neq(a.spec, b.spec);
}
do_check_eq(expect, a.equals(b));
do_check_eq(expect, b.equals(a));
}
function stringToURL(str) {
return (new StandardURL(nsIStandardURL.URLTYPE_AUTHORITY, 80,
str, "UTF-8", null))
.QueryInterface(Components.interfaces.nsIURL);
}
function pairToURLs(pair) {
do_check_eq(pair.length, 2);
return pair.map(stringToURL);
}
function test_setEmptyPath()
{
var pairs =
@ -19,22 +48,10 @@ function test_setEmptyPath()
["http://example.com/", "http://example.com/tests/dom/tests"],
["http://example.com/a", "http://example.com/tests/dom/tests"],
["http://example.com:80/a", "http://example.com/tests/dom/tests"],
];
].map(pairToURLs);
for (var i = 0; i < pairs.length; i++)
for each (var [provided, target] in pairs)
{
var provided = new StandardURL(nsIStandardURL.URLTYPE_AUTHORITY, 80,
pairs[i][0],
"UTF-8",
null);
var target = new StandardURL(nsIStandardURL.URLTYPE_AUTHORITY, 80,
pairs[i][1],
"UTF-8", null);
provided.QueryInterface(Components.interfaces.nsIURI);
target.QueryInterface(Components.interfaces.nsIURI);
symmetricEquality(false, target, provided);
provided.path = "";
@ -45,7 +62,49 @@ function test_setEmptyPath()
}
}
function test_setQuery()
{
var pairs =
[
["http://example.com", "http://example.com/?foo"],
["http://example.com/bar", "http://example.com/bar?foo"],
/* Can't test the path-less thing because of bug 429347 */
/* ["http://example.com#bar", "http://example.com/?foo#bar"], */
["http://example.com/#bar", "http://example.com/?foo#bar"],
["http://example.com/?longerthanfoo#bar", "http://example.com/?foo#bar"],
["http://example.com/?longerthanfoo", "http://example.com/?foo"],
/* And one that's nonempty but shorter than "foo" */
["http://example.com/?f#bar", "http://example.com/?foo#bar"],
["http://example.com/?f", "http://example.com/?foo"],
].map(pairToURLs);
for each (var [provided, target] in pairs) {
symmetricEquality(false, provided, target);
provided.query = "foo";
do_check_eq(provided.spec, target.spec);
symmetricEquality(true, provided, target);
}
[provided, target] =
["http://example.com/#", "http://example.com/?foo#bar"].map(stringToURL);
symmetricEquality(false, provided, target);
provided.query = "foo";
symmetricEquality(false, provided, target);
var newProvided = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService)
.newURI("#bar", null, provided)
.QueryInterface(Components.interfaces.nsIURL);
do_check_eq(newProvided.spec, target.spec);
symmetricEquality(true, newProvided, target);
}
function run_test()
{
test_setEmptyPath();
test_setQuery();
}