Bug 1470455 - Add the 'expected' arguments to throws/rejects for netwerk. r?mayhemer draft
authorMark Banner <standard8@mozilla.com>
Thu, 21 Jun 2018 17:08:24 +0100
changeset 809614 ae960d69c1ebf9e45cb832e93503543d14183160
parent 809613 f32cb36dbbe8157004672c7af3c5f0cfcaa948b4
child 809615 2c8303362a3249d286442bebeb0d8fdedd3dcff8
push id113728
push userbmo:standard8@mozilla.com
push dateFri, 22 Jun 2018 15:20:15 +0000
reviewersmayhemer
bugs1470455
milestone62.0a1
Bug 1470455 - Add the 'expected' arguments to throws/rejects for netwerk. r?mayhemer MozReview-Commit-ID: 40K0YkBQfue
netwerk/test/unit/test_bug412457.js
netwerk/test/unit/test_bug652761.js
netwerk/test/unit/test_large_port.js
netwerk/test/unit/test_standardurl.js
--- a/netwerk/test/unit/test_bug412457.js
+++ b/netwerk/test/unit/test_bug412457.js
@@ -8,17 +8,18 @@ function run_test() {
 
   // escaped UTF8
   newURI = newURI.mutate().setSpec("http://%e5%8d%81.com").finalize();
   Assert.equal(newURI.asciiHost, "xn--kkr.com");
 
   // There should be only allowed characters in hostname after
   // unescaping and attempting to apply IDNA. "\x80" is illegal in
   // UTF-8, so IDNA fails, and 0x80 is illegal in DNS too.
-  Assert.throws(() => { newURI = newURI.mutate().setSpec("http://%80.com").finalize(); }, "illegal UTF character");
+  Assert.throws(() => { newURI = newURI.mutate().setSpec("http://%80.com").finalize(); },
+                /NS_ERROR_UNEXPECTED/, "illegal UTF character");
 
   // test parsing URL with all possible host terminators
   newURI = newURI.mutate().setSpec("http://example.com?foo").finalize();
   Assert.equal(newURI.asciiHost, "example.com");
 
   newURI = newURI.mutate().setSpec("http://example.com#foo").finalize();
   Assert.equal(newURI.asciiHost, "example.com");
 
@@ -36,9 +37,9 @@ function run_test() {
   newURI = newURI.mutate().setSpec("http://example.com%3bfoo").finalize();
   Assert.equal(newURI.asciiHost, "example.com%3bfoo");
   newURI = newURI.mutate().setSpec("http://example.com%3a80").finalize();
   Assert.equal(newURI.asciiHost, "example.com%3a80");
   newURI = newURI.mutate().setSpec("http://example.com%2ffoo").finalize();
   Assert.equal(newURI.asciiHost, "example.com%2ffoo");
   newURI = newURI.mutate().setSpec("http://example.com%00").finalize();
   Assert.equal(newURI.asciiHost, "example.com%00");
-}
\ No newline at end of file
+}
--- a/netwerk/test/unit/test_bug652761.js
+++ b/netwerk/test/unit/test_bug652761.js
@@ -5,13 +5,12 @@ ChromeUtils.import("resource://gre/modul
 function run_test()
 {
     // Bug 1301621 makes invalid ports throw
     Assert.throws(() => {
         var chan = NetUtil.newChannel({
           uri: "http://localhost:80000/",
           loadUsingSystemPrincipal: true
         });
-    }, "invalid port");
+    }, /NS_ERROR_MALFORMED_URI/, "invalid port");
 
     do_test_finished();
 }
-
--- a/netwerk/test/unit/test_large_port.js
+++ b/netwerk/test/unit/test_large_port.js
@@ -20,26 +20,25 @@ function run_test()
 
     // Bug 1301621 makes invalid ports throw
     Assert.throws(() => {
         url = Cc["@mozilla.org/network/standard-url-mutator;1"]
                 .createInstance(Ci.nsIStandardURLMutator)
                 .init(Ci.nsIStandardURL.URLTYPE_AUTHORITY, 65536,
                       "http://localhost", "UTF-8", null)
                 .finalize();
-    }, "invalid port during creation");
+    }, /NS_ERROR_MALFORMED_URI/, "invalid port during creation");
 
     Assert.throws(() => {
         url = url.mutate()
                  .QueryInterface(Ci.nsIStandardURLMutator)
                  .setDefaultPort(65536)
                  .finalize();
-    }, "invalid port in setDefaultPort");
+    }, /NS_ERROR_MALFORMED_URI/, "invalid port in setDefaultPort");
     Assert.throws(() => {
         url = url.mutate()
                  .setPort(65536)
                  .finalize();
-    }, "invalid port in port setter");
+    }, /NS_ERROR_MALFORMED_URI/, "invalid port in port setter");
 
     Assert.equal(url.port, -1);
     do_test_finished();
 }
