netwerk/test/browser/browser_NetUtil.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/browser/browser_NetUtil.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,106 @@
     1.4 +/*
     1.5 +Any copyright is dedicated to the Public Domain.
     1.6 +http://creativecommons.org/publicdomain/zero/1.0/
     1.7 +*/
     1.8 +
     1.9 +Components.utils.import("resource://gre/modules/NetUtil.jsm");
    1.10 +
    1.11 +function test() {
    1.12 +  waitForExplicitFinish();
    1.13 +
    1.14 +  // We overload this test to include verifying that httpd.js is
    1.15 +  // importable as a testing-only JS module.
    1.16 +  Components.utils.import("resource://testing-common/httpd.js", {});
    1.17 +
    1.18 +  nextTest();
    1.19 +}
    1.20 +
    1.21 +function nextTest() {
    1.22 +  if (tests.length)
    1.23 +    executeSoon(tests.shift());
    1.24 +  else
    1.25 +    executeSoon(finish);
    1.26 +}
    1.27 +
    1.28 +var tests = [
    1.29 +  test_asyncFetchBadCert,
    1.30 +];
    1.31 +
    1.32 +var gCertErrorDialogShown = 0;
    1.33 +
    1.34 +// We used to show a dialog box by default when we encountered an SSL
    1.35 +// certificate error. Now we treat these errors just like other
    1.36 +// networking errors; the dialog is no longer shown.
    1.37 +function test_asyncFetchBadCert() {
    1.38 +  let listener = new WindowListener("chrome://pippki/content/certerror.xul", function (domwindow) {
    1.39 +    gCertErrorDialogShown++;
    1.40 +
    1.41 +    // Close the dialog
    1.42 +    domwindow.document.documentElement.cancelDialog();
    1.43 +  });
    1.44 +
    1.45 +  Services.wm.addListener(listener);
    1.46 +
    1.47 +  // Try a load from an untrusted cert, with errors supressed
    1.48 +  NetUtil.asyncFetch("https://untrusted.example.com", function (aInputStream, aStatusCode, aRequest) {
    1.49 +    ok(!Components.isSuccessCode(aStatusCode), "request failed");
    1.50 +    ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
    1.51 +
    1.52 +    is(gCertErrorDialogShown, 0, "cert error dialog was not shown");
    1.53 +
    1.54 +    // Now try again with a channel whose notificationCallbacks doesn't suprress errors
    1.55 +    let channel = NetUtil.newChannel("https://untrusted.example.com");
    1.56 +    channel.notificationCallbacks = {
    1.57 +      QueryInterface: XPCOMUtils.generateQI([Ci.nsIProgressEventSink,
    1.58 +                                             Ci.nsIInterfaceRequestor]),
    1.59 +      getInterface: function (aIID) this.QueryInterface(aIID),
    1.60 +      onProgress: function () {},
    1.61 +      onStatus: function () {}
    1.62 +    };
    1.63 +    NetUtil.asyncFetch(channel, function (aInputStream, aStatusCode, aRequest) {
    1.64 +      ok(!Components.isSuccessCode(aStatusCode), "request failed");
    1.65 +      ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
    1.66 +
    1.67 +      is(gCertErrorDialogShown, 0, "cert error dialog was not shown");
    1.68 +
    1.69 +      // Now try a valid request
    1.70 +      NetUtil.asyncFetch("https://example.com", function (aInputStream, aStatusCode, aRequest) {
    1.71 +        info("aStatusCode for valid request: " + aStatusCode);
    1.72 +        ok(Components.isSuccessCode(aStatusCode), "request succeeded");
    1.73 +        ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");
    1.74 +        ok(aRequest.requestSucceeded, "HTTP request succeeded");
    1.75 +  
    1.76 +        is(gCertErrorDialogShown, 0, "cert error dialog was not shown");
    1.77 +
    1.78 +        Services.wm.removeListener(listener);
    1.79 +        nextTest();
    1.80 +      });
    1.81 +    });
    1.82 +
    1.83 +  });
    1.84 +}
    1.85 +
    1.86 +function WindowListener(aURL, aCallback) {
    1.87 +  this.callback = aCallback;
    1.88 +  this.url = aURL;
    1.89 +}
    1.90 +WindowListener.prototype = {
    1.91 +  onOpenWindow: function(aXULWindow) {
    1.92 +    var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
    1.93 +                              .getInterface(Ci.nsIDOMWindow);
    1.94 +    var self = this;
    1.95 +    domwindow.addEventListener("load", function() {
    1.96 +      domwindow.removeEventListener("load", arguments.callee, false);
    1.97 +
    1.98 +      if (domwindow.document.location.href != self.url)
    1.99 +        return;
   1.100 +
   1.101 +      // Allow other window load listeners to execute before passing to callback
   1.102 +      executeSoon(function() {
   1.103 +        self.callback(domwindow);
   1.104 +      });
   1.105 +    }, false);
   1.106 +  },
   1.107 +  onCloseWindow: function(aXULWindow) {},
   1.108 +  onWindowTitleChange: function(aXULWindow, aNewTitle) {}
   1.109 +}

mercurial