netwerk/test/unit/test_standardurl.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     1 const StandardURL = Components.Constructor("@mozilla.org/network/standard-url;1",
     2                                            "nsIStandardURL",
     3                                            "init");
     4 const nsIStandardURL = Components.interfaces.nsIStandardURL;
     6 function symmetricEquality(expect, a, b)
     7 {
     8   /* Use if/else instead of |do_check_eq(expect, a.spec == b.spec)| so
     9      that we get the specs output on the console if the check fails.
    10    */
    11   if (expect) {
    12     /* Check all the sub-pieces too, since that can help with
    13        debugging cases when equals() returns something unexpected */
    14     /* We don't check port in the loop, because it can be defaulted in
    15        some cases. */
    16     ["spec", "prePath", "scheme", "userPass", "username", "password",
    17      "hostPort", "host", "path", "filePath", "param", "query",
    18      "ref", "directory", "fileName", "fileBaseName", "fileExtension"]
    19       .map(function(prop) {
    20 	dump("Testing '"+ prop + "'\n");
    21 	do_check_eq(a[prop], b[prop]);
    22       });  
    23   } else {
    24     do_check_neq(a.spec, b.spec);
    25   }
    26   do_check_eq(expect, a.equals(b));
    27   do_check_eq(expect, b.equals(a));
    28 }
    30 function stringToURL(str) {
    31   return (new StandardURL(nsIStandardURL.URLTYPE_AUTHORITY, 80,
    32 			 str, "UTF-8", null))
    33          .QueryInterface(Components.interfaces.nsIURL);
    34 }
    36 function pairToURLs(pair) {
    37   do_check_eq(pair.length, 2);
    38   return pair.map(stringToURL);
    39 }
    41 function test_setEmptyPath()
    42 {
    43   var pairs =
    44     [
    45      ["http://example.com", "http://example.com/tests/dom/tests"],
    46      ["http://example.com:80", "http://example.com/tests/dom/tests"],
    47      ["http://example.com:80/", "http://example.com/tests/dom/test"],
    48      ["http://example.com/", "http://example.com/tests/dom/tests"],
    49      ["http://example.com/a", "http://example.com/tests/dom/tests"],
    50      ["http://example.com:80/a", "http://example.com/tests/dom/tests"],
    51     ].map(pairToURLs);
    53   for each (var [provided, target] in pairs)
    54   {
    55     symmetricEquality(false, target, provided);
    57     provided.path = "";
    58     target.path = "";
    60     do_check_eq(provided.spec, target.spec);
    61     symmetricEquality(true, target, provided);
    62   }
    63 }
    65 function test_setQuery()
    66 {
    67   var pairs =
    68     [
    69      ["http://example.com", "http://example.com/?foo"],
    70      ["http://example.com/bar", "http://example.com/bar?foo"],
    71      ["http://example.com#bar", "http://example.com/?foo#bar"],
    72      ["http://example.com/#bar", "http://example.com/?foo#bar"],
    73      ["http://example.com/?longerthanfoo#bar", "http://example.com/?foo#bar"],
    74      ["http://example.com/?longerthanfoo", "http://example.com/?foo"],
    75      /* And one that's nonempty but shorter than "foo" */
    76      ["http://example.com/?f#bar", "http://example.com/?foo#bar"],
    77      ["http://example.com/?f", "http://example.com/?foo"],
    78     ].map(pairToURLs);
    80   for each (var [provided, target] in pairs) {
    81     symmetricEquality(false, provided, target);
    83     provided.query = "foo";
    85     do_check_eq(provided.spec, target.spec);
    86     symmetricEquality(true, provided, target);
    87   }
    89   [provided, target] =
    90     ["http://example.com/#", "http://example.com/?foo#bar"].map(stringToURL);
    91   symmetricEquality(false, provided, target);
    92   provided.query = "foo";
    93   symmetricEquality(false, provided, target);
    95   var newProvided = Components.classes["@mozilla.org/network/io-service;1"]
    96                               .getService(Components.interfaces.nsIIOService)
    97                               .newURI("#bar", null, provided)
    98                               .QueryInterface(Components.interfaces.nsIURL);
   100   do_check_eq(newProvided.spec, target.spec);
   101   symmetricEquality(true, newProvided, target);
   103 }
   105 function test_setRef()
   106 {
   107   var tests =
   108     [
   109      ["http://example.com",      "", "http://example.com/"],
   110      ["http://example.com:80",   "", "http://example.com:80/"],
   111      ["http://example.com:80/",  "", "http://example.com:80/"],
   112      ["http://example.com/",     "", "http://example.com/"],
   113      ["http://example.com/a",    "", "http://example.com/a"],
   114      ["http://example.com:80/a", "", "http://example.com:80/a"],
   116      ["http://example.com",      "x", "http://example.com/#x"],
   117      ["http://example.com:80",   "x", "http://example.com:80/#x"],
   118      ["http://example.com:80/",  "x", "http://example.com:80/#x"],
   119      ["http://example.com/",     "x", "http://example.com/#x"],
   120      ["http://example.com/a",    "x", "http://example.com/a#x"],
   121      ["http://example.com:80/a", "x", "http://example.com:80/a#x"],
   123      ["http://example.com",      "xx", "http://example.com/#xx"],
   124      ["http://example.com:80",   "xx", "http://example.com:80/#xx"],
   125      ["http://example.com:80/",  "xx", "http://example.com:80/#xx"],
   126      ["http://example.com/",     "xx", "http://example.com/#xx"],
   127      ["http://example.com/a",    "xx", "http://example.com/a#xx"],
   128      ["http://example.com:80/a", "xx", "http://example.com:80/a#xx"],
   130      ["http://example.com",      "xxxxxxxxxxxxxx", "http://example.com/#xxxxxxxxxxxxxx"],
   131      ["http://example.com:80",   "xxxxxxxxxxxxxx", "http://example.com:80/#xxxxxxxxxxxxxx"],
   132      ["http://example.com:80/",  "xxxxxxxxxxxxxx", "http://example.com:80/#xxxxxxxxxxxxxx"],
   133      ["http://example.com/",     "xxxxxxxxxxxxxx", "http://example.com/#xxxxxxxxxxxxxx"],
   134      ["http://example.com/a",    "xxxxxxxxxxxxxx", "http://example.com/a#xxxxxxxxxxxxxx"],
   135      ["http://example.com:80/a", "xxxxxxxxxxxxxx", "http://example.com:80/a#xxxxxxxxxxxxxx"],
   136     ];
   138   for each (var [before, ref, result] in tests)
   139   {
   140     /* Test1: starting with empty ref */
   141     var a = stringToURL(before);
   142     a.ref = ref;
   143     var b = stringToURL(result);
   145     do_check_eq(a.spec, b.spec);
   146     do_check_eq(ref, b.ref);
   147     symmetricEquality(true, a, b);
   149     /* Test2: starting with non-empty */
   150     a.ref = "yyyy";
   151     var c = stringToURL(before);
   152     c.ref = "yyyy";
   153     symmetricEquality(true, a, c);
   155     /* Test3: reset the ref */
   156     a.ref = "";
   157     symmetricEquality(true, a, stringToURL(before));
   159     /* Test4: verify again after reset */
   160     a.ref = ref;
   161     symmetricEquality(true, a, b);
   162   }
   163 }
   165 function run_test()
   166 {
   167   test_setEmptyPath();
   168   test_setQuery();
   169   test_setRef();
   170 }

mercurial