netwerk/test/unit/test_URIs2.js

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 Components.utils.import("resource://gre/modules/NetUtil.jsm");
michael@0 3
michael@0 4 var gIoService = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 5 .getService(Components.interfaces.nsIIOService);
michael@0 6
michael@0 7
michael@0 8 // Run by: cd objdir; make -C netwerk/test/ xpcshell-tests
michael@0 9 // or: cd objdir; make SOLO_FILE="test_URIs2.js" -C netwerk/test/ check-one
michael@0 10
michael@0 11 // This is a clone of test_URIs.js, with a different set of test data in gTests.
michael@0 12 // The original test data in test_URIs.js was split between test_URIs and test_URIs2.js
michael@0 13 // because test_URIs.js was running for too long on slow platforms, causing
michael@0 14 // intermittent timeouts.
michael@0 15
michael@0 16 // Relevant RFCs: 1738, 1808, 2396, 3986 (newer than the code)
michael@0 17 // http://greenbytes.de/tech/webdav/rfc3986.html#rfc.section.5.4
michael@0 18 // http://greenbytes.de/tech/tc/uris/
michael@0 19
michael@0 20 // TEST DATA
michael@0 21 // ---------
michael@0 22 var gTests = [
michael@0 23 // relative URL testcases from http://greenbytes.de/tech/webdav/rfc3986.html#rfc.section.5.4
michael@0 24 { spec: "http://a/b/c/d;p?q",
michael@0 25 relativeURI: "g:h",
michael@0 26 scheme: "g",
michael@0 27 prePath: "g:",
michael@0 28 path: "h",
michael@0 29 ref: "",
michael@0 30 nsIURL: false, nsINestedURI: false },
michael@0 31 { spec: "http://a/b/c/d;p?q",
michael@0 32 relativeURI: "g",
michael@0 33 scheme: "http",
michael@0 34 prePath: "http://a",
michael@0 35 path: "/b/c/g",
michael@0 36 ref: "",
michael@0 37 nsIURL: true, nsINestedURI: false },
michael@0 38 { spec: "http://a/b/c/d;p?q",
michael@0 39 relativeURI: "./g",
michael@0 40 scheme: "http",
michael@0 41 prePath: "http://a",
michael@0 42 path: "/b/c/g",
michael@0 43 ref: "",
michael@0 44 nsIURL: true, nsINestedURI: false },
michael@0 45 { spec: "http://a/b/c/d;p?q",
michael@0 46 relativeURI: "g/",
michael@0 47 scheme: "http",
michael@0 48 prePath: "http://a",
michael@0 49 path: "/b/c/g/",
michael@0 50 ref: "",
michael@0 51 nsIURL: true, nsINestedURI: false },
michael@0 52 { spec: "http://a/b/c/d;p?q",
michael@0 53 relativeURI: "/g",
michael@0 54 scheme: "http",
michael@0 55 prePath: "http://a",
michael@0 56 path: "/g",
michael@0 57 ref: "",
michael@0 58 nsIURL: true, nsINestedURI: false },
michael@0 59 { spec: "http://a/b/c/d;p?q",
michael@0 60 relativeURI: "?y",
michael@0 61 scheme: "http",
michael@0 62 prePath: "http://a",
michael@0 63 path: "/b/c/d;p?y",
michael@0 64 ref: "",// fix
michael@0 65 nsIURL: true, nsINestedURI: false },
michael@0 66 { spec: "http://a/b/c/d;p?q",
michael@0 67 relativeURI: "g?y",
michael@0 68 scheme: "http",
michael@0 69 prePath: "http://a",
michael@0 70 path: "/b/c/g?y",
michael@0 71 ref: "",// fix
michael@0 72 specIgnoringRef: "http://a/b/c/g?y",
michael@0 73 hasRef: false,
michael@0 74 nsIURL: true, nsINestedURI: false },
michael@0 75 { spec: "http://a/b/c/d;p?q",
michael@0 76 relativeURI: "#s",
michael@0 77 scheme: "http",
michael@0 78 prePath: "http://a",
michael@0 79 path: "/b/c/d;p?q#s",
michael@0 80 ref: "s",// fix
michael@0 81 specIgnoringRef: "http://a/b/c/d;p?q",
michael@0 82 hasRef: true,
michael@0 83 nsIURL: true, nsINestedURI: false },
michael@0 84 { spec: "http://a/b/c/d;p?q",
michael@0 85 relativeURI: "g#s",
michael@0 86 scheme: "http",
michael@0 87 prePath: "http://a",
michael@0 88 path: "/b/c/g#s",
michael@0 89 ref: "s",
michael@0 90 nsIURL: true, nsINestedURI: false },
michael@0 91 { spec: "http://a/b/c/d;p?q",
michael@0 92 relativeURI: "g?y#s",
michael@0 93 scheme: "http",
michael@0 94 prePath: "http://a",
michael@0 95 path: "/b/c/g?y#s",
michael@0 96 ref: "s",
michael@0 97 nsIURL: true, nsINestedURI: false },
michael@0 98 /*
michael@0 99 Bug xxxxxx - we return a path of b/c/;x
michael@0 100 { spec: "http://a/b/c/d;p?q",
michael@0 101 relativeURI: ";x",
michael@0 102 scheme: "http",
michael@0 103 prePath: "http://a",
michael@0 104 path: "/b/c/d;x",
michael@0 105 ref: "",
michael@0 106 nsIURL: true, nsINestedURI: false },
michael@0 107 */
michael@0 108 { spec: "http://a/b/c/d;p?q",
michael@0 109 relativeURI: "g;x",
michael@0 110 scheme: "http",
michael@0 111 prePath: "http://a",
michael@0 112 path: "/b/c/g;x",
michael@0 113 ref: "",
michael@0 114 nsIURL: true, nsINestedURI: false },
michael@0 115 { spec: "http://a/b/c/d;p?q",
michael@0 116 relativeURI: "g;x?y#s",
michael@0 117 scheme: "http",
michael@0 118 prePath: "http://a",
michael@0 119 path: "/b/c/g;x?y#s",
michael@0 120 ref: "s",
michael@0 121 nsIURL: true, nsINestedURI: false },
michael@0 122 /*
michael@0 123 Can't easily specify a relative URI of "" to the test code
michael@0 124 { spec: "http://a/b/c/d;p?q",
michael@0 125 relativeURI: "",
michael@0 126 scheme: "http",
michael@0 127 prePath: "http://a",
michael@0 128 path: "/b/c/d",
michael@0 129 ref: "",
michael@0 130 nsIURL: true, nsINestedURI: false },
michael@0 131 */
michael@0 132 { spec: "http://a/b/c/d;p?q",
michael@0 133 relativeURI: ".",
michael@0 134 scheme: "http",
michael@0 135 prePath: "http://a",
michael@0 136 path: "/b/c/",
michael@0 137 ref: "",
michael@0 138 nsIURL: true, nsINestedURI: false },
michael@0 139 { spec: "http://a/b/c/d;p?q",
michael@0 140 relativeURI: "./",
michael@0 141 scheme: "http",
michael@0 142 prePath: "http://a",
michael@0 143 path: "/b/c/",
michael@0 144 ref: "",
michael@0 145 nsIURL: true, nsINestedURI: false },
michael@0 146 { spec: "http://a/b/c/d;p?q",
michael@0 147 relativeURI: "..",
michael@0 148 scheme: "http",
michael@0 149 prePath: "http://a",
michael@0 150 path: "/b/",
michael@0 151 ref: "",
michael@0 152 nsIURL: true, nsINestedURI: false },
michael@0 153 { spec: "http://a/b/c/d;p?q",
michael@0 154 relativeURI: "../",
michael@0 155 scheme: "http",
michael@0 156 prePath: "http://a",
michael@0 157 path: "/b/",
michael@0 158 ref: "",
michael@0 159 nsIURL: true, nsINestedURI: false },
michael@0 160 { spec: "http://a/b/c/d;p?q",
michael@0 161 relativeURI: "../g",
michael@0 162 scheme: "http",
michael@0 163 prePath: "http://a",
michael@0 164 path: "/b/g",
michael@0 165 ref: "",
michael@0 166 nsIURL: true, nsINestedURI: false },
michael@0 167 { spec: "http://a/b/c/d;p?q",
michael@0 168 relativeURI: "../..",
michael@0 169 scheme: "http",
michael@0 170 prePath: "http://a",
michael@0 171 path: "/",
michael@0 172 ref: "",
michael@0 173 nsIURL: true, nsINestedURI: false },
michael@0 174 { spec: "http://a/b/c/d;p?q",
michael@0 175 relativeURI: "../../",
michael@0 176 scheme: "http",
michael@0 177 prePath: "http://a",
michael@0 178 path: "/",
michael@0 179 ref: "",
michael@0 180 nsIURL: true, nsINestedURI: false },
michael@0 181 { spec: "http://a/b/c/d;p?q",
michael@0 182 relativeURI: "../../g",
michael@0 183 scheme: "http",
michael@0 184 prePath: "http://a",
michael@0 185 path: "/g",
michael@0 186 ref: "",
michael@0 187 nsIURL: true, nsINestedURI: false },
michael@0 188
michael@0 189 // abnormal examples
michael@0 190 { spec: "http://a/b/c/d;p?q",
michael@0 191 relativeURI: "../../../g",
michael@0 192 scheme: "http",
michael@0 193 prePath: "http://a",
michael@0 194 path: "/g",
michael@0 195 ref: "",
michael@0 196 nsIURL: true, nsINestedURI: false },
michael@0 197 { spec: "http://a/b/c/d;p?q",
michael@0 198 relativeURI: "../../../../g",
michael@0 199 scheme: "http",
michael@0 200 prePath: "http://a",
michael@0 201 path: "/g",
michael@0 202 ref: "",
michael@0 203 nsIURL: true, nsINestedURI: false },
michael@0 204
michael@0 205 // coalesce
michael@0 206 { spec: "http://a/b/c/d;p?q",
michael@0 207 relativeURI: "/./g",
michael@0 208 scheme: "http",
michael@0 209 prePath: "http://a",
michael@0 210 path: "/g",
michael@0 211 ref: "",
michael@0 212 nsIURL: true, nsINestedURI: false },
michael@0 213 { spec: "http://a/b/c/d;p?q",
michael@0 214 relativeURI: "/../g",
michael@0 215 scheme: "http",
michael@0 216 prePath: "http://a",
michael@0 217 path: "/g",
michael@0 218 ref: "",
michael@0 219 nsIURL: true, nsINestedURI: false },
michael@0 220 { spec: "http://a/b/c/d;p?q",
michael@0 221 relativeURI: "g.",
michael@0 222 scheme: "http",
michael@0 223 prePath: "http://a",
michael@0 224 path: "/b/c/g.",
michael@0 225 ref: "",
michael@0 226 nsIURL: true, nsINestedURI: false },
michael@0 227 { spec: "http://a/b/c/d;p?q",
michael@0 228 relativeURI: ".g",
michael@0 229 scheme: "http",
michael@0 230 prePath: "http://a",
michael@0 231 path: "/b/c/.g",
michael@0 232 ref: "",
michael@0 233 nsIURL: true, nsINestedURI: false },
michael@0 234 { spec: "http://a/b/c/d;p?q",
michael@0 235 relativeURI: "g..",
michael@0 236 scheme: "http",
michael@0 237 prePath: "http://a",
michael@0 238 path: "/b/c/g..",
michael@0 239 ref: "",
michael@0 240 nsIURL: true, nsINestedURI: false },
michael@0 241 { spec: "http://a/b/c/d;p?q",
michael@0 242 relativeURI: "..g",
michael@0 243 scheme: "http",
michael@0 244 prePath: "http://a",
michael@0 245 path: "/b/c/..g",
michael@0 246 ref: "",
michael@0 247 nsIURL: true, nsINestedURI: false },
michael@0 248 { spec: "http://a/b/c/d;p?q",
michael@0 249 relativeURI: ".",
michael@0 250 scheme: "http",
michael@0 251 prePath: "http://a",
michael@0 252 path: "/b/c/",
michael@0 253 ref: "",
michael@0 254 nsIURL: true, nsINestedURI: false },
michael@0 255 { spec: "http://a/b/c/d;p?q",
michael@0 256 relativeURI: "./../g",
michael@0 257 scheme: "http",
michael@0 258 prePath: "http://a",
michael@0 259 path: "/b/g",
michael@0 260 ref: "",
michael@0 261 nsIURL: true, nsINestedURI: false },
michael@0 262 { spec: "http://a/b/c/d;p?q",
michael@0 263 relativeURI: "./g/.",
michael@0 264 scheme: "http",
michael@0 265 prePath: "http://a",
michael@0 266 path: "/b/c/g/",
michael@0 267 ref: "",
michael@0 268 nsIURL: true, nsINestedURI: false },
michael@0 269 { spec: "http://a/b/c/d;p?q",
michael@0 270 relativeURI: "g/./h",
michael@0 271 scheme: "http",
michael@0 272 prePath: "http://a",
michael@0 273 path: "/b/c/g/h",
michael@0 274 ref: "",
michael@0 275 nsIURL: true, nsINestedURI: false },
michael@0 276 { spec: "http://a/b/c/d;p?q",
michael@0 277 relativeURI: "g/../h",
michael@0 278 scheme: "http",
michael@0 279 prePath: "http://a",
michael@0 280 path: "/b/c/h",
michael@0 281 ref: "",// fix
michael@0 282 nsIURL: true, nsINestedURI: false },
michael@0 283 { spec: "http://a/b/c/d;p?q",
michael@0 284 relativeURI: "g;x=1/./y",
michael@0 285 scheme: "http",
michael@0 286 prePath: "http://a",
michael@0 287 path: "/b/c/g;x=1/y",
michael@0 288 ref: "",
michael@0 289 nsIURL: true, nsINestedURI: false },
michael@0 290 { spec: "http://a/b/c/d;p?q",
michael@0 291 relativeURI: "g;x=1/../y",
michael@0 292 scheme: "http",
michael@0 293 prePath: "http://a",
michael@0 294 path: "/b/c/y",
michael@0 295 ref: "",
michael@0 296 nsIURL: true, nsINestedURI: false },
michael@0 297 // protocol-relative http://tools.ietf.org/html/rfc3986#section-4.2
michael@0 298 { spec: "http://www2.example.com/",
michael@0 299 relativeURI: "//www3.example2.com/bar",
michael@0 300 scheme: "http",
michael@0 301 prePath: "http://www3.example2.com",
michael@0 302 path: "/bar",
michael@0 303 ref: "",
michael@0 304 nsIURL: true, nsINestedURI: false },
michael@0 305 { spec: "https://www2.example.com/",
michael@0 306 relativeURI: "//www3.example2.com/bar",
michael@0 307 scheme: "https",
michael@0 308 prePath: "https://www3.example2.com",
michael@0 309 path: "/bar",
michael@0 310 ref: "",
michael@0 311 nsIURL: true, nsINestedURI: false },
michael@0 312 ];
michael@0 313
michael@0 314 var gHashSuffixes = [
michael@0 315 "#",
michael@0 316 "#myRef",
michael@0 317 "#myRef?a=b",
michael@0 318 "#myRef#",
michael@0 319 "#myRef#x:yz"
michael@0 320 ];
michael@0 321
michael@0 322 // TEST HELPER FUNCTIONS
michael@0 323 // ---------------------
michael@0 324 function do_info(text, stack) {
michael@0 325 if (!stack)
michael@0 326 stack = Components.stack.caller;
michael@0 327
michael@0 328 dump( "\n" +
michael@0 329 "TEST-INFO | " + stack.filename + " | [" + stack.name + " : " +
michael@0 330 stack.lineNumber + "] " + text + "\n");
michael@0 331 }
michael@0 332
michael@0 333 // Checks that the URIs satisfy equals(), in both possible orderings.
michael@0 334 // Also checks URI.equalsExceptRef(), because equal URIs should also be equal
michael@0 335 // when we ignore the ref.
michael@0 336 //
michael@0 337 // The third argument is optional. If the client passes a third argument
michael@0 338 // (e.g. todo_check_true), we'll use that in lieu of do_check_true.
michael@0 339 function do_check_uri_eq(aURI1, aURI2, aCheckTrueFunc) {
michael@0 340 if (!aCheckTrueFunc) {
michael@0 341 aCheckTrueFunc = do_check_true;
michael@0 342 }
michael@0 343
michael@0 344 do_info("(uri equals check: '" + aURI1.spec + "' == '" + aURI2.spec + "')");
michael@0 345 aCheckTrueFunc(aURI1.equals(aURI2));
michael@0 346 do_info("(uri equals check: '" + aURI2.spec + "' == '" + aURI1.spec + "')");
michael@0 347 aCheckTrueFunc(aURI2.equals(aURI1));
michael@0 348
michael@0 349 // (Only take the extra step of testing 'equalsExceptRef' when we expect the
michael@0 350 // URIs to really be equal. In 'todo' cases, the URIs may or may not be
michael@0 351 // equal when refs are ignored - there's no way of knowing in general.)
michael@0 352 if (aCheckTrueFunc == do_check_true) {
michael@0 353 do_check_uri_eqExceptRef(aURI1, aURI2, aCheckTrueFunc);
michael@0 354 }
michael@0 355 }
michael@0 356
michael@0 357 // Checks that the URIs satisfy equalsExceptRef(), in both possible orderings.
michael@0 358 //
michael@0 359 // The third argument is optional. If the client passes a third argument
michael@0 360 // (e.g. todo_check_true), we'll use that in lieu of do_check_true.
michael@0 361 function do_check_uri_eqExceptRef(aURI1, aURI2, aCheckTrueFunc) {
michael@0 362 if (!aCheckTrueFunc) {
michael@0 363 aCheckTrueFunc = do_check_true;
michael@0 364 }
michael@0 365
michael@0 366 do_info("(uri equalsExceptRef check: '" +
michael@0 367 aURI1.spec + "' == '" + aURI2.spec + "')");
michael@0 368 aCheckTrueFunc(aURI1.equalsExceptRef(aURI2));
michael@0 369 do_info("(uri equalsExceptRef check: '" +
michael@0 370 aURI2.spec + "' == '" + aURI1.spec + "')");
michael@0 371 aCheckTrueFunc(aURI2.equalsExceptRef(aURI1));
michael@0 372 }
michael@0 373
michael@0 374 // Checks that the given property on aURI matches the corresponding property
michael@0 375 // in the test bundle (or matches some function of that corresponding property,
michael@0 376 // if aTestFunctor is passed in).
michael@0 377 function do_check_property(aTest, aURI, aPropertyName, aTestFunctor) {
michael@0 378 if (aTest[aPropertyName]) {
michael@0 379 var expectedVal = aTestFunctor ?
michael@0 380 aTestFunctor(aTest[aPropertyName]) :
michael@0 381 aTest[aPropertyName];
michael@0 382
michael@0 383 do_info("testing " + aPropertyName + " of " +
michael@0 384 (aTestFunctor ? "modified '" : "'" ) + aTest.spec +
michael@0 385 "' is '" + expectedVal + "'");
michael@0 386 do_check_eq(aURI[aPropertyName], expectedVal);
michael@0 387 }
michael@0 388 }
michael@0 389
michael@0 390 // Test that a given URI parses correctly into its various components.
michael@0 391 function do_test_uri_basic(aTest) {
michael@0 392 var URI;
michael@0 393
michael@0 394 do_info("Basic tests for " + aTest.spec + " relative URI: " + aTest.relativeURI);
michael@0 395
michael@0 396 try {
michael@0 397 URI = NetUtil.newURI(aTest.spec);
michael@0 398 } catch(e) {
michael@0 399 do_info("Caught error on parse of" + aTest.spec + " Error: " + e.result);
michael@0 400 if (aTest.fail) {
michael@0 401 do_check_eq(e.result, aTest.result);
michael@0 402 return;
michael@0 403 }
michael@0 404 do_throw(e.result);
michael@0 405 }
michael@0 406
michael@0 407 if (aTest.relativeURI) {
michael@0 408 var relURI;
michael@0 409
michael@0 410 try {
michael@0 411 relURI = gIoService.newURI(aTest.relativeURI, null, URI);
michael@0 412 } catch (e) {
michael@0 413 do_info("Caught error on Relative parse of " + aTest.spec + " + " + aTest.relativeURI +" Error: " + e.result);
michael@0 414 if (aTest.relativeFail) {
michael@0 415 do_check_eq(e.result, aTest.relativeFail);
michael@0 416 return;
michael@0 417 }
michael@0 418 do_throw(e.result);
michael@0 419 }
michael@0 420 do_info("relURI.path = " + relURI.path + ", was " + URI.path);
michael@0 421 URI = relURI;
michael@0 422 do_info("URI.path now = " + URI.path);
michael@0 423 }
michael@0 424
michael@0 425 // Sanity-check
michael@0 426 do_info("testing " + aTest.spec + " equals a clone of itself");
michael@0 427 do_check_uri_eq(URI, URI.clone());
michael@0 428 do_check_uri_eqExceptRef(URI, URI.cloneIgnoringRef());
michael@0 429 do_info("testing " + aTest.spec + " instanceof nsIURL");
michael@0 430 do_check_eq(URI instanceof Ci.nsIURL, aTest.nsIURL);
michael@0 431 do_info("testing " + aTest.spec + " instanceof nsINestedURI");
michael@0 432 do_check_eq(URI instanceof Ci.nsINestedURI,
michael@0 433 aTest.nsINestedURI);
michael@0 434
michael@0 435 do_info("testing that " + aTest.spec + " throws or returns false " +
michael@0 436 "from equals(null)");
michael@0 437 // XXXdholbert At some point it'd probably be worth making this behavior
michael@0 438 // (throwing vs. returning false) consistent across URI implementations.
michael@0 439 var threw = false;
michael@0 440 var isEqualToNull;
michael@0 441 try {
michael@0 442 isEqualToNull = URI.equals(null);
michael@0 443 } catch(e) {
michael@0 444 threw = true;
michael@0 445 }
michael@0 446 do_check_true(threw || !isEqualToNull);
michael@0 447
michael@0 448
michael@0 449 // Check the various components
michael@0 450 do_check_property(aTest, URI, "scheme");
michael@0 451 do_check_property(aTest, URI, "prePath");
michael@0 452 do_check_property(aTest, URI, "path");
michael@0 453 do_check_property(aTest, URI, "ref");
michael@0 454 do_check_property(aTest, URI, "port");
michael@0 455 do_check_property(aTest, URI, "username");
michael@0 456 do_check_property(aTest, URI, "password");
michael@0 457 do_check_property(aTest, URI, "host");
michael@0 458 do_check_property(aTest, URI, "specIgnoringRef");
michael@0 459 if ("hasRef" in aTest) {
michael@0 460 do_info("testing hasref: " + aTest.hasRef + " vs " + URI.hasRef);
michael@0 461 do_check_eq(aTest.hasRef, URI.hasRef);
michael@0 462 }
michael@0 463 }
michael@0 464
michael@0 465 // Test that a given URI parses correctly when we add a given ref to the end
michael@0 466 function do_test_uri_with_hash_suffix(aTest, aSuffix) {
michael@0 467 do_info("making sure caller is using suffix that starts with '#'");
michael@0 468 do_check_eq(aSuffix[0], "#");
michael@0 469
michael@0 470 var origURI = NetUtil.newURI(aTest.spec);
michael@0 471 var testURI;
michael@0 472
michael@0 473 if (aTest.relativeURI) {
michael@0 474 try {
michael@0 475 origURI = gIoService.newURI(aTest.relativeURI, null, origURI);
michael@0 476 } catch (e) {
michael@0 477 do_info("Caught error on Relative parse of " + aTest.spec + " + " + aTest.relativeURI +" Error: " + e.result);
michael@0 478 return;
michael@0 479 }
michael@0 480 try {
michael@0 481 testURI = gIoService.newURI(aSuffix, null, origURI);
michael@0 482 } catch (e) {
michael@0 483 do_info("Caught error adding suffix to " + aTest.spec + " + " + aTest.relativeURI + ", suffix " + aSuffix + " Error: " + e.result);
michael@0 484 return;
michael@0 485 }
michael@0 486 } else {
michael@0 487 testURI = NetUtil.newURI(aTest.spec + aSuffix);
michael@0 488 }
michael@0 489
michael@0 490 do_info("testing " + aTest.spec + " with '" + aSuffix + "' appended " +
michael@0 491 "equals a clone of itself");
michael@0 492 do_check_uri_eq(testURI, testURI.clone());
michael@0 493
michael@0 494 do_info("testing " + aTest.spec +
michael@0 495 " doesn't equal self with '" + aSuffix + "' appended");
michael@0 496
michael@0 497 do_check_false(origURI.equals(testURI));
michael@0 498
michael@0 499 do_info("testing " + aTest.spec +
michael@0 500 " is equalExceptRef to self with '" + aSuffix + "' appended");
michael@0 501 do_check_uri_eqExceptRef(origURI, testURI);
michael@0 502
michael@0 503 do_check_eq(testURI.hasRef, true);
michael@0 504
michael@0 505 if (!origURI.ref) {
michael@0 506 // These tests fail if origURI has a ref
michael@0 507 do_info("testing cloneIgnoringRef on " + testURI.spec +
michael@0 508 " is equal to no-ref version but not equal to ref version");
michael@0 509 var cloneNoRef = testURI.cloneIgnoringRef();
michael@0 510 do_check_uri_eq(cloneNoRef, origURI);
michael@0 511 do_check_false(cloneNoRef.equals(testURI));
michael@0 512 }
michael@0 513
michael@0 514 do_check_property(aTest, testURI, "scheme");
michael@0 515 do_check_property(aTest, testURI, "prePath");
michael@0 516 if (!origURI.ref) {
michael@0 517 // These don't work if it's a ref already because '+' doesn't give the right result
michael@0 518 do_check_property(aTest, testURI, "path",
michael@0 519 function(aStr) { return aStr + aSuffix; });
michael@0 520 do_check_property(aTest, testURI, "ref",
michael@0 521 function(aStr) { return aSuffix.substr(1); });
michael@0 522 }
michael@0 523 }
michael@0 524
michael@0 525 // Tests various ways of setting & clearing a ref on a URI.
michael@0 526 function do_test_mutate_ref(aTest, aSuffix) {
michael@0 527 do_info("making sure caller is using suffix that starts with '#'");
michael@0 528 do_check_eq(aSuffix[0], "#");
michael@0 529
michael@0 530 var refURIWithSuffix = NetUtil.newURI(aTest.spec + aSuffix);
michael@0 531 var refURIWithoutSuffix = NetUtil.newURI(aTest.spec);
michael@0 532
michael@0 533 var testURI = NetUtil.newURI(aTest.spec);
michael@0 534
michael@0 535 // First: Try setting .ref to our suffix
michael@0 536 do_info("testing that setting .ref on " + aTest.spec +
michael@0 537 " to '" + aSuffix + "' does what we expect");
michael@0 538 testURI.ref = aSuffix;
michael@0 539 do_check_uri_eq(testURI, refURIWithSuffix);
michael@0 540 do_check_uri_eqExceptRef(testURI, refURIWithoutSuffix);
michael@0 541
michael@0 542 // Now try setting .ref but leave off the initial hash (expect same result)
michael@0 543 var suffixLackingHash = aSuffix.substr(1);
michael@0 544 if (suffixLackingHash) { // (skip this our suffix was *just* a #)
michael@0 545 do_info("testing that setting .ref on " + aTest.spec +
michael@0 546 " to '" + suffixLackingHash + "' does what we expect");
michael@0 547 testURI.ref = suffixLackingHash;
michael@0 548 do_check_uri_eq(testURI, refURIWithSuffix);
michael@0 549 do_check_uri_eqExceptRef(testURI, refURIWithoutSuffix);
michael@0 550 }
michael@0 551
michael@0 552 // Now, clear .ref (should get us back the original spec)
michael@0 553 do_info("testing that clearing .ref on " + testURI.spec +
michael@0 554 " does what we expect");
michael@0 555 testURI.ref = "";
michael@0 556 do_check_uri_eq(testURI, refURIWithoutSuffix);
michael@0 557 do_check_uri_eqExceptRef(testURI, refURIWithSuffix);
michael@0 558
michael@0 559 if (!aTest.relativeURI) {
michael@0 560 // TODO: These tests don't work as-is for relative URIs.
michael@0 561
michael@0 562 // Now try setting .spec directly (including suffix) and then clearing .ref
michael@0 563 var specWithSuffix = aTest.spec + aSuffix;
michael@0 564 do_info("testing that setting spec to " +
michael@0 565 specWithSuffix + " and then clearing ref does what we expect");
michael@0 566 testURI.spec = specWithSuffix;
michael@0 567 testURI.ref = "";
michael@0 568 do_check_uri_eq(testURI, refURIWithoutSuffix);
michael@0 569 do_check_uri_eqExceptRef(testURI, refURIWithSuffix);
michael@0 570
michael@0 571 // XXX nsIJARURI throws an exception in SetPath(), so skip it for next part.
michael@0 572 if (!(testURI instanceof Ci.nsIJARURI)) {
michael@0 573 // Now try setting .path directly (including suffix) and then clearing .ref
michael@0 574 // (same as above, but with now with .path instead of .spec)
michael@0 575 testURI = NetUtil.newURI(aTest.spec);
michael@0 576
michael@0 577 var pathWithSuffix = aTest.path + aSuffix;
michael@0 578 do_info("testing that setting path to " +
michael@0 579 pathWithSuffix + " and then clearing ref does what we expect");
michael@0 580 testURI.path = pathWithSuffix;
michael@0 581 testURI.ref = "";
michael@0 582 do_check_uri_eq(testURI, refURIWithoutSuffix);
michael@0 583 do_check_uri_eqExceptRef(testURI, refURIWithSuffix);
michael@0 584
michael@0 585 // Also: make sure that clearing .path also clears .ref
michael@0 586 testURI.path = pathWithSuffix;
michael@0 587 do_info("testing that clearing path from " +
michael@0 588 pathWithSuffix + " also clears .ref");
michael@0 589 testURI.path = "";
michael@0 590 do_check_eq(testURI.ref, "");
michael@0 591 }
michael@0 592 }
michael@0 593 }
michael@0 594
michael@0 595 // Tests that normally-mutable properties can't be modified on
michael@0 596 // special URIs that are known to be immutable.
michael@0 597 function do_test_immutable(aTest) {
michael@0 598 do_check_true(aTest.immutable);
michael@0 599
michael@0 600 var URI = NetUtil.newURI(aTest.spec);
michael@0 601 // All the non-readonly attributes on nsIURI.idl:
michael@0 602 var propertiesToCheck = ["spec", "scheme", "userPass", "username", "password",
michael@0 603 "hostPort", "host", "port", "path", "ref"];
michael@0 604
michael@0 605 propertiesToCheck.forEach(function(aProperty) {
michael@0 606 var threw = false;
michael@0 607 try {
michael@0 608 URI[aProperty] = "anothervalue";
michael@0 609 } catch(e) {
michael@0 610 threw = true;
michael@0 611 }
michael@0 612
michael@0 613 do_info("testing that setting '" + aProperty +
michael@0 614 "' on immutable URI '" + aTest.spec + "' will throw");
michael@0 615 do_check_true(threw);
michael@0 616 });
michael@0 617 }
michael@0 618
michael@0 619
michael@0 620 // TEST MAIN FUNCTION
michael@0 621 // ------------------
michael@0 622 function run_test()
michael@0 623 {
michael@0 624 // UTF-8 check - From bug 622981
michael@0 625 // ASCII
michael@0 626 let base = gIoService.newURI("http://example.org/xenia?", null, null);
michael@0 627 let resolved = gIoService.newURI("?x", null, base);
michael@0 628 let expected = gIoService.newURI("http://example.org/xenia?x",
michael@0 629 null, null);
michael@0 630 do_info("Bug 662981: ACSII - comparing " + resolved.spec + " and " + expected.spec);
michael@0 631 do_check_true(resolved.equals(expected));
michael@0 632
michael@0 633 // UTF-8 character "è"
michael@0 634 // Bug 622981 was triggered by an empty query string
michael@0 635 base = gIoService.newURI("http://example.org/xènia?", null, null);
michael@0 636 resolved = gIoService.newURI("?x", null, base);
michael@0 637 expected = gIoService.newURI("http://example.org/xènia?x",
michael@0 638 null, null);
michael@0 639 do_info("Bug 662981: UTF8 - comparing " + resolved.spec + " and " + expected.spec);
michael@0 640 do_check_true(resolved.equals(expected));
michael@0 641
michael@0 642 gTests.forEach(function(aTest) {
michael@0 643 // Check basic URI functionality
michael@0 644 do_test_uri_basic(aTest);
michael@0 645
michael@0 646 if (!aTest.fail) {
michael@0 647 // Try adding various #-prefixed strings to the ends of the URIs
michael@0 648 gHashSuffixes.forEach(function(aSuffix) {
michael@0 649 do_test_uri_with_hash_suffix(aTest, aSuffix);
michael@0 650 if (!aTest.immutable) {
michael@0 651 do_test_mutate_ref(aTest, aSuffix);
michael@0 652 }
michael@0 653 });
michael@0 654
michael@0 655 // For URIs that we couldn't mutate above due to them being immutable:
michael@0 656 // Now we check that they're actually immutable.
michael@0 657 if (aTest.immutable) {
michael@0 658 do_test_immutable(aTest);
michael@0 659 }
michael@0 660 }
michael@0 661 });
michael@0 662 }

mercurial