test_httpResponseTimeout.js (4865B)
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/. */ 6 7 "use strict"; 8 9 const { HttpServer } = ChromeUtils.importESModule( 10 "resource://testing-common/httpd.sys.mjs" 11 ); 12 13 var baseURL; 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"; 20 21 const prefService = Services.prefs; 22 23 var server = new HttpServer(); 24 25 function TimeoutListener(expectResponse) { 26 this.expectResponse = expectResponse; 27 } 28 29 TimeoutListener.prototype = { 30 onStartRequest() {}, 31 32 onDataAvailable() {}, 33 34 onStopRequest(request, status) { 35 if (this.expectResponse) { 36 Assert.equal(status, Cr.NS_OK); 37 } else { 38 Assert.equal(status, Cr.NS_ERROR_NET_TIMEOUT); 39 } 40 41 run_next_test(); 42 }, 43 }; 44 45 function serverStopListener() { 46 do_test_finished(); 47 } 48 49 function testTimeout(timeoutEnabled, expectResponse) { 50 // Set timeout pref. 51 if (timeoutEnabled) { 52 prefService.setIntPref(kResponseTimeoutPref, kResponseTimeout); 53 } else { 54 prefService.setIntPref(kResponseTimeoutPref, 0); 55 } 56 57 var chan = NetUtil.newChannel({ 58 uri: baseURL, 59 loadUsingSystemPrincipal: true, 60 }).QueryInterface(Ci.nsIHttpChannel); 61 var listener = new TimeoutListener(expectResponse); 62 chan.asyncOpen(listener); 63 } 64 65 function testTimeoutEnabled() { 66 // Set a timeout value; expect a timeout and no response. 67 testTimeout(true, false); 68 } 69 70 function testTimeoutDisabled() { 71 // Set a timeout value of 0; expect a response. 72 testTimeout(false, true); 73 } 74 75 function testTimeoutDisabledByShortLivedKeepalives() { 76 // Enable TCP Keepalives for short lived HTTP connections. 77 prefService.setBoolPref(kShortLivedKeepalivePref, true); 78 prefService.setBoolPref(kLongLivedKeepalivePref, false); 79 80 // Try to set a timeout value, but expect a response without timeout. 81 testTimeout(true, true); 82 } 83 84 function testTimeoutDisabledByLongLivedKeepalives() { 85 // Enable TCP Keepalives for long lived HTTP connections. 86 prefService.setBoolPref(kShortLivedKeepalivePref, false); 87 prefService.setBoolPref(kLongLivedKeepalivePref, true); 88 89 // Try to set a timeout value, but expect a response without timeout. 90 testTimeout(true, true); 91 } 92 93 function testTimeoutDisabledByBothKeepalives() { 94 // Enable TCP Keepalives for short and long lived HTTP connections. 95 prefService.setBoolPref(kShortLivedKeepalivePref, true); 96 prefService.setBoolPref(kLongLivedKeepalivePref, true); 97 98 // Try to set a timeout value, but expect a response without timeout. 99 testTimeout(true, true); 100 } 101 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); 109 }); 110 } 111 if (prefService.getBoolPref(kLongLivedKeepalivePref)) { 112 prefService.setBoolPref(kLongLivedKeepalivePref, false); 113 registerCleanupFunction(function () { 114 prefService.setBoolPref(kLongLivedKeepalivePref, true); 115 }); 116 } 117 118 var tests = [ 119 // Enable with a timeout value >0; 120 testTimeoutEnabled, 121 // Disable with a timeout value of 0; 122 testTimeoutDisabled, 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, 129 ]; 130 131 for (var i = 0; i < tests.length; i++) { 132 add_test(tests[i]); 133 } 134 } 135 136 function setup_http_server() { 137 // Start server; will be stopped at test cleanup time. 138 server.start(-1); 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(); 144 145 do_timeout((kResponseTimeout + 1) * 1000 /* ms */, function () { 146 response.setStatusLine(metadata.httpVersion, 200, "OK"); 147 response.write("Hello world"); 148 response.finish(); 149 }); 150 }); 151 registerCleanupFunction(function () { 152 server.stop(serverStopListener); 153 }); 154 } 155 156 function run_test() { 157 setup_http_server(); 158 159 setup_tests(); 160 161 run_next_test(); 162 }