netwerk/test/unit/test_URIs2.js

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

mercurial