-
--- a/netwerk/test/unit/test_standardurl.js
+++ b/netwerk/test/unit/test_standardurl.js
@@ -13,17 +13,17 @@ function symmetricEquality(expect, a, b)
     /* We don't check port in the loop, because it can be defaulted in
        some cases. */
     ["spec", "prePath", "scheme", "userPass", "username", "password",
      "hostPort", "host", "pathQueryRef", "filePath", "query",
      "ref", "directory", "fileName", "fileBaseName", "fileExtension"]
       .map(function(prop) {
 	dump("Testing '"+ prop + "'\n");
 	Assert.equal(a[prop], b[prop]);
-      });  
+      });
   } else {
     Assert.notEqual(a.spec, b.spec);
   }
   Assert.equal(expect, a.equals(b));
   Assert.equal(expect, b.equals(a));
 }
 
 function stringToURL(str) {
@@ -185,49 +185,69 @@ add_test(function test_ipv6()
   Assert.equal(url.hostPort, "0.0.7.209:1");
   run_next_test();
 });
 
 add_test(function test_ipv6_fail()
 {
   var url = stringToURL("http://example.com");
 
-  Assert.throws(() => { url = url.mutate().setHost("2001::1").finalize(); }, "missing brackets");
-  Assert.throws(() => { url = url.mutate().setHost("[2001::1]:20").finalize(); }, "url.host with port");
-  Assert.throws(() => { url = url.mutate().setHost("[2001::1").finalize(); }, "missing last bracket");
-  Assert.throws(() => { url = url.mutate().setHost("2001::1]").finalize(); }, "missing first bracket");
-  Assert.throws(() => { url = url.mutate().setHost("2001[::1]").finalize(); }, "bad bracket position");
-  Assert.throws(() => { url = url.mutate().setHost("[]").finalize(); }, "empty IPv6 address");
-  Assert.throws(() => { url = url.mutate().setHost("[hello]").finalize(); }, "bad IPv6 address");
-  Assert.throws(() => { url = url.mutate().setHost("[192.168.1.1]").finalize(); }, "bad IPv6 address");
-  Assert.throws(() => { url = url.mutate().setHostPort("2001::1").finalize(); }, "missing brackets");
-  Assert.throws(() => { url = url.mutate().setHostPort("[2001::1]30").finalize(); }, "missing : after IP");
-  Assert.throws(() => { url = url.mutate().setHostPort("[2001:1]").finalize(); }, "bad IPv6 address");
-  Assert.throws(() => { url = url.mutate().setHostPort("[2001:1]10").finalize(); }, "bad IPv6 address");
-  Assert.throws(() => { url = url.mutate().setHostPort("[2001:1]10:20").finalize(); }, "bad IPv6 address");
-  Assert.throws(() => { url = url.mutate().setHostPort("[2001:1]:10:20").finalize(); }, "bad IPv6 address");
-  Assert.throws(() => { url = url.mutate().setHostPort("[2001:1").finalize(); }, "bad IPv6 address");
-  Assert.throws(() => { url = url.mutate().setHostPort("2001]:1").finalize(); }, "bad IPv6 address");
-  Assert.throws(() => { url = url.mutate().setHostPort("2001:1]").finalize(); }, "bad IPv6 address");
-  Assert.throws(() => { url = url.mutate().setHostPort("").finalize(); }, "Empty hostPort should fail");
+  Assert.throws(() => { url = url.mutate().setHost("2001::1").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "missing brackets");
+  Assert.throws(() => { url = url.mutate().setHost("[2001::1]:20").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "url.host with port");
+  Assert.throws(() => { url = url.mutate().setHost("[2001::1").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "missing last bracket");
+  Assert.throws(() => { url = url.mutate().setHost("2001::1]").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "missing first bracket");
+  Assert.throws(() => { url = url.mutate().setHost("2001[::1]").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "bad bracket position");
+  Assert.throws(() => { url = url.mutate().setHost("[]").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "empty IPv6 address");
+  Assert.throws(() => { url = url.mutate().setHost("[hello]").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "bad IPv6 address");
+  Assert.throws(() => { url = url.mutate().setHost("[192.168.1.1]").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "bad IPv6 address");
+  Assert.throws(() => { url = url.mutate().setHostPort("2001::1").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "missing brackets");
+  Assert.throws(() => { url = url.mutate().setHostPort("[2001::1]30").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "missing : after IP");
+  Assert.throws(() => { url = url.mutate().setHostPort("[2001:1]").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "bad IPv6 address");
+  Assert.throws(() => { url = url.mutate().setHostPort("[2001:1]10").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "bad IPv6 address");
+  Assert.throws(() => { url = url.mutate().setHostPort("[2001:1]10:20").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "bad IPv6 address");
+  Assert.throws(() => { url = url.mutate().setHostPort("[2001:1]:10:20").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "bad IPv6 address");
+  Assert.throws(() => { url = url.mutate().setHostPort("[2001:1").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "bad IPv6 address");
+  Assert.throws(() => { url = url.mutate().setHostPort("2001]:1").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "bad IPv6 address");
+  Assert.throws(() => { url = url.mutate().setHostPort("2001:1]").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "bad IPv6 address");
+  Assert.throws(() => { url = url.mutate().setHostPort("").finalize(); },
+                /NS_ERROR_UNEXPECTED/, "Empty hostPort should fail");
 
   // These checks used to fail, but now don't (see bug 1433958 comment 57)
   url = url.mutate().setHostPort("[2001::1]:").finalize();
   Assert.equal(url.spec, "http://[2001::1]/");
   url = url.mutate().setHostPort("[2002::1]:bad").finalize();
   Assert.equal(url.spec, "http://[2002::1]/");
 
   run_next_test();
 });
 
 add_test(function test_clearedSpec()
 {
   var url = stringToURL("http://example.com/path");
-  Assert.throws(() => { url = url.mutate().setSpec("http: example").finalize(); }, "set bad spec");
-  Assert.throws(() => { url = url.mutate().setSpec("").finalize(); }, "set empty spec");
+  Assert.throws(() => { url = url.mutate().setSpec("http: example").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "set bad spec");
+  Assert.throws(() => { url = url.mutate().setSpec("").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "set empty spec");
   Assert.equal(url.spec, "http://example.com/path");
   url = url.mutate().setHost("allizom.org").finalize().QueryInterface(Ci.nsIURL);
 
   var ref = stringToURL("http://allizom.org/path");
   symmetricEquality(true, url, ref);
   run_next_test();
 });
 
@@ -361,31 +381,34 @@ add_test(function test_backslashReplacem
   Assert.equal(url.host, "test.com");
   Assert.equal(url.pathQueryRef, "/example.org/path/to/file");
 
   run_next_test();
 });
 
 add_test(function test_authority_host()
 {
-  Assert.throws(() => { stringToURL("http:"); }, "TYPE_AUTHORITY should have host");
-  Assert.throws(() => { stringToURL("http:///"); }, "TYPE_AUTHORITY should have host");
+  Assert.throws(() => { stringToURL("http:"); },
+                /NS_ERROR_MALFORMED_URI/, "TYPE_AUTHORITY should have host");
+  Assert.throws(() => { stringToURL("http:///"); },
+                /NS_ERROR_MALFORMED_URI/, "TYPE_AUTHORITY should have host");
 
   run_next_test();
 });
 
 add_test(function test_trim_C0_and_space()
 {
   var url = stringToURL("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f http://example.com/ \x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f ");
   Assert.equal(url.spec, "http://example.com/");
   url = url.mutate()
            .setSpec("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f http://test.com/ \x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f ")
            .finalize();
   Assert.equal(url.spec, "http://test.com/");
-  Assert.throws(() => { url = url.mutate().setSpec("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19 ").finalize(); }, "set empty spec");
+  Assert.throws(() => { url = url.mutate().setSpec("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19 ").finalize(); },
+                /NS_ERROR_MALFORMED_URI/, "set empty spec");
   run_next_test();
 });
 
 // This tests that C0-and-space characters in the path, query and ref are
 // percent encoded.
 add_test(function test_encode_C0_and_space()
 {
   function toHex(d) {
@@ -477,20 +500,22 @@ add_test(function test_ipv4Normalize()
   Assert.equal(url.host, "123");
 
   run_next_test();
 });
 
 add_test(function test_invalidHostChars() {
   var url = stringToURL("http://example.org/");
   for (let i = 0; i <= 0x20; i++) {
-    Assert.throws(() => { url = url.mutate().setHost("a" + String.fromCharCode(i) + "b").finalize(); }, "Trying to set hostname containing char code: " + i);
+    Assert.throws(() => { url = url.mutate().setHost("a" + String.fromCharCode(i) + "b").finalize(); },
+                  /NS_ERROR_MALFORMED_URI/, "Trying to set hostname containing char code: " + i);
   }
   for (let c of "@[]*<>|:\"") {
-    Assert.throws(() => { url = url.mutate().setHost("a" + c).finalize(); }, "Trying to set hostname containing char: " + c);
+    Assert.throws(() => { url = url.mutate().setHost("a" + c).finalize(); },
+                  /NS_ERROR_MALFORMED_URI/, "Trying to set hostname containing char: " + c);
   }
 
   // It also can't contain /, \, #, ?, but we treat these characters as
   // hostname separators, so there is no way to set them and fail.
   run_next_test();
 });
 
 add_test(function test_normalize_ipv6() {