netwerk/test/unit/test_httpResponseTimeout.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     7 "use strict";
     9 Cu.import("resource://testing-common/httpd.js");
    11 var baseURL;
    12 const kResponseTimeoutPref = "network.http.response.timeout";
    13 const kResponseTimeout = 1;
    14 const kShortLivedKeepalivePref =
    15   "network.http.tcp_keepalive.short_lived_connections";
    16 const kLongLivedKeepalivePref =
    17   "network.http.tcp_keepalive.long_lived_connections";
    19 const prefService = Cc["@mozilla.org/preferences-service;1"]
    20                        .getService(Ci.nsIPrefBranch);
    22 var server = new HttpServer();
    24 function TimeoutListener(expectResponse) {
    25   this.expectResponse = expectResponse;
    26 }
    28 TimeoutListener.prototype = {
    29   onStartRequest: function (request, ctx) {
    30   },
    32   onDataAvailable: function (request, ctx, stream) {
    33   },
    35   onStopRequest: function (request, ctx, status) {
    36     if (this.expectResponse) {
    37       do_check_eq(status, Cr.NS_OK);
    38     } else {
    39       do_check_eq(status, Cr.NS_ERROR_NET_TIMEOUT);
    40     }
    42     run_next_test();
    43   },
    44 };
    46 function serverStopListener() {
    47   do_test_finished();
    48 }
    50 function testTimeout(timeoutEnabled, expectResponse) {
    51   // Set timeout pref.
    52   if (timeoutEnabled) {
    53     prefService.setIntPref(kResponseTimeoutPref, kResponseTimeout);
    54   } else {
    55     prefService.setIntPref(kResponseTimeoutPref, 0);
    56   }
    58   var ios = Cc["@mozilla.org/network/io-service;1"]
    59   .getService(Ci.nsIIOService);
    60   var chan = ios.newChannel(baseURL, null, null)
    61   .QueryInterface(Ci.nsIHttpChannel);
    62   var listener = new TimeoutListener(expectResponse);
    63   chan.asyncOpen(listener, null);
    64 }
    66 function testTimeoutEnabled() {
    67   // Set a timeout value; expect a timeout and no response.
    68   testTimeout(true, false);
    69 }
    71 function testTimeoutDisabled() {
    72   // Set a timeout value of 0; expect a response.
    73   testTimeout(false, true);
    74 }
    76 function testTimeoutDisabledByShortLivedKeepalives() {
    77   // Enable TCP Keepalives for short lived HTTP connections.
    78   prefService.setBoolPref(kShortLivedKeepalivePref, true);
    79   prefService.setBoolPref(kLongLivedKeepalivePref, false);
    81   // Try to set a timeout value, but expect a response without timeout.
    82   testTimeout(true, true);
    83 }
    85 function testTimeoutDisabledByLongLivedKeepalives() {
    86   // Enable TCP Keepalives for long lived HTTP connections.
    87   prefService.setBoolPref(kShortLivedKeepalivePref, false);
    88   prefService.setBoolPref(kLongLivedKeepalivePref, true);
    90   // Try to set a timeout value, but expect a response without timeout.
    91   testTimeout(true, true);
    92 }
    94 function testTimeoutDisabledByBothKeepalives() {
    95   // Enable TCP Keepalives for short and long lived HTTP connections.
    96   prefService.setBoolPref(kShortLivedKeepalivePref, true);
    97   prefService.setBoolPref(kLongLivedKeepalivePref, true);
    99   // Try to set a timeout value, but expect a response without timeout.
   100   testTimeout(true, true);
   101 }
   103 function setup_tests() {
   104   // Start tests with timeout enabled, i.e. disable TCP keepalives for HTTP.
   105   // Reset pref in cleanup.
   106   if (prefService.getBoolPref(kShortLivedKeepalivePref)) {
   107     prefService.setBoolPref(kShortLivedKeepalivePref, false);
   108     do_register_cleanup(function() {
   109       prefService.setBoolPref(kShortLivedKeepalivePref, true);
   110     });
   111   }
   112   if (prefService.getBoolPref(kLongLivedKeepalivePref)) {
   113     prefService.setBoolPref(kLongLivedKeepalivePref, false);
   114     do_register_cleanup(function() {
   115       prefService.setBoolPref(kLongLivedKeepalivePref, true);
   116     });
   117   }
   119   var tests = [
   120     // Enable with a timeout value >0;
   121     testTimeoutEnabled,
   122     // Disable with a timeout value of 0;
   123     testTimeoutDisabled,
   124     // Disable by enabling TCP keepalive for short-lived HTTP connections.
   125     testTimeoutDisabledByShortLivedKeepalives,
   126     // Disable by enabling TCP keepalive for long-lived HTTP connections.
   127     testTimeoutDisabledByLongLivedKeepalives,
   128     // Disable by enabling TCP keepalive for both HTTP connection types.
   129     testTimeoutDisabledByBothKeepalives
   130   ];
   132   for (var i=0; i < tests.length; i++) {
   133     add_test(tests[i]);
   134   }
   135 }
   137 function setup_http_server() {
   138   // Start server; will be stopped at test cleanup time.
   139   server.start(-1);
   140   baseURL = "http://localhost:" + server.identity.primaryPort + "/";
   141   do_print("Using baseURL: " + baseURL);
   142   server.registerPathHandler('/', function(metadata, response) {
   143     // Wait until the timeout should have passed, then respond.
   144     response.processAsync();
   146     do_timeout((kResponseTimeout+1)*1000 /* ms */, function() {
   147       response.setStatusLine(metadata.httpVersion, 200, "OK");
   148       response.write("Hello world");
   149       response.finish();
   150     });
   151   });
   152   do_register_cleanup(function() {
   153     server.stop(serverStopListener);
   154   });
   155 }
   157 function run_test() {
   158   setup_http_server();
   160   setup_tests();
   162   run_next_test();
   163 }

mercurial