Bug 852957 - Add tests for channel decoding when the Content-Encoding header is present. r=paolo
authorRaymond Lee <raymond@raysquare.com>
Thu, 06 Jun 2013 20:33:24 +0800
changeset 145863 6fee06d61d20fbaa473ecf4218f29b0a861bfcde
parent 145862 d6db31e46b02c6a0edde1ddbbeafc770f358ffeb
child 145864 c9ae1381518aba16f28809f08e2c06e5ad70e439
push id2697
push userbbajaj@mozilla.com
push dateMon, 05 Aug 2013 18:49:53 +0000
treeherdermozilla-beta@dfec938c7b63 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspaolo
bugs852957
milestone24.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 852957 - Add tests for channel decoding when the Content-Encoding header is present. r=paolo
toolkit/components/jsdownloads/test/unit/head.js
toolkit/components/jsdownloads/test/unit/test_DownloadCore.js
--- a/toolkit/components/jsdownloads/test/unit/head.js
+++ b/toolkit/components/jsdownloads/test/unit/head.js
@@ -39,16 +39,20 @@ XPCOMUtils.defineLazyModuleGetter(this, 
                                   "resource://gre/modules/Task.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "OS",
                                   "resource://gre/modules/osfile.jsm");
 
 const ServerSocket = Components.Constructor(
                                 "@mozilla.org/network/server-socket;1",
                                 "nsIServerSocket",
                                 "init");
+const BinaryOutputStream = Components.Constructor(
+                                      "@mozilla.org/binaryoutputstream;1",
+                                      "nsIBinaryOutputStream",
+                                      "setOutputStream")
 
 const HTTP_SERVER_PORT = 4444;
 const HTTP_BASE = "http://localhost:" + HTTP_SERVER_PORT;
 
 const FAKE_SERVER_PORT = 4445;
 const FAKE_BASE = "http://localhost:" + FAKE_SERVER_PORT;
 
 const TEST_REFERRER_URI = NetUtil.newURI(HTTP_BASE + "/referrer.html");
@@ -59,18 +63,31 @@ const TEST_FAKE_SOURCE_URI = NetUtil.new
 const TEST_EMPTY_NOPROGRESS_PATH = "/empty-noprogress.txt";
 const TEST_EMPTY_NOPROGRESS_URI = NetUtil.newURI(HTTP_BASE +
                                                  TEST_EMPTY_NOPROGRESS_PATH);
 
 const TEST_INTERRUPTIBLE_PATH = "/interruptible.txt";
 const TEST_INTERRUPTIBLE_URI = NetUtil.newURI(HTTP_BASE +
                                               TEST_INTERRUPTIBLE_PATH);
 
+const TEST_INTERRUPTIBLE_GZIP_PATH = "/interruptible_gzip.txt";
+const TEST_INTERRUPTIBLE_GZIP_URI = NetUtil.newURI(HTTP_BASE +
+                                                   TEST_INTERRUPTIBLE_GZIP_PATH);
+
 const TEST_TARGET_FILE_NAME = "test-download.txt";
 const TEST_DATA_SHORT = "This test string is downloaded.";
+// Generate using gzipCompressString in TelemetryPing.js.
+const TEST_DATA_SHORT_GZIP_ENCODED_FIRST = [
+ 31,139,8,0,0,0,0,0,0,3,11,201,200,44,86,40,73,45,46,81,40,46,41,202,204
+];
+const TEST_DATA_SHORT_GZIP_ENCODED_SECOND = [
+  75,87,0,114,83,242,203,243,114,242,19,83,82,83,244,0,151,222,109,43,31,0,0,0
+];
+const TEST_DATA_SHORT_GZIP_ENCODED =
+  TEST_DATA_SHORT_GZIP_ENCODED_FIRST.concat(TEST_DATA_SHORT_GZIP_ENCODED_SECOND);
 
 /**
  * All the tests are implemented with add_task, this starts them automatically.
  */
 function run_test()
 {
   run_next_test();
 }
@@ -346,9 +363,25 @@ add_task(function test_common_initialize
     }, function secondPart(aRequest, aResponse) {
       aResponse.write(TEST_DATA_SHORT);
     });
 
   registerInterruptibleHandler(TEST_EMPTY_NOPROGRESS_PATH,
     function firstPart(aRequest, aResponse) {
       aResponse.setHeader("Content-Type", "text/plain", false);
     }, function secondPart(aRequest, aResponse) { });
+
+
+  registerInterruptibleHandler(TEST_INTERRUPTIBLE_GZIP_PATH,
+    function firstPart(aRequest, aResponse) {
+      aResponse.setHeader("Content-Type", "text/plain", false);
+      aResponse.setHeader("Content-Encoding", "gzip", false);
+      aResponse.setHeader("Content-Length", "" + TEST_DATA_SHORT_GZIP_ENCODED.length);
+
+      let bos =  new BinaryOutputStream(aResponse.bodyOutputStream);
+      bos.writeByteArray(TEST_DATA_SHORT_GZIP_ENCODED_FIRST,
+                         TEST_DATA_SHORT_GZIP_ENCODED_FIRST.length);
+    }, function secondPart(aRequest, aResponse) {
+      let bos =  new BinaryOutputStream(aResponse.bodyOutputStream);
+      bos.writeByteArray(TEST_DATA_SHORT_GZIP_ENCODED_SECOND,
+                         TEST_DATA_SHORT_GZIP_ENCODED_SECOND.length);
+    });
 });
