summaryrefslogtreecommitdiffstats
path: root/netwerk/test/browser/browser_NetUtil.js
blob: 791498f031d9a1df7726362c22e61ed4b2303256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
*/
"use strict";

function test() {
  waitForExplicitFinish();

  // We overload this test to include verifying that httpd.js is
  // importable as a testing-only JS module.
  ChromeUtils.importESModule("resource://testing-common/httpd.sys.mjs");

  nextTest();
}

function nextTest() {
  if (tests.length) {
    executeSoon(tests.shift());
  } else {
    executeSoon(finish);
  }
}

var tests = [test_asyncFetchBadCert];

function test_asyncFetchBadCert() {
  // Try a load from an untrusted cert, with errors supressed
  NetUtil.asyncFetch(
    {
      uri: "https://untrusted.example.com",
      loadUsingSystemPrincipal: true,
    },
    function (aInputStream, aStatusCode, aRequest) {
      ok(!Components.isSuccessCode(aStatusCode), "request failed");
      ok(aRequest instanceof Ci.nsIHttpChannel, "request is an nsIHttpChannel");

      // Now try again with a channel whose notificationCallbacks doesn't suprress errors
      let channel = NetUtil.newChannel({
        uri: "https://untrusted.example.com",
        loadUsingSystemPrincipal: true,
      });
      channel.notificationCallbacks = {
        QueryInterface: ChromeUtils.generateQI([
          "nsIProgressEventSink",
          "nsIInterfaceRequestor",
        ]),
        getInterface(aIID) {
          return this.QueryInterface(aIID);
        },
        onProgress() {},
        onStatus() {},
      };
      NetUtil.asyncFetch(
        channel,
        function (aInputStream, aStatusCode, aRequest) {
          ok(!Components.isSuccessCode(aStatusCode), "request failed");
          ok(
            aRequest instanceof Ci.nsIHttpChannel,
            "request is an nsIHttpChannel"
          );

          // Now try a valid request
          NetUtil.asyncFetch(
            {
              uri: "https://example.com",
              loadUsingSystemPrincipal: true,
            },
            function (aInputStream, aStatusCode, aRequest) {
              info("aStatusCode for valid request: " + aStatusCode);
              ok(Components.isSuccessCode(aStatusCode), "request succeeded");
              ok(
                aRequest instanceof Ci.nsIHttpChannel,
                "request is an nsIHttpChannel"
              );
              ok(aRequest.requestSucceeded, "HTTP request succeeded");

              nextTest();
            }
          );
        }
      );
    }
  );
}

function WindowListener(aURL, aCallback) {
  this.callback = aCallback;
  this.url = aURL;
}
WindowListener.prototype = {
  onOpenWindow(aXULWindow) {
    var domwindow = aXULWindow.docShell.domWindow;
    var self = this;
    domwindow.addEventListener(
      "load",
      function () {
        if (domwindow.document.location.href != self.url) {
          return;
        }

        // Allow other window load listeners to execute before passing to callback
        executeSoon(function () {
          self.callback(domwindow);
        });
      },
      { once: true }
    );
  },
  onCloseWindow() {},
};