4 // The purpose of this test is to test whether the HttpConnectionMgr really
5 // cancel and close all connecitons when get "net:cancel-all-connections".
8 // 1. Create 6 http requests. Server would not process responses and just put
9 // all requests in its queue.
10 // 2. Once server receive all 6 requests, call notifyObservers with the
11 // topic "net:cancel-all-connections".
12 // 3. We expect that all 6 active connections should be closed with the status
17 const { HttpServer } = ChromeUtils.importESModule(
18 "resource://testing-common/httpd.sys.mjs"
21 var server = new HttpServer();
23 var baseURL = "http://localhost:" + server.identity.primaryPort + "/";
24 var maxConnections = 0;
34 dump("TEST INFO | " + msg + "\n");
38 function make_channel(url) {
39 var request = NetUtil.newChannel({
41 loadUsingSystemPrincipal: true,
43 request.QueryInterface(Ci.nsIHttpChannel);
47 function serverStopListener() {
51 function createHttpRequest(status) {
53 var chan = make_channel(uri);
54 var listner = new HttpResponseListener(++requestId, status);
55 chan.setRequestHeader("X-ID", requestId, false);
56 chan.setRequestHeader("Cache-control", "no-store", false);
57 chan.asyncOpen(listner);
58 log("Create http request id=" + requestId);
61 function setupHttpRequests(status) {
62 log("setupHttpRequests");
63 for (var i = 0; i < maxConnections; i++) {
64 createHttpRequest(status);
69 function HttpResponseListener(id, onStopRequestStatus) {
71 this.onStopRequestStatus = onStopRequestStatus;
74 HttpResponseListener.prototype = {
75 onStartRequest(request) {},
77 onDataAvailable(request, stream, off, cnt) {},
79 onStopRequest(request, status) {
80 log("STOP id=" + this.id + " status=" + status);
81 Assert.ok(this.onStopRequestStatus == status);
86 var responseQueue = [];
87 function setup_http_server() {
88 log("setup_http_server");
89 maxConnections = Services.prefs.getIntPref(
90 "network.http.max-persistent-connections-per-server"
93 // Start server; will be stopped at test cleanup time.
94 server.registerPathHandler("/", function (metadata, response) {
95 var id = metadata.getHeader("X-ID");
96 log("Server recived the response id=" + id);
98 response.processAsync();
99 response.setHeader("X-ID", id);
100 responseQueue.push(response);
102 if (responseQueue.length == maxConnections) {
103 log("received all http requets");
104 Services.obs.notifyObservers(null, "net:cancel-all-connections");
108 registerCleanupFunction(function () {
109 server.stop(serverStopListener);
113 function run_test() {
115 setupHttpRequests(Cr.NS_ERROR_ABORT);