Bug 959340 - Add updated serve_file.sjs to support testing resuming downloads and update pause_resume test to use it. r=gwagner

This commit is contained in:
Ghislain Lacroix 2014-01-13 14:14:34 -08:00
parent 43e9e7bade
commit bf522028ca
2 changed files with 48 additions and 6 deletions

View File

@ -53,8 +53,14 @@ function handleResponse() {
function handleRequest(request, response) {
var query = getQuery(request);
// Default status when responding.
var version = "1.1";
var statusCode = 200;
var description = "OK";
// Default values for content type, size and rate.
var contentType = "text/plain";
var contentRange = null;
var size = 1024;
var rate = 0;
@ -68,6 +74,39 @@ function handleRequest(request, response) {
size = parseInt(query["size"]);
}
// optional range request check.
if (request.hasHeader("range")) {
version = "1.1";
statusCode = 206;
description = "Partial Content";
// We'll only support simple range byte style requests.
var [offset, total] = request.getHeader("range").slice("bytes=".length).split("-");
// Enforce valid Number values.
offset = parseInt(offset);
offset = isNaN(offset) ? 0 : offset;
// Same.
total = parseInt(total);
total = isNaN(total) ? 0 : total;
// We'll need to original total size as part of the Content-Range header
// value in our response.
var originalSize = size;
// If we have a total size requested, we must make sure to send that number
// of bytes only (minus the start offset).
if (total && total < size) {
size = total - offset;
} else if (offset) {
// Looks like we just have a byte offset to deal with.
size = size - offset;
}
// We specifically need to add a Content-Range header to all responses for
// requests that include a range request header.
contentRange = "bytes " + offset + "-" + (size - 1) + "/" + originalSize;
}
// optional rate (in bytes/s) at which to send the file.
if ("rate" in query) {
rate = parseInt(query["rate"]);
@ -96,7 +135,11 @@ function handleRequest(request, response) {
response.processAsync();
// generate the content.
response.setStatusLine(version, statusCode, description);
response.setHeader("Content-Type", contentType, false);
if (contentRange) {
response.setHeader("Content-Range", contentRange, false);
}
response.setHeader("Content-Length", size.toString(), false);
// initialize the timer and start writing out the response.

View File

@ -15,7 +15,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=938023
<p id="display"></p>
<div id="content" style="display: none">
</div>
<a href="serve_file.sjs?contentType=application/octet-stream&size=102400&rate=1024" download="test.bin" id="download1">Large Download</a>
<a href="serve_file.sjs?contentType=application/octet-stream&size=102400&rate=10240" download="test.bin" id="download1">Large Download</a>
<pre id="test">
<script class="testbody" type="text/javascript;version=1.7">
@ -52,10 +52,10 @@ function checkDownloadList(downloads) {
SimpleTest.finish();
}
function checkResumedFailed(download) {
ok(download.state == "stopped", "Download fails to resume.");
function checkResumeSucceeded(download) {
ok(download.state == "succeeded", "Download resumed successfully.");
navigator.mozDownloadManager.clearAllDone()
.then(checkDownloadList, error);
.then(checkDownloadList, error);
}
function downloadChange(evt) {
@ -67,9 +67,8 @@ function downloadChange(evt) {
} else if (download.state == "stopped" && !resuming) {
resuming = true;
ok(pausing, "Download stopped by pause()");
// serve_file.sjs does not support resuming, so that should fail.
download.resume()
.then(error, function() { checkResumedFailed(download); });
.then(function() { checkResumeSucceeded(download); }, error);
}
}