netwerk/test/unit/test_standardurl.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_standardurl.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     1.4 +const StandardURL = Components.Constructor("@mozilla.org/network/standard-url;1",
     1.5 +                                           "nsIStandardURL",
     1.6 +                                           "init");
     1.7 +const nsIStandardURL = Components.interfaces.nsIStandardURL;
     1.8 +
     1.9 +function symmetricEquality(expect, a, b)
    1.10 +{
    1.11 +  /* Use if/else instead of |do_check_eq(expect, a.spec == b.spec)| so
    1.12 +     that we get the specs output on the console if the check fails.
    1.13 +   */
    1.14 +  if (expect) {
    1.15 +    /* Check all the sub-pieces too, since that can help with
    1.16 +       debugging cases when equals() returns something unexpected */
    1.17 +    /* We don't check port in the loop, because it can be defaulted in
    1.18 +       some cases. */
    1.19 +    ["spec", "prePath", "scheme", "userPass", "username", "password",
    1.20 +     "hostPort", "host", "path", "filePath", "param", "query",
    1.21 +     "ref", "directory", "fileName", "fileBaseName", "fileExtension"]
    1.22 +      .map(function(prop) {
    1.23 +	dump("Testing '"+ prop + "'\n");
    1.24 +	do_check_eq(a[prop], b[prop]);
    1.25 +      });  
    1.26 +  } else {
    1.27 +    do_check_neq(a.spec, b.spec);
    1.28 +  }
    1.29 +  do_check_eq(expect, a.equals(b));
    1.30 +  do_check_eq(expect, b.equals(a));
    1.31 +}
    1.32 +
    1.33 +function stringToURL(str) {
    1.34 +  return (new StandardURL(nsIStandardURL.URLTYPE_AUTHORITY, 80,
    1.35 +			 str, "UTF-8", null))
    1.36 +         .QueryInterface(Components.interfaces.nsIURL);
    1.37 +}
    1.38 +
    1.39 +function pairToURLs(pair) {
    1.40 +  do_check_eq(pair.length, 2);
    1.41 +  return pair.map(stringToURL);
    1.42 +}
    1.43 +
    1.44 +function test_setEmptyPath()
    1.45 +{
    1.46 +  var pairs =
    1.47 +    [
    1.48 +     ["http://example.com", "http://example.com/tests/dom/tests"],
    1.49 +     ["http://example.com:80", "http://example.com/tests/dom/tests"],
    1.50 +     ["http://example.com:80/", "http://example.com/tests/dom/test"],
    1.51 +     ["http://example.com/", "http://example.com/tests/dom/tests"],
    1.52 +     ["http://example.com/a", "http://example.com/tests/dom/tests"],
    1.53 +     ["http://example.com:80/a", "http://example.com/tests/dom/tests"],
    1.54 +    ].map(pairToURLs);
    1.55 +
    1.56 +  for each (var [provided, target] in pairs)
    1.57 +  {
    1.58 +    symmetricEquality(false, target, provided);
    1.59 +
    1.60 +    provided.path = "";
    1.61 +    target.path = "";
    1.62 +
    1.63 +    do_check_eq(provided.spec, target.spec);
    1.64 +    symmetricEquality(true, target, provided);
    1.65 +  }
    1.66 +}
    1.67 +
    1.68 +function test_setQuery()
    1.69 +{
    1.70 +  var pairs =
    1.71 +    [
    1.72 +     ["http://example.com", "http://example.com/?foo"],
    1.73 +     ["http://example.com/bar", "http://example.com/bar?foo"],
    1.74 +     ["http://example.com#bar", "http://example.com/?foo#bar"],
    1.75 +     ["http://example.com/#bar", "http://example.com/?foo#bar"],
    1.76 +     ["http://example.com/?longerthanfoo#bar", "http://example.com/?foo#bar"],
    1.77 +     ["http://example.com/?longerthanfoo", "http://example.com/?foo"],
    1.78 +     /* And one that's nonempty but shorter than "foo" */
    1.79 +     ["http://example.com/?f#bar", "http://example.com/?foo#bar"],
    1.80 +     ["http://example.com/?f", "http://example.com/?foo"],
    1.81 +    ].map(pairToURLs);
    1.82 +
    1.83 +  for each (var [provided, target] in pairs) {
    1.84 +    symmetricEquality(false, provided, target);
    1.85 +
    1.86 +    provided.query = "foo";
    1.87 +
    1.88 +    do_check_eq(provided.spec, target.spec);
    1.89 +    symmetricEquality(true, provided, target);
    1.90 +  }
    1.91 +
    1.92 +  [provided, target] =
    1.93 +    ["http://example.com/#", "http://example.com/?foo#bar"].map(stringToURL);
    1.94 +  symmetricEquality(false, provided, target);
    1.95 +  provided.query = "foo";
    1.96 +  symmetricEquality(false, provided, target);
    1.97 +
    1.98 +  var newProvided = Components.classes["@mozilla.org/network/io-service;1"]
    1.99 +                              .getService(Components.interfaces.nsIIOService)
   1.100 +                              .newURI("#bar", null, provided)
   1.101 +                              .QueryInterface(Components.interfaces.nsIURL);
   1.102 +
   1.103 +  do_check_eq(newProvided.spec, target.spec);
   1.104 +  symmetricEquality(true, newProvided, target);
   1.105 +
   1.106 +}
   1.107 +		      
   1.108 +function test_setRef()
   1.109 +{
   1.110 +  var tests =
   1.111 +    [
   1.112 +     ["http://example.com",      "", "http://example.com/"],
   1.113 +     ["http://example.com:80",   "", "http://example.com:80/"],
   1.114 +     ["http://example.com:80/",  "", "http://example.com:80/"],
   1.115 +     ["http://example.com/",     "", "http://example.com/"],
   1.116 +     ["http://example.com/a",    "", "http://example.com/a"],
   1.117 +     ["http://example.com:80/a", "", "http://example.com:80/a"],
   1.118 +
   1.119 +     ["http://example.com",      "x", "http://example.com/#x"],
   1.120 +     ["http://example.com:80",   "x", "http://example.com:80/#x"],
   1.121 +     ["http://example.com:80/",  "x", "http://example.com:80/#x"],
   1.122 +     ["http://example.com/",     "x", "http://example.com/#x"],
   1.123 +     ["http://example.com/a",    "x", "http://example.com/a#x"],
   1.124 +     ["http://example.com:80/a", "x", "http://example.com:80/a#x"],
   1.125 +
   1.126 +     ["http://example.com",      "xx", "http://example.com/#xx"],
   1.127 +     ["http://example.com:80",   "xx", "http://example.com:80/#xx"],
   1.128 +     ["http://example.com:80/",  "xx", "http://example.com:80/#xx"],
   1.129 +     ["http://example.com/",     "xx", "http://example.com/#xx"],
   1.130 +     ["http://example.com/a",    "xx", "http://example.com/a#xx"],
   1.131 +     ["http://example.com:80/a", "xx", "http://example.com:80/a#xx"],
   1.132 +
   1.133 +     ["http://example.com",      "xxxxxxxxxxxxxx", "http://example.com/#xxxxxxxxxxxxxx"],
   1.134 +     ["http://example.com:80",   "xxxxxxxxxxxxxx", "http://example.com:80/#xxxxxxxxxxxxxx"],
   1.135 +     ["http://example.com:80/",  "xxxxxxxxxxxxxx", "http://example.com:80/#xxxxxxxxxxxxxx"],
   1.136 +     ["http://example.com/",     "xxxxxxxxxxxxxx", "http://example.com/#xxxxxxxxxxxxxx"],
   1.137 +     ["http://example.com/a",    "xxxxxxxxxxxxxx", "http://example.com/a#xxxxxxxxxxxxxx"],
   1.138 +     ["http://example.com:80/a", "xxxxxxxxxxxxxx", "http://example.com:80/a#xxxxxxxxxxxxxx"],
   1.139 +    ];
   1.140 +
   1.141 +  for each (var [before, ref, result] in tests)
   1.142 +  {
   1.143 +    /* Test1: starting with empty ref */
   1.144 +    var a = stringToURL(before);
   1.145 +    a.ref = ref;
   1.146 +    var b = stringToURL(result);
   1.147 +
   1.148 +    do_check_eq(a.spec, b.spec);
   1.149 +    do_check_eq(ref, b.ref);
   1.150 +    symmetricEquality(true, a, b);
   1.151 +
   1.152 +    /* Test2: starting with non-empty */
   1.153 +    a.ref = "yyyy";
   1.154 +    var c = stringToURL(before);
   1.155 +    c.ref = "yyyy";
   1.156 +    symmetricEquality(true, a, c);
   1.157 +
   1.158 +    /* Test3: reset the ref */
   1.159 +    a.ref = "";
   1.160 +    symmetricEquality(true, a, stringToURL(before));
   1.161 +
   1.162 +    /* Test4: verify again after reset */
   1.163 +    a.ref = ref;
   1.164 +    symmetricEquality(true, a, b);
   1.165 +  }
   1.166 +}
   1.167 +
   1.168 +function run_test()
   1.169 +{
   1.170 +  test_setEmptyPath();
   1.171 +  test_setQuery();
   1.172 +  test_setRef();
   1.173 +}

mercurial