netwerk/test/unit/test_standardurl.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

Back out 97036ab72558 which inappropriately compared turds to third parties.

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

mercurial