diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
commit | 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch) | |
tree | a31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /netwerk/test/unit/test_bug1411316_http1.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip |
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'netwerk/test/unit/test_bug1411316_http1.js')
-rw-r--r-- | netwerk/test/unit/test_bug1411316_http1.js | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/netwerk/test/unit/test_bug1411316_http1.js b/netwerk/test/unit/test_bug1411316_http1.js new file mode 100644 index 0000000000..ef163024d6 --- /dev/null +++ b/netwerk/test/unit/test_bug1411316_http1.js @@ -0,0 +1,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); +} |