--- a/toolkit/components/jsdownloads/test/unit/test_DownloadCore.js
+++ b/toolkit/components/jsdownloads/test/unit/test_DownloadCore.js
@@ -723,17 +723,16 @@ add_task(function test_download_error_re
   do_check_true(download.succeeded);
   do_check_false(download.canceled);
   do_check_true(download.error === null);
   do_check_eq(download.progress, 100);
 
   yield promiseVerifyContents(download.target.file, TEST_DATA_SHORT);
 });
 
-
 /**
  * Executes download in both public and private modes.
  */
 add_task(function test_download_public_and_private()
 {
   let source_path = "/test_download_public_and_private.txt";
   let source_uri = NetUtil.newURI(HTTP_BASE + source_path);
   let testCount = 0;
@@ -741,17 +740,16 @@ add_task(function test_download_public_a
   // Apply pref to allow all cookies.
   Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
 
   function cleanup() {
     Services.prefs.clearUserPref("network.cookie.cookieBehavior");
     Services.cookies.removeAll();
     gHttpServer.registerPathHandler(source_path, null);
   }
-
   do_register_cleanup(cleanup);
 
   gHttpServer.registerPathHandler(source_path, function (aRequest, aResponse) {
     aResponse.setHeader("Content-Type", "text/plain", false);
 
     if (testCount == 0) {
       // No cookies should exist for first public download.
       do_check_false(aRequest.hasHeader("Cookie"));
@@ -797,8 +795,81 @@ add_task(function test_download_cancel_i
 
   // Wait for a timeout.
   yield promiseTimeout(10);
 
   yield download.start();
   do_check_true(download.startTime.getTime() > startTime.getTime());
 });
 
+/**
+ * Executes download with content-encoding.
+ */
+add_task(function test_download_with_content_encoding()
+{
+  let source_path = "/test_download_with_content_encoding.txt";
+  let source_uri = NetUtil.newURI(HTTP_BASE + source_path);
+
+  function cleanup() {
+    gHttpServer.registerPathHandler(source_path, null);
+  }
+  do_register_cleanup(cleanup);
+
+  gHttpServer.registerPathHandler(source_path, function (aRequest, aResponse) {
+    aResponse.setHeader("Content-Type", "text/plain", false);
+    aResponse.setHeader("Content-Encoding", "gzip", false);
+    aResponse.setHeader("Content-Length",
+                        "" + TEST_DATA_SHORT_GZIP_ENCODED.length, false);
+
+    let bos =  new BinaryOutputStream(aResponse.bodyOutputStream);
+    bos.writeByteArray(TEST_DATA_SHORT_GZIP_ENCODED,
+                       TEST_DATA_SHORT_GZIP_ENCODED.length);
+  });
+
+  let download = yield Downloads.createDownload({
+    source: { uri: source_uri },
+    target: { file: getTempFile(TEST_TARGET_FILE_NAME) },
+    saver: { type: "copy" },
+  });
+  yield download.start();
+
+  do_check_eq(download.progress, 100);
+  do_check_eq(download.totalBytes, TEST_DATA_SHORT_GZIP_ENCODED.length);
+
+  // Ensure the content matches the decoded test data.
+  yield promiseVerifyContents(download.target.file, TEST_DATA_SHORT);
+});
+
+/**
+ * Cancels and restarts a download sequentially with content-encoding.
+ */
+add_task(function test_download_cancel_midway_restart_with_content_encoding()
+{
+  let download = yield promiseSimpleDownload(TEST_INTERRUPTIBLE_GZIP_URI);
+
+  // The first time, cancel the download midway.
+  let deferResponse = deferNextResponse();
+  try {
+    let deferCancel = Promise.defer();
+    download.onchange = function () {
+      if (!download.stopped && !download.canceled &&
+          download.currentBytes == TEST_DATA_SHORT_GZIP_ENCODED_FIRST.length) {
+        deferCancel.resolve(download.cancel());
+      }
+    };
+    download.start();
+    yield deferCancel.promise;
+  } finally {
+    deferResponse.resolve();
+  }
+
+  do_check_true(download.stopped);
+
+  // The second time, we'll provide the entire interruptible response.
+  download.onchange = null;
+  yield download.start()
+
+  do_check_eq(download.progress, 100);
+  do_check_eq(download.totalBytes, TEST_DATA_SHORT_GZIP_ENCODED.length);
+
+  yield promiseVerifyContents(download.target.file, TEST_DATA_SHORT);
+});
+