summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_bug1411316_http1.js
blob: ef163024d691bf5615c817dbc6f6fa82975c5018 (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
112
113
114
// Test bug 1411316.
//
// Summary:
// The purpose of this test is to test whether the HttpConnectionMgr really
// cancel and close all connecitons when get "net:cancel-all-connections".
//
// Test step:
// 1. Create 6 http requests. Server would not process responses and just put
//    all requests in its queue.
// 2. Once server receive all 6 requests, call notifyObservers with the
//    topic "net:cancel-all-connections".
// 3. We expect that all 6 active connections should be closed with the status
//    NS_ERROR_ABORT.

"use strict";

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

var server = new HttpServer();
server.start(-1);
var baseURL = "http://localhost:" + server.identity.primaryPort + "/";
var maxConnections = 0;
var debug = false;
var requestId = 0;

function log(msg) {
  if (!debug) {
    return;
  }

  if (msg) {
    dump("TEST INFO | " + msg + "\n");
  }
}

function make_channel(url) {
  var request = NetUtil.newChannel({
    uri: url,
    loadUsingSystemPrincipal: true,
  });
  request.QueryInterface(Ci.nsIHttpChannel);
  return request;
}

function serverStopListener() {
  server.stop();
}

function createHttpRequest(status) {
  let uri = baseURL;
  var chan = make_channel(uri);
  var listner = new HttpResponseListener(++requestId, status);
  chan.setRequestHeader("X-ID", requestId, false);
  chan.setRequestHeader("Cache-control", "no-store", false);
  chan.asyncOpen(listner);
  log("Create http request id=" + requestId);
}

function setupHttpRequests(status) {
  log("setupHttpRequests");
  for (var i = 0; i < maxConnections; i++) {
    createHttpRequest(status);
    do_test_pending();
  }
}

function HttpResponseListener(id, onStopRequestStatus) {
  this.id = id;
  this.onStopRequestStatus = onStopRequestStatus;
}

HttpResponseListener.prototype = {
  onStartRequest(request) {},

  onDataAvailable(request, stream, off, cnt) {},

  onStopRequest(request, status) {
    log("STOP id=" + this.id + " status=" + status);
    Assert.ok(this.onStopRequestStatus == status);
    do_test_finished();
  },
};

var responseQueue = [];
function setup_http_server() {
  log("setup_http_server");
  maxConnections = Services.prefs.getIntPref(
    "network.http.max-persistent-connections-per-server"
  );

  // Start server; will be stopped at test cleanup time.
  server.registerPathHandler("/", function (metadata, response) {
    var id = metadata.getHeader("X-ID");
    log("Server recived the response id=" + id);

    response.processAsync();
    response.setHeader("X-ID", id);
    responseQueue.push(response);

    if (responseQueue.length == maxConnections) {
      log("received all http requets");
      Services.obs.notifyObservers(null, "net:cancel-all-connections");
    }
  });

  registerCleanupFunction(function () {
    server.stop(serverStopListener);
  });
}

function run_test() {
  setup_http_server();
  setupHttpRequests(Cr.NS_ERROR_ABORT);
}