toolkit/components/downloads/test/unit/test_private_resume.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/downloads/test/unit/test_private_resume.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// Public request gets times=0 cookie, completes
     1.9 +// Private request gets times=1 cookie, canceled
    1.10 +// Private resumed request sends times=1 cookie, completes
    1.11 +
    1.12 +function run_test() {
    1.13 +  if (oldDownloadManagerDisabled()) {
    1.14 +    return;
    1.15 +  }
    1.16 +
    1.17 +  // Allow all cookies.
    1.18 +  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
    1.19 +
    1.20 +  do_test_pending();
    1.21 +  let httpserv = new HttpServer();
    1.22 +
    1.23 +  let times = 0;
    1.24 +  httpserv.registerPathHandler("/head_download_manager.js", function (meta, response) {
    1.25 +    response.setHeader("Content-Type", "text/plain", false);
    1.26 +    response.setStatusLine("1.1", !meta.hasHeader('range') ? 200 : 206);
    1.27 +
    1.28 +    // Set a cookie if none is sent with the request. Public and private requests
    1.29 +    // should therefore receive different cookies, so we can tell if the resumed
    1.30 +    // request is actually treated as private or not.
    1.31 +    if (!meta.hasHeader('Cookie')) {
    1.32 +      do_check_true(times == 0 || times == 1);
    1.33 +      response.setHeader('Set-Cookie', 'times=' + times++);
    1.34 +    } else {
    1.35 +      do_check_eq(times, 2);
    1.36 +      do_check_eq(meta.getHeader('Cookie'), 'times=1');
    1.37 +    }
    1.38 +    let full = "";
    1.39 +    let body = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; //60
    1.40 +    for (var i = 0; i < 1000; i++) {
    1.41 +      full += body;
    1.42 +    }
    1.43 +    response.write(full);
    1.44 +  });
    1.45 +  httpserv.start(-1);
    1.46 +
    1.47 +  let state = 0;
    1.48 +
    1.49 +  let listener = {
    1.50 +    onDownloadStateChange: function(aState, aDownload) {
    1.51 +      switch (aDownload.state) {
    1.52 +        case downloadUtils.downloadManager.DOWNLOAD_DOWNLOADING:
    1.53 +          // We only care about the private download
    1.54 +          if (state != 1)
    1.55 +            break;
    1.56 +
    1.57 +          state++;
    1.58 +          do_check_true(aDownload.resumable);
    1.59 +
    1.60 +          aDownload.pause();
    1.61 +          do_check_eq(aDownload.state, downloadUtils.downloadManager.DOWNLOAD_PAUSED);
    1.62 +
    1.63 +          do_execute_soon(function() {
    1.64 +            aDownload.resume();
    1.65 +          });
    1.66 +          break;
    1.67 +
    1.68 +        case downloadUtils.downloadManager.DOWNLOAD_FINISHED:
    1.69 +          if (state == 0) {
    1.70 +            do_execute_soon(function() {
    1.71 +              // Perform an identical request but in private mode.
    1.72 +              // It should receive a different cookie than the
    1.73 +              // public request.
    1.74 +
    1.75 +              state++;
    1.76 +
    1.77 +              addDownload(httpserv, {
    1.78 +                isPrivate: true,
    1.79 +                sourceURI: downloadCSource,
    1.80 +                downloadName: downloadCName + "!!!",
    1.81 +                runBeforeStart: function (aDownload) {
    1.82 +                  // Check that dl is retrievable
    1.83 +                  do_check_eq(downloadUtils.downloadManager.activePrivateDownloadCount, 1);
    1.84 +                }
    1.85 +              });
    1.86 +            });
    1.87 +          } else if (state == 2) {
    1.88 +            // We're done here.
    1.89 +            do_execute_soon(function() {
    1.90 +              httpserv.stop(do_test_finished);
    1.91 +            });
    1.92 +          }
    1.93 +          break;
    1.94 +
    1.95 +        default:
    1.96 +          break;
    1.97 +      }
    1.98 +    },
    1.99 +    onStateChange: function(a, b, c, d, e) { },
   1.100 +    onProgressChange: function(a, b, c, d, e, f, g) { },
   1.101 +    onSecurityChange: function(a, b, c, d) { }
   1.102 +  };
   1.103 +
   1.104 +  downloadUtils.downloadManager.addPrivacyAwareListener(listener);
   1.105 +
   1.106 +  const downloadCSource = "http://localhost:" +
   1.107 +                          httpserv.identity.primaryPort +
   1.108 +                          "/head_download_manager.js";
   1.109 +  const downloadCName = "download-C";
   1.110 +
   1.111 +  // First a public download that completes without interruption.
   1.112 +  let dl = addDownload(httpserv, {
   1.113 +    isPrivate: false,
   1.114 +    sourceURI: downloadCSource,
   1.115 +    downloadName: downloadCName,
   1.116 +    runBeforeStart: function (aDownload) {
   1.117 +      // Check that dl is retrievable
   1.118 +      do_check_eq(downloadUtils.downloadManager.activeDownloadCount, 1);
   1.119 +    }
   1.120 +  });
   1.121 +}

mercurial