netwerk/test/unit/test_httpsuspend.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_httpsuspend.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,80 @@
     1.4 +// This file ensures that suspending a channel directly after opening it
     1.5 +// suspends future notifications correctly.
     1.6 +
     1.7 +Cu.import("resource://testing-common/httpd.js");
     1.8 +
     1.9 +XPCOMUtils.defineLazyGetter(this, "URL", function() {
    1.10 +  return "http://localhost:" + httpserv.identity.primaryPort;
    1.11 +});
    1.12 +
    1.13 +const MIN_TIME_DIFFERENCE = 3000;
    1.14 +const RESUME_DELAY = 5000;
    1.15 +
    1.16 +var listener = {
    1.17 +  _lastEvent: 0,
    1.18 +  _gotData: false,
    1.19 +
    1.20 +  QueryInterface: function(iid) {
    1.21 +    if (iid.equals(Components.interfaces.nsIStreamListener) ||
    1.22 +        iid.equals(Components.interfaces.nsIRequestObserver) ||
    1.23 +        iid.equals(Components.interfaces.nsISupports))
    1.24 +      return this;
    1.25 +    throw Components.results.NS_ERROR_NO_INTERFACE;
    1.26 +  },
    1.27 +
    1.28 +  onStartRequest: function(request, ctx) {
    1.29 +    this._lastEvent = Date.now();
    1.30 +    request.QueryInterface(Ci.nsIRequest);
    1.31 +
    1.32 +    // Insert a delay between this and the next callback to ensure message buffering
    1.33 +    // works correctly
    1.34 +    request.suspend();
    1.35 +    request.suspend();
    1.36 +    do_timeout(RESUME_DELAY, function() request.resume());
    1.37 +    do_timeout(RESUME_DELAY + 1000, function() request.resume());
    1.38 +  },
    1.39 +
    1.40 +  onDataAvailable: function(request, context, stream, offset, count) {
    1.41 +    do_check_true(Date.now() - this._lastEvent >= MIN_TIME_DIFFERENCE);
    1.42 +    read_stream(stream, count);
    1.43 +
    1.44 +    // Ensure that suspending and resuming inside a callback works correctly
    1.45 +    request.suspend();
    1.46 +    request.suspend();
    1.47 +    request.resume();
    1.48 +    request.resume();
    1.49 +
    1.50 +    this._gotData = true;
    1.51 +  },
    1.52 +
    1.53 +  onStopRequest: function(request, ctx, status) {
    1.54 +    do_check_true(this._gotData);
    1.55 +    httpserv.stop(do_test_finished);
    1.56 +  }
    1.57 +};
    1.58 +
    1.59 +function makeChan(url) {
    1.60 +  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    1.61 +  var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
    1.62 +  return chan;
    1.63 +}
    1.64 +
    1.65 +var httpserv = null;
    1.66 +
    1.67 +function run_test() {
    1.68 +  httpserv = new HttpServer();
    1.69 +  httpserv.registerPathHandler("/woo", data);
    1.70 +  httpserv.start(-1);
    1.71 +
    1.72 +  var chan = makeChan(URL + "/woo");
    1.73 +  chan.QueryInterface(Ci.nsIRequest);
    1.74 +  chan.asyncOpen(listener, null);
    1.75 +
    1.76 +  do_test_pending();
    1.77 +}
    1.78 +
    1.79 +function data(metadata, response) {
    1.80 +  let httpbody = "0123456789";
    1.81 +  response.setHeader("Content-Type", "text/plain", false);
    1.82 +  response.bodyOutputStream.write(httpbody, httpbody.length);
    1.83 +}

mercurial