1 /* -*- Mode: javascript; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 const { HttpServer
} = ChromeUtils
.importESModule(
10 "resource://testing-common/httpd.sys.mjs"
14 const kResponseTimeoutPref
= "network.http.response.timeout";
15 const kResponseTimeout
= 1;
16 const kShortLivedKeepalivePref
=
17 "network.http.tcp_keepalive.short_lived_connections";
18 const kLongLivedKeepalivePref
=
19 "network.http.tcp_keepalive.long_lived_connections";
21 const prefService
= Services
.prefs
;
23 var server
= new HttpServer();
25 function TimeoutListener(expectResponse
) {
26 this.expectResponse
= expectResponse
;
29 TimeoutListener
.prototype = {
34 onStopRequest(request
, status
) {
35 if (this.expectResponse
) {
36 Assert
.equal(status
, Cr
.NS_OK
);
38 Assert
.equal(status
, Cr
.NS_ERROR_NET_TIMEOUT
);
45 function serverStopListener() {
49 function testTimeout(timeoutEnabled
, expectResponse
) {
52 prefService
.setIntPref(kResponseTimeoutPref
, kResponseTimeout
);
54 prefService
.setIntPref(kResponseTimeoutPref
, 0);
57 var chan
= NetUtil
.newChannel({
59 loadUsingSystemPrincipal
: true,
60 }).QueryInterface(Ci
.nsIHttpChannel
);
61 var listener
= new TimeoutListener(expectResponse
);
62 chan
.asyncOpen(listener
);
65 function testTimeoutEnabled() {
66 // Set a timeout value; expect a timeout and no response.
67 testTimeout(true, false);
70 function testTimeoutDisabled() {
71 // Set a timeout value of 0; expect a response.
72 testTimeout(false, true);
75 function testTimeoutDisabledByShortLivedKeepalives() {
76 // Enable TCP Keepalives for short lived HTTP connections.
77 prefService
.setBoolPref(kShortLivedKeepalivePref
, true);
78 prefService
.setBoolPref(kLongLivedKeepalivePref
, false);
80 // Try to set a timeout value, but expect a response without timeout.
81 testTimeout(true, true);
84 function testTimeoutDisabledByLongLivedKeepalives() {
85 // Enable TCP Keepalives for long lived HTTP connections.
86 prefService
.setBoolPref(kShortLivedKeepalivePref
, false);
87 prefService
.setBoolPref(kLongLivedKeepalivePref
, true);
89 // Try to set a timeout value, but expect a response without timeout.
90 testTimeout(true, true);
93 function testTimeoutDisabledByBothKeepalives() {
94 // Enable TCP Keepalives for short and long lived HTTP connections.
95 prefService
.setBoolPref(kShortLivedKeepalivePref
, true);
96 prefService
.setBoolPref(kLongLivedKeepalivePref
, true);
98 // Try to set a timeout value, but expect a response without timeout.
99 testTimeout(true, true);
102 function setup_tests() {
103 // Start tests with timeout enabled, i.e. disable TCP keepalives for HTTP.
104 // Reset pref in cleanup.
105 if (prefService
.getBoolPref(kShortLivedKeepalivePref
)) {
106 prefService
.setBoolPref(kShortLivedKeepalivePref
, false);
107 registerCleanupFunction(function () {
108 prefService
.setBoolPref(kShortLivedKeepalivePref
, true);
111 if (prefService
.getBoolPref(kLongLivedKeepalivePref
)) {
112 prefService
.setBoolPref(kLongLivedKeepalivePref
, false);
113 registerCleanupFunction(function () {
114 prefService
.setBoolPref(kLongLivedKeepalivePref
, true);
119 // Enable with a timeout value >0;
121 // Disable with a timeout value of 0;
123 // Disable by enabling TCP keepalive for short-lived HTTP connections.
124 testTimeoutDisabledByShortLivedKeepalives
,
125 // Disable by enabling TCP keepalive for long-lived HTTP connections.
126 testTimeoutDisabledByLongLivedKeepalives
,
127 // Disable by enabling TCP keepalive for both HTTP connection types.
128 testTimeoutDisabledByBothKeepalives
,
131 for (var i
= 0; i
< tests
.length
; i
++) {
136 function setup_http_server() {
137 // Start server; will be stopped at test cleanup time.
139 baseURL
= "http://localhost:" + server
.identity
.primaryPort
+ "/";
140 info("Using baseURL: " + baseURL
);
141 server
.registerPathHandler("/", function (metadata
, response
) {
142 // Wait until the timeout should have passed, then respond.
143 response
.processAsync();
145 do_timeout((kResponseTimeout
+ 1) * 1000 /* ms */, function () {
146 response
.setStatusLine(metadata
.httpVersion
, 200, "OK");
147 response
.write("Hello world");
151 registerCleanupFunction(function () {
152 server
.stop(serverStopListener
);
156 function run_test() {