Bug 945240 - Add unit test for new behaviour r=mcmanus draft
authorValentin Gosu <valentin.gosu@gmail.com>
Tue, 11 Jul 2017 19:10:37 +0200
changeset 607142 6bc9ca82227a6bc9b273855590138cd2686ca5dc
parent 607141 18cd23a849830ed715573b544d086a1306dbe53c
child 636951 a2d48d0df9e992136dc936dc38562deb646b0f76
push id67902
push uservalentin.gosu@gmail.com
push dateTue, 11 Jul 2017 21:59:36 +0000
reviewersmcmanus
bugs945240
milestone56.0a1
Bug 945240 - Add unit test for new behaviour r=mcmanus MozReview-Commit-ID: In47wgDKbhR
netwerk/test/unit/test_standardurl.js
--- a/netwerk/test/unit/test_standardurl.js
+++ b/netwerk/test/unit/test_standardurl.js
@@ -1,14 +1,15 @@
 "use strict";
 
 const StandardURL = Components.Constructor("@mozilla.org/network/standard-url;1",
                                            "nsIStandardURL",
                                            "init");
 const nsIStandardURL = Components.interfaces.nsIStandardURL;
+const gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
 
 function symmetricEquality(expect, a, b)
 {
   /* Use if/else instead of |do_check_eq(expect, a.spec == b.spec)| so
      that we get the specs output on the console if the check fails.
    */
   if (expect) {
     /* Check all the sub-pieces too, since that can help with
@@ -484,8 +485,59 @@ add_test(function test_emptyPassword() {
   url.userPass = "xxxxx:yyyyyy";
   do_check_eq(url.spec, "http://xxxxx:yyyyyy@example.com/");
   url.userPass = "z:";
   do_check_eq(url.spec, "http://z@example.com/");
   url.password = "ppppppppppp";
   do_check_eq(url.spec, "http://z:ppppppppppp@example.com/");
   run_next_test();
 });
+
+do_register_cleanup(function () {
+  gPrefs.clearUserPref("network.standard-url.punycode-host");
+});
+
+add_test(function test_idna_host() {
+  // See bug 945240 - this test makes sure that URLs return a punycode hostname
+  // when the pref is set, or unicode otherwise.
+
+  // First we test that the old behaviour still works properly for all methods
+  // that return strings containing the hostname
+
+  gPrefs.setBoolPref("network.standard-url.punycode-host", false);
+  let url = stringToURL("http://user:password@ält.example.org:8080/path?query#etc");
+
+  equal(url.host, "ält.example.org");
+  equal(url.hostPort, "ält.example.org:8080");
+  equal(url.prePath, "http://user:password@ält.example.org:8080");
+  equal(url.spec, "http://user:password@ält.example.org:8080/path?query#etc");
+  equal(url.specIgnoringRef, "http://user:password@ält.example.org:8080/path?query");
+  equal(url.QueryInterface(Components.interfaces.nsISensitiveInfoHiddenURI).getSensitiveInfoHiddenSpec(), "http://user:****@ält.example.org:8080/path?query#etc");
+
+  equal(url.displayHost, "ält.example.org");
+  equal(url.displayHostPort, "ält.example.org:8080");
+  equal(url.displaySpec, "http://user:password@ält.example.org:8080/path?query#etc");
+
+  equal(url.asciiHost, "xn--lt-uia.example.org");
+  equal(url.asciiHostPort, "xn--lt-uia.example.org:8080");
+  equal(url.asciiSpec, "http://user:password@xn--lt-uia.example.org:8080/path?query#etc");
+
+  // We also check that the default behaviour changes once we filp the pref
+  gPrefs.setBoolPref("network.standard-url.punycode-host", true);
+
+  url = stringToURL("http://user:password@ält.example.org:8080/path?query#etc");
+  equal(url.host, "xn--lt-uia.example.org");
+  equal(url.hostPort, "xn--lt-uia.example.org:8080");
+  equal(url.prePath, "http://user:password@xn--lt-uia.example.org:8080");
+  equal(url.spec, "http://user:password@xn--lt-uia.example.org:8080/path?query#etc");
+  equal(url.specIgnoringRef, "http://user:password@xn--lt-uia.example.org:8080/path?query");
+  equal(url.QueryInterface(Components.interfaces.nsISensitiveInfoHiddenURI).getSensitiveInfoHiddenSpec(), "http://user:****@xn--lt-uia.example.org:8080/path?query#etc");
+
+  equal(url.displayHost, "ält.example.org");
+  equal(url.displayHostPort, "ält.example.org:8080");
+  equal(url.displaySpec, "http://user:password@ält.example.org:8080/path?query#etc");
+
+  equal(url.asciiHost, "xn--lt-uia.example.org");
+  equal(url.asciiHostPort, "xn--lt-uia.example.org:8080");
+  equal(url.asciiSpec, "http://user:password@xn--lt-uia.example.org:8080/path?query#etc");
+
+  run_next_test();
+});