netwerk/test/unit/test_URIs.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_URIs.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,634 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +Components.utils.import("resource://gre/modules/NetUtil.jsm");
     1.6 +
     1.7 +var gIoService = Components.classes["@mozilla.org/network/io-service;1"]
     1.8 +                           .getService(Components.interfaces.nsIIOService);
     1.9 +
    1.10 +
    1.11 +// Run by: cd objdir;  make -C netwerk/test/ xpcshell-tests    
    1.12 +// or: cd objdir; make SOLO_FILE="test_URIs.js" -C netwerk/test/ check-one
    1.13 +
    1.14 +// See also test_URIs2.js.
    1.15 +
    1.16 +// Relevant RFCs: 1738, 1808, 2396, 3986 (newer than the code)
    1.17 +// http://greenbytes.de/tech/webdav/rfc3986.html#rfc.section.5.4
    1.18 +// http://greenbytes.de/tech/tc/uris/
    1.19 +
    1.20 +// TEST DATA
    1.21 +// ---------
    1.22 +var gTests = [
    1.23 +  { spec:    "about:blank",
    1.24 +    scheme:  "about",
    1.25 +    prePath: "about:",
    1.26 +    path:    "blank",
    1.27 +    ref:     "",
    1.28 +    nsIURL:  false, nsINestedURI: true, immutable: true },
    1.29 +  { spec:    "about:foobar",
    1.30 +    scheme:  "about",
    1.31 +    prePath: "about:",
    1.32 +    path:    "foobar",
    1.33 +    ref:     "",
    1.34 +    nsIURL:  false, nsINestedURI: false, immutable: true },
    1.35 +  { spec:    "chrome://foobar/somedir/somefile.xml",
    1.36 +    scheme:  "chrome",
    1.37 +    prePath: "chrome://foobar",
    1.38 +    path:    "/somedir/somefile.xml",
    1.39 +    ref:     "",
    1.40 +    nsIURL:  true, nsINestedURI: false, immutable: true },
    1.41 +  { spec:    "data:text/html;charset=utf-8,<html></html>",
    1.42 +    scheme:  "data",
    1.43 +    prePath: "data:",
    1.44 +    path:    "text/html;charset=utf-8,<html></html>",
    1.45 +    ref:     "",
    1.46 +    nsIURL:  false, nsINestedURI: false },
    1.47 +  { spec:    "data:text/html;charset=utf-8,<html>\r\n\t</html>",
    1.48 +    scheme:  "data",
    1.49 +    prePath: "data:",
    1.50 +    path:    "text/html;charset=utf-8,<html></html>",
    1.51 +    ref:     "",
    1.52 +    nsIURL:  false, nsINestedURI: false },
    1.53 +  { spec:    "data:text/plain,hello world",
    1.54 +    scheme:  "data",
    1.55 +    prePath: "data:",
    1.56 +    path:    "text/plain,hello%20world",
    1.57 +    ref:     "",
    1.58 +    nsIURL:  false, nsINestedURI: false },
    1.59 +  { spec:    "file:///dir/afile",
    1.60 +    scheme:  "data",
    1.61 +    prePath: "data:",
    1.62 +    path:    "text/plain,2",
    1.63 +    ref:     "",
    1.64 +    relativeURI: "data:te\nxt/plain,2",
    1.65 +    nsIURL:  false, nsINestedURI: false },
    1.66 +  { spec:    "file://",
    1.67 +    scheme:  "file",
    1.68 +    prePath: "file://",
    1.69 +    path:    "/",
    1.70 +    ref:     "",
    1.71 +    nsIURL:  true, nsINestedURI: false },
    1.72 +  { spec:    "file:///",
    1.73 +    scheme:  "file",
    1.74 +    prePath: "file://",
    1.75 +    path:    "/",
    1.76 +    ref:     "",
    1.77 +    nsIURL:  true, nsINestedURI: false },
    1.78 +  { spec:    "file:///myFile.html",
    1.79 +    scheme:  "file",
    1.80 +    prePath: "file://",
    1.81 +    path:    "/myFile.html",
    1.82 +    ref:     "",
    1.83 +    nsIURL:  true, nsINestedURI: false },
    1.84 +  { spec:    "file:///dir/afile",
    1.85 +    scheme:  "file",
    1.86 +    prePath: "file://",
    1.87 +    path:    "/dir/data/text/plain,2",
    1.88 +    ref:     "",
    1.89 +    relativeURI: "data/text/plain,2",
    1.90 +    nsIURL:  true, nsINestedURI: false },
    1.91 +  { spec:    "file:///dir/dir2/",
    1.92 +    scheme:  "file",
    1.93 +    prePath: "file://",
    1.94 +    path:    "/dir/dir2/data/text/plain,2",
    1.95 +    ref:     "",
    1.96 +    relativeURI: "data/text/plain,2",
    1.97 +    nsIURL:  true, nsINestedURI: false },
    1.98 +  { spec:    "ftp://",
    1.99 +    scheme:  "ftp",
   1.100 +    prePath: "ftp://",
   1.101 +    path:    "/",
   1.102 +    ref:     "",
   1.103 +    nsIURL:  true, nsINestedURI: false },
   1.104 +  { spec:    "ftp:///",
   1.105 +    scheme:  "ftp",
   1.106 +    prePath: "ftp://",
   1.107 +    path:    "/",
   1.108 +    ref:     "",
   1.109 +    nsIURL:  true, nsINestedURI: false },
   1.110 +  { spec:    "ftp://ftp.mozilla.org/pub/mozilla.org/README",
   1.111 +    scheme:  "ftp",
   1.112 +    prePath: "ftp://ftp.mozilla.org",
   1.113 +    path:    "/pub/mozilla.org/README",
   1.114 +    ref:     "",
   1.115 +    nsIURL:  true, nsINestedURI: false },
   1.116 +  { spec:    "ftp://foo:bar@ftp.mozilla.org:100/pub/mozilla.org/README",
   1.117 +    scheme:  "ftp",
   1.118 +    prePath: "ftp://foo:bar@ftp.mozilla.org:100",
   1.119 +    port:    100,
   1.120 +    username: "foo",
   1.121 +    password: "bar",
   1.122 +    path:    "/pub/mozilla.org/README",
   1.123 +    ref:     "",
   1.124 +    nsIURL:  true, nsINestedURI: false },
   1.125 +  { spec:    "ftp://foo:@ftp.mozilla.org:100/pub/mozilla.org/README",
   1.126 +    scheme:  "ftp",
   1.127 +    prePath: "ftp://foo:@ftp.mozilla.org:100",
   1.128 +    port:    100,
   1.129 +    username: "foo",
   1.130 +    password: "",
   1.131 +    path:    "/pub/mozilla.org/README",
   1.132 +    ref:     "",
   1.133 +    nsIURL:  true, nsINestedURI: false },
   1.134 +  //Bug 706249
   1.135 +  { spec:    "http:x:@",
   1.136 +    scheme:  "http",
   1.137 +    prePath: "http://x:@",
   1.138 +    username: "x",
   1.139 +    password: "",
   1.140 +    path:    "",
   1.141 +    ref:     "",
   1.142 +    nsIURL:  true, nsINestedURI: false },
   1.143 +  { spec:    "gopher://mozilla.org/",
   1.144 +    scheme:  "gopher",
   1.145 +    prePath: "gopher:",
   1.146 +    path:    "//mozilla.org/",
   1.147 +    ref:     "",
   1.148 +    nsIURL:  false, nsINestedURI: false },
   1.149 +  { spec:    "http://",
   1.150 +    scheme:  "http",
   1.151 +    prePath: "http://",
   1.152 +    path:    "/",
   1.153 +    ref:     "",
   1.154 +    nsIURL:  true, nsINestedURI: false },
   1.155 +  { spec:    "http:///",
   1.156 +    scheme:  "http",
   1.157 +    prePath: "http://",
   1.158 +    path:    "/",
   1.159 +    ref:     "",
   1.160 +    nsIURL:  true, nsINestedURI: false },
   1.161 +  { spec:    "http://www.example.com/",
   1.162 +    scheme:  "http",
   1.163 +    prePath: "http://www.example.com",
   1.164 +    path:    "/",
   1.165 +    ref:     "",
   1.166 +    nsIURL:  true, nsINestedURI: false },
   1.167 +  { spec:    "http://www.exa\nmple.com/",
   1.168 +    scheme:  "http",
   1.169 +    prePath: "http://www.example.com",
   1.170 +    path:    "/",
   1.171 +    ref:     "",
   1.172 +    nsIURL:  true, nsINestedURI: false },
   1.173 +  { spec:    "http://10.32.4.239/",
   1.174 +    scheme:  "http",
   1.175 +    prePath: "http://10.32.4.239",
   1.176 +    host:    "10.32.4.239",
   1.177 +    path:    "/",
   1.178 +    ref:     "",
   1.179 +    nsIURL:  true, nsINestedURI: false },
   1.180 +  { spec:    "http://[::192.9.5.5]/ipng",
   1.181 +    scheme:  "http",
   1.182 +    prePath: "http://[::192.9.5.5]",
   1.183 +    host:    "::192.9.5.5",
   1.184 +    path:    "/ipng",
   1.185 +    ref:     "",
   1.186 +    nsIURL:  true, nsINestedURI: false },
   1.187 +  { spec:    "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:8888/index.html",
   1.188 +    scheme:  "http",
   1.189 +    prePath: "http://[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:8888",
   1.190 +    host:    "fedc:ba98:7654:3210:fedc:ba98:7654:3210",
   1.191 +    port:    8888,
   1.192 +    path:    "/index.html",
   1.193 +    ref:     "",
   1.194 +    nsIURL:  true, nsINestedURI: false },
   1.195 +  { spec:    "http://bar:foo@www.mozilla.org:8080/pub/mozilla.org/README.html",
   1.196 +    scheme:  "http",
   1.197 +    prePath: "http://bar:foo@www.mozilla.org:8080",
   1.198 +    port:    8080,
   1.199 +    username: "bar",
   1.200 +    password: "foo",
   1.201 +    host:    "www.mozilla.org",
   1.202 +    path:    "/pub/mozilla.org/README.html",
   1.203 +    ref:     "",
   1.204 +    nsIURL:  true, nsINestedURI: false },
   1.205 +  { spec:    "jar:resource://!/",
   1.206 +    scheme:  "jar",
   1.207 +    prePath: "jar:",
   1.208 +    path:    "resource:///!/",
   1.209 +    ref:     "",
   1.210 +    nsIURL:  true, nsINestedURI: true },
   1.211 +  { spec:    "jar:resource://gre/chrome.toolkit.jar!/",
   1.212 +    scheme:  "jar",
   1.213 +    prePath: "jar:",
   1.214 +    path:    "resource://gre/chrome.toolkit.jar!/",
   1.215 +    ref:     "",
   1.216 +    nsIURL:  true, nsINestedURI: true },
   1.217 +  { spec:    "mailto:webmaster@mozilla.com",
   1.218 +    scheme:  "mailto",
   1.219 +    prePath: "mailto:",
   1.220 +    path:    "webmaster@mozilla.com",
   1.221 +    ref:     "",
   1.222 +    nsIURL:  false, nsINestedURI: false },
   1.223 +  { spec:    "javascript:new Date()",
   1.224 +    scheme:  "javascript",
   1.225 +    prePath: "javascript:",
   1.226 +    path:    "new%20Date()",
   1.227 +    ref:     "",
   1.228 +    nsIURL:  false, nsINestedURI: false },
   1.229 +  { spec:    "blob:123456",
   1.230 +    scheme:  "blob",
   1.231 +    prePath: "blob:",
   1.232 +    path:    "123456",
   1.233 +    ref:     "",
   1.234 +    nsIURL:  false, nsINestedURI: false, immutable: true },
   1.235 +  { spec:    "place:sort=8&maxResults=10",
   1.236 +    scheme:  "place",
   1.237 +    prePath: "place:",
   1.238 +    path:    "sort=8&maxResults=10",
   1.239 +    ref:     "",
   1.240 +    nsIURL:  false, nsINestedURI: false },
   1.241 +  { spec:    "resource://gre/",
   1.242 +    scheme:  "resource",
   1.243 +    prePath: "resource://gre",
   1.244 +    path:    "/",
   1.245 +    ref:     "",
   1.246 +    nsIURL:  true, nsINestedURI: false },
   1.247 +  { spec:    "resource://gre/components/",
   1.248 +    scheme:  "resource",
   1.249 +    prePath: "resource://gre",
   1.250 +    path:    "/components/",
   1.251 +    ref:     "",
   1.252 +    nsIURL:  true, nsINestedURI: false },
   1.253 +  { spec:    "view-source:about:blank",
   1.254 +    scheme:  "view-source",
   1.255 +    prePath: "view-source:",
   1.256 +    path:    "about:blank",
   1.257 +    ref:     "",
   1.258 +    nsIURL:  false, nsINestedURI: true, immutable: true },
   1.259 +  { spec:    "view-source:http://www.mozilla.org/",
   1.260 +    scheme:  "view-source",
   1.261 +    prePath: "view-source:",
   1.262 +    path:    "http://www.mozilla.org/",
   1.263 +    ref:     "",
   1.264 +    nsIURL:  false, nsINestedURI: true, immutable: true },
   1.265 +  { spec:    "x-external:",
   1.266 +    scheme:  "x-external",
   1.267 +    prePath: "x-external:",
   1.268 +    path:    "",
   1.269 +    ref:     "",
   1.270 +    nsIURL:  false, nsINestedURI: false },
   1.271 +  { spec:    "x-external:abc",
   1.272 +    scheme:  "x-external",
   1.273 +    prePath: "x-external:",
   1.274 +    path:    "abc",
   1.275 +    ref:     "",
   1.276 +    nsIURL:  false, nsINestedURI: false },
   1.277 +  { spec:    "http://www2.example.com/",
   1.278 +    relativeURI: "a/b/c/d",
   1.279 +    scheme:  "http",
   1.280 +    prePath: "http://www2.example.com",
   1.281 +    path:    "/a/b/c/d",
   1.282 +    ref:     "",
   1.283 +    nsIURL:  true, nsINestedURI: false },
   1.284 +
   1.285 +    // Adding more? Consider adding to test_URIs2.js instead, so that neither
   1.286 +    // test runs for *too* long, risking timeouts on slow platforms.
   1.287 +];
   1.288 +
   1.289 +var gHashSuffixes = [
   1.290 +  "#",
   1.291 +  "#myRef",
   1.292 +  "#myRef?a=b",
   1.293 +  "#myRef#",
   1.294 +  "#myRef#x:yz"
   1.295 +];
   1.296 +
   1.297 +// TEST HELPER FUNCTIONS
   1.298 +// ---------------------
   1.299 +function do_info(text, stack) {
   1.300 +  if (!stack)
   1.301 +    stack = Components.stack.caller;
   1.302 +
   1.303 +  dump( "\n" +
   1.304 +       "TEST-INFO | " + stack.filename + " | [" + stack.name + " : " +
   1.305 +       stack.lineNumber + "] " + text + "\n");
   1.306 +}
   1.307 +
   1.308 +// Checks that the URIs satisfy equals(), in both possible orderings.
   1.309 +// Also checks URI.equalsExceptRef(), because equal URIs should also be equal
   1.310 +// when we ignore the ref.
   1.311 +// 
   1.312 +// The third argument is optional. If the client passes a third argument
   1.313 +// (e.g. todo_check_true), we'll use that in lieu of do_check_true.
   1.314 +function do_check_uri_eq(aURI1, aURI2, aCheckTrueFunc) {
   1.315 +  if (!aCheckTrueFunc) {
   1.316 +    aCheckTrueFunc = do_check_true;
   1.317 +  }
   1.318 +
   1.319 +  do_info("(uri equals check: '" + aURI1.spec + "' == '" + aURI2.spec + "')");
   1.320 +  aCheckTrueFunc(aURI1.equals(aURI2));
   1.321 +  do_info("(uri equals check: '" + aURI2.spec + "' == '" + aURI1.spec + "')");
   1.322 +  aCheckTrueFunc(aURI2.equals(aURI1));
   1.323 +
   1.324 +  // (Only take the extra step of testing 'equalsExceptRef' when we expect the
   1.325 +  // URIs to really be equal.  In 'todo' cases, the URIs may or may not be
   1.326 +  // equal when refs are ignored - there's no way of knowing in general.)
   1.327 +  if (aCheckTrueFunc == do_check_true) {
   1.328 +    do_check_uri_eqExceptRef(aURI1, aURI2, aCheckTrueFunc);
   1.329 +  }
   1.330 +}
   1.331 +
   1.332 +// Checks that the URIs satisfy equalsExceptRef(), in both possible orderings.
   1.333 +//
   1.334 +// The third argument is optional. If the client passes a third argument
   1.335 +// (e.g. todo_check_true), we'll use that in lieu of do_check_true.
   1.336 +function do_check_uri_eqExceptRef(aURI1, aURI2, aCheckTrueFunc) {
   1.337 +  if (!aCheckTrueFunc) {
   1.338 +    aCheckTrueFunc = do_check_true;
   1.339 +  }
   1.340 +
   1.341 +  do_info("(uri equalsExceptRef check: '" +
   1.342 +          aURI1.spec + "' == '" + aURI2.spec + "')");
   1.343 +  aCheckTrueFunc(aURI1.equalsExceptRef(aURI2));
   1.344 +  do_info("(uri equalsExceptRef check: '" +
   1.345 +          aURI2.spec + "' == '" + aURI1.spec + "')");
   1.346 +  aCheckTrueFunc(aURI2.equalsExceptRef(aURI1));
   1.347 +}
   1.348 +
   1.349 +// Checks that the given property on aURI matches the corresponding property
   1.350 +// in the test bundle (or matches some function of that corresponding property,
   1.351 +// if aTestFunctor is passed in).
   1.352 +function do_check_property(aTest, aURI, aPropertyName, aTestFunctor) {
   1.353 +  if (aTest[aPropertyName]) {
   1.354 +    var expectedVal = aTestFunctor ?
   1.355 +                      aTestFunctor(aTest[aPropertyName]) :
   1.356 +                      aTest[aPropertyName];
   1.357 +
   1.358 +    do_info("testing " + aPropertyName + " of " +
   1.359 +            (aTestFunctor ? "modified '" : "'" ) + aTest.spec +
   1.360 +            "' is '" + expectedVal + "'");
   1.361 +    do_check_eq(aURI[aPropertyName], expectedVal);
   1.362 +  }
   1.363 +}
   1.364 +
   1.365 +// Test that a given URI parses correctly into its various components.
   1.366 +function do_test_uri_basic(aTest) {
   1.367 +  var URI;
   1.368 +
   1.369 +  do_info("Basic tests for " + aTest.spec + " relative URI: " + aTest.relativeURI);
   1.370 +
   1.371 +  try {
   1.372 +    URI = NetUtil.newURI(aTest.spec);
   1.373 +  } catch(e) {
   1.374 +    do_info("Caught error on parse of" + aTest.spec + " Error: " + e.result);
   1.375 +    if (aTest.fail) {
   1.376 +      do_check_eq(e.result, aTest.result);
   1.377 +      return;
   1.378 +    }
   1.379 +    do_throw(e.result);
   1.380 +  }
   1.381 +
   1.382 +  if (aTest.relativeURI) {
   1.383 +    var relURI;
   1.384 +
   1.385 +    try {
   1.386 +      relURI = gIoService.newURI(aTest.relativeURI, null, URI);
   1.387 +    } catch (e) {
   1.388 +      do_info("Caught error on Relative parse of " + aTest.spec + " + " + aTest.relativeURI +" Error: " + e.result);
   1.389 +      if (aTest.relativeFail) {
   1.390 +        do_check_eq(e.result, aTest.relativeFail);
   1.391 +        return;
   1.392 +      }
   1.393 +      do_throw(e.result);
   1.394 +    }
   1.395 +    do_info("relURI.path = " + relURI.path + ", was " + URI.path);
   1.396 +    URI = relURI;
   1.397 +    do_info("URI.path now = " + URI.path);
   1.398 +  }
   1.399 +
   1.400 +  // Sanity-check
   1.401 +  do_info("testing " + aTest.spec + " equals a clone of itself");
   1.402 +  do_check_uri_eq(URI, URI.clone());
   1.403 +  do_check_uri_eqExceptRef(URI, URI.cloneIgnoringRef());
   1.404 +  do_info("testing " + aTest.spec + " instanceof nsIURL");
   1.405 +  do_check_eq(URI instanceof Ci.nsIURL, aTest.nsIURL);
   1.406 +  do_info("testing " + aTest.spec + " instanceof nsINestedURI");
   1.407 +  do_check_eq(URI instanceof Ci.nsINestedURI,
   1.408 +              aTest.nsINestedURI);
   1.409 +
   1.410 +  do_info("testing that " + aTest.spec + " throws or returns false " +
   1.411 +          "from equals(null)");
   1.412 +  // XXXdholbert At some point it'd probably be worth making this behavior
   1.413 +  // (throwing vs. returning false) consistent across URI implementations.
   1.414 +  var threw = false;
   1.415 +  var isEqualToNull;
   1.416 +  try {
   1.417 +    isEqualToNull = URI.equals(null);
   1.418 +  } catch(e) {
   1.419 +    threw = true;
   1.420 +  }
   1.421 +  do_check_true(threw || !isEqualToNull);
   1.422 +
   1.423 +
   1.424 +  // Check the various components
   1.425 +  do_check_property(aTest, URI, "scheme");
   1.426 +  do_check_property(aTest, URI, "prePath");
   1.427 +  do_check_property(aTest, URI, "path");
   1.428 +  do_check_property(aTest, URI, "ref");
   1.429 +  do_check_property(aTest, URI, "port");
   1.430 +  do_check_property(aTest, URI, "username");
   1.431 +  do_check_property(aTest, URI, "password");
   1.432 +  do_check_property(aTest, URI, "host");
   1.433 +  do_check_property(aTest, URI, "specIgnoringRef");
   1.434 +  if ("hasRef" in aTest) {
   1.435 +    do_info("testing hasref: " + aTest.hasRef + " vs " + URI.hasRef);
   1.436 +    do_check_eq(aTest.hasRef, URI.hasRef);
   1.437 +  }
   1.438 +}
   1.439 +
   1.440 +// Test that a given URI parses correctly when we add a given ref to the end
   1.441 +function do_test_uri_with_hash_suffix(aTest, aSuffix) {
   1.442 +  do_info("making sure caller is using suffix that starts with '#'");
   1.443 +  do_check_eq(aSuffix[0], "#");
   1.444 +
   1.445 +  var origURI = NetUtil.newURI(aTest.spec);
   1.446 +  var testURI;
   1.447 +
   1.448 +  if (aTest.relativeURI) {
   1.449 +    try {
   1.450 +      origURI = gIoService.newURI(aTest.relativeURI, null, origURI);
   1.451 +    } catch (e) {
   1.452 +      do_info("Caught error on Relative parse of " + aTest.spec + " + " + aTest.relativeURI +" Error: " + e.result);
   1.453 +      return;
   1.454 +    }
   1.455 +    try {
   1.456 +      testURI = gIoService.newURI(aSuffix, null, origURI);
   1.457 +    } catch (e) {
   1.458 +      do_info("Caught error adding suffix to " + aTest.spec + " + " + aTest.relativeURI + ", suffix " + aSuffix + " Error: " + e.result);
   1.459 +      return;
   1.460 +    }
   1.461 +  } else {
   1.462 +    testURI = NetUtil.newURI(aTest.spec + aSuffix);
   1.463 +  }
   1.464 +
   1.465 +  do_info("testing " + aTest.spec + " with '" + aSuffix + "' appended " +
   1.466 +           "equals a clone of itself");
   1.467 +  do_check_uri_eq(testURI, testURI.clone());
   1.468 +
   1.469 +  do_info("testing " + aTest.spec +
   1.470 +          " doesn't equal self with '" + aSuffix + "' appended");
   1.471 +
   1.472 +  do_check_false(origURI.equals(testURI));
   1.473 +
   1.474 +  do_info("testing " + aTest.spec +
   1.475 +          " is equalExceptRef to self with '" + aSuffix + "' appended");
   1.476 +  do_check_uri_eqExceptRef(origURI, testURI);
   1.477 +
   1.478 +  do_check_eq(testURI.hasRef, true);
   1.479 +
   1.480 +  if (!origURI.ref) {
   1.481 +    // These tests fail if origURI has a ref
   1.482 +    do_info("testing cloneIgnoringRef on " + testURI.spec +
   1.483 +            " is equal to no-ref version but not equal to ref version");
   1.484 +    var cloneNoRef = testURI.cloneIgnoringRef();
   1.485 +    do_check_uri_eq(cloneNoRef, origURI);
   1.486 +    do_check_false(cloneNoRef.equals(testURI));
   1.487 +  }
   1.488 +
   1.489 +  do_check_property(aTest, testURI, "scheme");
   1.490 +  do_check_property(aTest, testURI, "prePath");
   1.491 +  if (!origURI.ref) {
   1.492 +    // These don't work if it's a ref already because '+' doesn't give the right result
   1.493 +    do_check_property(aTest, testURI, "path",
   1.494 +                      function(aStr) { return aStr + aSuffix; });
   1.495 +    do_check_property(aTest, testURI, "ref",
   1.496 +                      function(aStr) { return aSuffix.substr(1); });
   1.497 +  }
   1.498 +}
   1.499 +
   1.500 +// Tests various ways of setting & clearing a ref on a URI.
   1.501 +function do_test_mutate_ref(aTest, aSuffix) {
   1.502 +  do_info("making sure caller is using suffix that starts with '#'");
   1.503 +  do_check_eq(aSuffix[0], "#");
   1.504 +
   1.505 +  var refURIWithSuffix    = NetUtil.newURI(aTest.spec + aSuffix);
   1.506 +  var refURIWithoutSuffix = NetUtil.newURI(aTest.spec);
   1.507 +
   1.508 +  var testURI             = NetUtil.newURI(aTest.spec);
   1.509 +
   1.510 +  // First: Try setting .ref to our suffix
   1.511 +  do_info("testing that setting .ref on " + aTest.spec +
   1.512 +          " to '" + aSuffix + "' does what we expect");
   1.513 +  testURI.ref = aSuffix;
   1.514 +  do_check_uri_eq(testURI, refURIWithSuffix);
   1.515 +  do_check_uri_eqExceptRef(testURI, refURIWithoutSuffix);
   1.516 +
   1.517 +  // Now try setting .ref but leave off the initial hash (expect same result)
   1.518 +  var suffixLackingHash = aSuffix.substr(1);
   1.519 +  if (suffixLackingHash) { // (skip this our suffix was *just* a #)
   1.520 +    do_info("testing that setting .ref on " + aTest.spec +
   1.521 +            " to '" + suffixLackingHash + "' does what we expect");
   1.522 +    testURI.ref = suffixLackingHash;
   1.523 +    do_check_uri_eq(testURI, refURIWithSuffix);
   1.524 +    do_check_uri_eqExceptRef(testURI, refURIWithoutSuffix);
   1.525 +  }
   1.526 +
   1.527 +  // Now, clear .ref (should get us back the original spec)
   1.528 +  do_info("testing that clearing .ref on " + testURI.spec +
   1.529 +          " does what we expect");
   1.530 +  testURI.ref = "";
   1.531 +  do_check_uri_eq(testURI, refURIWithoutSuffix);
   1.532 +  do_check_uri_eqExceptRef(testURI, refURIWithSuffix);
   1.533 +
   1.534 +  if (!aTest.relativeURI) {
   1.535 +    // TODO: These tests don't work as-is for relative URIs.
   1.536 +
   1.537 +    // Now try setting .spec directly (including suffix) and then clearing .ref
   1.538 +    var specWithSuffix = aTest.spec + aSuffix;
   1.539 +    do_info("testing that setting spec to " +
   1.540 +            specWithSuffix + " and then clearing ref does what we expect");
   1.541 +    testURI.spec = specWithSuffix;
   1.542 +    testURI.ref = "";
   1.543 +    do_check_uri_eq(testURI, refURIWithoutSuffix);
   1.544 +    do_check_uri_eqExceptRef(testURI, refURIWithSuffix);
   1.545 +
   1.546 +    // XXX nsIJARURI throws an exception in SetPath(), so skip it for next part.
   1.547 +    if (!(testURI instanceof Ci.nsIJARURI)) {
   1.548 +      // Now try setting .path directly (including suffix) and then clearing .ref
   1.549 +      // (same as above, but with now with .path instead of .spec)
   1.550 +      testURI = NetUtil.newURI(aTest.spec);
   1.551 +
   1.552 +      var pathWithSuffix = aTest.path + aSuffix;
   1.553 +      do_info("testing that setting path to " +
   1.554 +              pathWithSuffix + " and then clearing ref does what we expect");
   1.555 +      testURI.path = pathWithSuffix;
   1.556 +      testURI.ref = "";
   1.557 +      do_check_uri_eq(testURI, refURIWithoutSuffix);
   1.558 +      do_check_uri_eqExceptRef(testURI, refURIWithSuffix);
   1.559 +
   1.560 +      // Also: make sure that clearing .path also clears .ref
   1.561 +      testURI.path = pathWithSuffix;
   1.562 +      do_info("testing that clearing path from " + 
   1.563 +              pathWithSuffix + " also clears .ref");
   1.564 +      testURI.path = "";
   1.565 +      do_check_eq(testURI.ref, "");
   1.566 +    }
   1.567 +  }
   1.568 +}
   1.569 +
   1.570 +// Tests that normally-mutable properties can't be modified on
   1.571 +// special URIs that are known to be immutable.
   1.572 +function do_test_immutable(aTest) {
   1.573 +  do_check_true(aTest.immutable);
   1.574 +
   1.575 +  var URI = NetUtil.newURI(aTest.spec);
   1.576 +  // All the non-readonly attributes on nsIURI.idl:
   1.577 +  var propertiesToCheck = ["spec", "scheme", "userPass", "username", "password",
   1.578 +                           "hostPort", "host", "port", "path", "ref"];
   1.579 +
   1.580 +  propertiesToCheck.forEach(function(aProperty) {
   1.581 +    var threw = false;
   1.582 +    try {
   1.583 +      URI[aProperty] = "anothervalue";
   1.584 +    } catch(e) {
   1.585 +      threw = true;
   1.586 +    }
   1.587 +
   1.588 +    do_info("testing that setting '" + aProperty +
   1.589 +            "' on immutable URI '" + aTest.spec + "' will throw");
   1.590 +    do_check_true(threw);
   1.591 +  });
   1.592 +}
   1.593 +
   1.594 +
   1.595 +// TEST MAIN FUNCTION
   1.596 +// ------------------
   1.597 +function run_test()
   1.598 +{
   1.599 +  // UTF-8 check - From bug 622981
   1.600 +  // ASCII
   1.601 +  let base = gIoService.newURI("http://example.org/xenia?", null, null);
   1.602 +  let resolved = gIoService.newURI("?x", null, base);
   1.603 +  let expected = gIoService.newURI("http://example.org/xenia?x",
   1.604 +                                  null, null);
   1.605 +  do_info("Bug 662981: ACSII - comparing " + resolved.spec + " and " + expected.spec);
   1.606 +  do_check_true(resolved.equals(expected));
   1.607 +
   1.608 +  // UTF-8 character "è"
   1.609 +  // Bug 622981 was triggered by an empty query string
   1.610 +  base = gIoService.newURI("http://example.org/xènia?", null, null);
   1.611 +  resolved = gIoService.newURI("?x", null, base);
   1.612 +  expected = gIoService.newURI("http://example.org/xènia?x",
   1.613 +                              null, null);
   1.614 +  do_info("Bug 662981: UTF8 - comparing " + resolved.spec + " and " + expected.spec);
   1.615 +  do_check_true(resolved.equals(expected));
   1.616 +
   1.617 +  gTests.forEach(function(aTest) {
   1.618 +    // Check basic URI functionality
   1.619 +    do_test_uri_basic(aTest);
   1.620 +
   1.621 +    if (!aTest.fail) {
   1.622 +      // Try adding various #-prefixed strings to the ends of the URIs
   1.623 +      gHashSuffixes.forEach(function(aSuffix) {
   1.624 +          do_test_uri_with_hash_suffix(aTest, aSuffix);
   1.625 +          if (!aTest.immutable) {
   1.626 +            do_test_mutate_ref(aTest, aSuffix);
   1.627 +          }
   1.628 +        });
   1.629 +
   1.630 +      // For URIs that we couldn't mutate above due to them being immutable:
   1.631 +      // Now we check that they're actually immutable.
   1.632 +      if (aTest.immutable) {
   1.633 +        do_test_immutable(aTest);
   1.634 +      }
   1.635 +    }
   1.636 +  });
   1.637 +}

mercurial