diff --git a/package.json b/package.json
index 36421bd..c44066b 100644
--- a/package.json
+++ b/package.json
@@ -39,6 +39,9 @@
"cordova-firefoxos",
"cordova-browser"
],
+ "peerDependencies" {
+ "cordova-plugin-file": ">=1.0.1"
+ },
"author": "Apache Software Foundation",
"license": "Apache 2.0"
}
diff --git a/plugin.xml b/plugin.xml
index 46868cf..c0ebfe9 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -30,7 +30,7 @@
https://issues.apache.org/jira/browse/CB/component/12320650
-
+
diff --git a/src/windows/FileTransferProxy.js b/src/windows/FileTransferProxy.js
index 659e5c9..72612fc 100644
--- a/src/windows/FileTransferProxy.js
+++ b/src/windows/FileTransferProxy.js
@@ -161,11 +161,18 @@ exec(win, fail, 'FileTransfer', 'upload',
}
var response = result.getResponseInformation();
- // creating a data reader, attached to response stream to get server's response
+ var ftResult = new FileUploadResult(result.progress.bytesSent, response.statusCode, '');
+
+ // if server's response doesn't contain any data, then resolve operation now
+ if (result.progress.bytesReceived === 0) {
+ successCallback(ftResult);
+ return;
+ }
+
+ // otherwise create a data reader, attached to response stream to get server's response
var reader = new Windows.Storage.Streams.DataReader(result.getResultStreamAt(0));
- reader.loadAsync(result.progress.bytesReceived).then(function(size) {
- var responseText = reader.readString(size);
- var ftResult = new FileUploadResult(size, response.statusCode, responseText);
+ reader.loadAsync(result.progress.bytesReceived).then(function (size) {
+ ftResult.response = reader.readString(size);
successCallback(ftResult);
reader.close();
});
@@ -268,7 +275,7 @@ exec(win, fail, 'FileTransfer', 'upload',
fileTransferOps[downloadId] = new FileTransferOperation(FileTransferOperation.PENDING, null);
var downloadCallback = function(storageFolder) {
- storageFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.generateUniqueName).then(function(storageFile) {
+ storageFolder.createFileAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(function(storageFile) {
// check if download isn't already cancelled
var downloadOp = fileTransferOps[downloadId];
diff --git a/src/wp/FileTransfer.cs b/src/wp/FileTransfer.cs
index 11db284..1ddfc28 100644
--- a/src/wp/FileTransfer.cs
+++ b/src/wp/FileTransfer.cs
@@ -513,7 +513,7 @@ namespace WPCordovaClassLib.Cordova.Commands
string id = optionStrings[0];
string callbackId = optionStrings[1];
- if (InProcDownloads.ContainsKey(id))
+ if (id != null && InProcDownloads.ContainsKey(id))
{
DownloadRequestState state = InProcDownloads[id];
if (!state.isCancelled)
diff --git a/tests/tests.js b/tests/tests.js
index 682ccc9..c13a88f 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -28,10 +28,12 @@
exports.defineAutoTests = function () {
// constants
- var GRACE_TIME_DELTA = 300; // in milliseconds
+ var GRACE_TIME_DELTA = 600; // in milliseconds
var DEFAULT_FILESYSTEM_SIZE = 1024*50; //filesystem size in bytes
var UNKNOWN_HOST = "http://foobar.apache.org";
var HEADERS_ECHO = "http://whatheaders.com"; // NOTE: this site is very useful!
+ var DOWNLOAD_TIMEOUT = 10000; // download tests sometimes need a higher timeout to complete successfully
+ var ABORT_DELAY = 100; // for abort() tests
// config for upload test server
// NOTE:
@@ -305,7 +307,7 @@ exports.defineAutoTests = function () {
// - 'httpssss://example.com'
// - 'apache.org', with subdomains="true"
// - 'cordova-filetransfer.jitsu.com'
- describe('download', function() {
+ describe('download', function () {
// helpers
var verifyDownload = function (fileEntry) {
@@ -317,6 +319,10 @@ exports.defineAutoTests = function () {
deleteFile(root, fileName, done);
});
+ it('ensures that test file does not exist', function (done) {
+ deleteFile(root, fileName, done);
+ });
+
it('filetransfer.spec.4 should download a file', function (done) {
var fileURL = SERVER + '/robots.txt';
@@ -342,7 +348,7 @@ exports.defineAutoTests = function () {
};
transfer.download(fileURL, localFilePath, downloadWin, unexpectedCallbacks.httpFail);
- });
+ }, DOWNLOAD_TIMEOUT);
it('filetransfer.spec.5 should download a file using http basic auth', function (done) {
@@ -354,7 +360,7 @@ exports.defineAutoTests = function () {
};
transfer.download(fileURL, localFilePath, downloadWin, unexpectedCallbacks.httpFail);
- });
+ }, DOWNLOAD_TIMEOUT);
it('filetransfer.spec.6 should get 401 status on http basic auth failure', function (done) {
@@ -368,8 +374,13 @@ exports.defineAutoTests = function () {
done();
};
- transfer.download(fileURL, localFilePath, unexpectedCallbacks.httpWin, downloadFail);
- });
+ transfer.download(fileURL, localFilePath, unexpectedCallbacks.httpWin, downloadFail, null,
+ {
+ headers: {
+ 'If-Modified-Since': 'Thu, 19 Mar 2015 00:00:00 GMT'
+ }
+ });
+ }, DOWNLOAD_TIMEOUT);
it("filetransfer.spec.7 should download a file using file:// (when hosted from file://)", function (done) {
@@ -394,7 +405,7 @@ exports.defineAutoTests = function () {
};
transfer.download(fileURL, localFilePath, downloadWin, unexpectedCallbacks.httpFail);
- });
+ }, DOWNLOAD_TIMEOUT);
it("filetransfer.spec.8 should download a file using https://", function (done) {
@@ -419,15 +430,17 @@ exports.defineAutoTests = function () {
};
transfer.download(fileURL, localFilePath, downloadWin, unexpectedCallbacks.httpFail);
- });
+ }, DOWNLOAD_TIMEOUT);
it("filetransfer.spec.11 should call the error callback on abort()", function (done) {
var fileURL = 'http://cordova.apache.org/downloads/BlueZedEx.mp3';
transfer.download(fileURL, localFilePath, unexpectedCallbacks.httpWin, done);
- transfer.abort();
- });
+ setTimeout(function() {
+ transfer.abort();
+ }, ABORT_DELAY);
+ }, DOWNLOAD_TIMEOUT);
it("filetransfer.spec.9 should not leave partial file due to abort", function (done) {
@@ -452,7 +465,7 @@ exports.defineAutoTests = function () {
spyOn(transfer, 'onprogress').and.callThrough();
transfer.download(fileURL, localFilePath, unexpectedCallbacks.httpWin, downloadFail);
- });
+ }, DOWNLOAD_TIMEOUT);
it("filetransfer.spec.10 should be stopped by abort() right away", function (done) {
@@ -472,13 +485,15 @@ exports.defineAutoTests = function () {
};
transfer.download(fileURL, localFilePath, unexpectedCallbacks.httpWin, downloadFail);
- transfer.abort();
+ setTimeout(function() {
+ transfer.abort();
+ }, ABORT_DELAY);
// call abort() again, after a time greater than the grace period
setTimeout(function () {
expect(transfer.abort).not.toThrow();
}, GRACE_TIME_DELTA);
- });
+ }, DOWNLOAD_TIMEOUT);
it("filetransfer.spec.12 should get http status on failure", function (done) {
@@ -493,7 +508,7 @@ exports.defineAutoTests = function () {
};
transfer.download(fileURL, localFilePath, unexpectedCallbacks.httpWin, downloadFail);
- });
+ }, DOWNLOAD_TIMEOUT);
it("filetransfer.spec.13 should get http body on failure", function (done) {
@@ -511,7 +526,7 @@ exports.defineAutoTests = function () {
};
transfer.download(fileURL, localFilePath, unexpectedCallbacks.httpWin, downloadFail);
- });
+ }, DOWNLOAD_TIMEOUT);
it("filetransfer.spec.14 should handle malformed urls", function (done) {
@@ -531,33 +546,19 @@ exports.defineAutoTests = function () {
transfer.download(fileURL, localFilePath, unexpectedCallbacks.httpWin, downloadFail);
});
- describe('unknown host:', function () {
- var originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
+ it("filetransfer.spec.15 should handle unknown host", function (done) {
+ var fileURL = UNKNOWN_HOST;
- beforeEach(function() {
- jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
- });
+ var downloadFail = function (error) {
+ expect(error.code).toBe(FileTransferError.CONNECTION_ERR);
+ done();
+ };
- afterEach(function() {
- jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
- });
+ // turn off the onprogress handler
+ transfer.onprogress = function () {};
- it("filetransfer.spec.15 should handle unknown host", function (done) {
-
- var fileURL = UNKNOWN_HOST;
-
- var downloadFail = function (error) {
- expect(error.code).toBe(FileTransferError.CONNECTION_ERR);
- done();
- };
-
- // turn off the onprogress handler
- transfer.onprogress = function () {};
-
- transfer.download(fileURL, localFilePath, unexpectedCallbacks.httpWin, downloadFail);
- });
- });
-
+ transfer.download(fileURL, localFilePath, unexpectedCallbacks.httpWin, downloadFail);
+ }, 30000);
it("filetransfer.spec.16 should handle bad file path", function (done) {
var fileURL = SERVER;
@@ -580,7 +581,7 @@ exports.defineAutoTests = function () {
};
transfer.download(fileURL, localFilePath, downloadWin, unexpectedCallbacks.httpFail);
- });
+ }, DOWNLOAD_TIMEOUT);
it("filetransfer.spec.30 downloaded file entries should have a toNativeURL method", function (done) {
@@ -613,7 +614,7 @@ exports.defineAutoTests = function () {
};
transfer.download(fileURL, localFilePath, downloadWin, unexpectedCallbacks.httpFail);
- });
+ }, DOWNLOAD_TIMEOUT);
it("filetransfer.spec.28 (compatibility) should be able to download a file using local paths", function (done) {
@@ -757,15 +758,17 @@ exports.defineAutoTests = function () {
// NOTE: removing uploadOptions cause Android to timeout
transfer.upload(localFilePath, fileURL, unexpectedCallbacks.httpWin, uploadFail, uploadOptions);
- transfer.abort();
+ setTimeout(function() {
+ transfer.abort();
+ }, ABORT_DELAY);
setTimeout(function () {
expect(transfer.abort).not.toThrow();
}, GRACE_TIME_DELTA);
};
- writeFile(root, fileName, new Array(10000).join('aborttest!'), fileWin);
- });
+ writeFile(root, fileName, new Array(100000).join('aborttest!'), fileWin);
+ }, 10000); // we are creating a pretty big file here so we need some time
it("filetransfer.spec.22 should get http status and body on failure", function (done) {
@@ -804,7 +807,7 @@ exports.defineAutoTests = function () {
};
transfer.upload(localFilePath, fileURL, unexpectedCallbacks.httpWin, uploadFail, {});
- });
+ }, 30000); // unknown host may need quite some time on some devices
it("filetransfer.spec.25 should handle missing file", function (done) {