summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_network_activity.js
blob: 77058dbf77e05729a642f9de8863310ec9085f88 (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
// test for networkactivity
//

"use strict";

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

var httpserver = new HttpServer();
var results = [];
var prefs = Cc["@mozilla.org/preferences-service;1"].getService(
  Ci.nsIPrefBranch
);

function createChannel() {
  var chan = NetUtil.newChannel({
    uri: "http://localhost:" + httpserver.identity.primaryPort + "/ok",
    loadUsingSystemPrincipal: true,
  });
  var httpChan = chan.QueryInterface(Ci.nsIHttpChannel);
  return httpChan;
}

function handler(metadata, response) {
  var body = "meh";
  response.setHeader("Content-Type", "text/plain", false);
  response.setStatusLine(metadata.httpVersion, 200, "OK");
  response.bodyOutputStream.write(body, body.length);
}

async function checkValueAndTrigger(request, data) {
  // give Firefox 150 ms to send notifications out
  do_timeout(150, doTest);
}

function doTest() {
  ok(results.length > 0);
  ok(results[0].host == "127.0.0.1");
  ok(results[0].rx > 0 || results[0].tx > 0);
  httpserver.stop(do_test_finished);
}

function run_test() {
  // setting up an observer
  let networkActivity = function(subject, topic, value) {
    subject.QueryInterface(Ci.nsIMutableArray);
    for (let data of subject.enumerate()) {
      results.push(data);
    }
  };

  Services.obs.addObserver(networkActivity, "network-activity");

  // why do I have to do this ??
  Services.obs.notifyObservers(null, "profile-initial-state");

  do_test_pending();
  httpserver.registerPathHandler("/ok", handler);
  httpserver.start(-1);
  var channel = createChannel();
  channel.asyncOpen(new ChannelListener(checkValueAndTrigger, null));
}