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