Bug 852957 - Add tests for channel decoding when the Content-Encoding header is present. r=paolo

This commit is contained in:
Raymond Lee 2013-06-06 20:33:24 +08:00
parent 6915f629eb
commit 37db599161
2 changed files with 106 additions and 2 deletions

View File

@ -44,6 +44,10 @@ 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;
@ -64,8 +68,21 @@ 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.
@ -351,4 +368,20 @@ add_task(function test_common_initialize()
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);
});
});

View File

@ -728,7 +728,6 @@ add_task(function test_download_error_restart()
yield promiseVerifyContents(download.target.file, TEST_DATA_SHORT);
});
/**
* Executes download in both public and private modes.
*/
@ -746,7 +745,6 @@ add_task(function test_download_public_and_private()
Services.cookies.removeAll();
gHttpServer.registerPathHandler(source_path, null);
}
do_register_cleanup(cleanup);
gHttpServer.registerPathHandler(source_path, function (aRequest, aResponse) {
@ -802,3 +800,76 @@ add_task(function test_download_cancel_immediately_restart_and_check_startTime()
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);
});