tests.js 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. /* global exports, cordova, FileTransfer, FileTransferError, FileUploadOptions, LocalFileSystem, WinJS */
  22. /* jshint jasmine: true */
  23. exports.defineAutoTests = function () {
  24. "use strict";
  25. // constants
  26. var ONE_SECOND = 1000; // in milliseconds
  27. var GRACE_TIME_DELTA = 600; // in milliseconds
  28. var DEFAULT_FILESYSTEM_SIZE = 1024 * 50; // filesystem size in bytes
  29. var UNKNOWN_HOST = "http://foobar.apache.org";
  30. var DOWNLOAD_TIMEOUT = 15 * ONE_SECOND;
  31. var LONG_TIMEOUT = 60 * ONE_SECOND;
  32. var UPLOAD_TIMEOUT = 15 * ONE_SECOND;
  33. var ABORT_DELAY = 100; // for abort() tests
  34. var LATIN1_SYMBOLS = '¥§©ÆÖÑøøø¼';
  35. var DATA_URI_PREFIX = "data:image/png;base64,";
  36. var DATA_URI_CONTENT = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
  37. var DATA_URI_CONTENT_LENGTH = 85; // bytes. (This is the raw file size: used https://en.wikipedia.org/wiki/File:Red-dot-5px.png from https://en.wikipedia.org/wiki/Data_URI_scheme)
  38. var RETRY_COUNT = 100; // retry some flaky tests (yes, THIS many times, due to Heroku server instability)
  39. var RETRY_INTERVAL = 100;
  40. // upload test server address
  41. // NOTE:
  42. // more info at https://github.com/apache/cordova-labs/tree/cordova-filetransfer
  43. // Will get it from the config
  44. // you can specify it as a 'FILETRANSFER_SERVER_ADDRESS' variable upon test plugin installation
  45. // or change the default value in plugin.xml
  46. var SERVER = "";
  47. var SERVER_WITH_CREDENTIALS = "";
  48. // flags
  49. var isWindows = cordova.platformId === "windows8" || cordova.platformId === "windows";
  50. var isWindowsPhone81 = isWindows && WinJS.Utilities.isPhone;
  51. var isWP8 = cordova.platformId === "windowsphone";
  52. var isBrowser = cordova.platformId === "browser";
  53. var isIE = isBrowser && navigator.userAgent.indexOf("Trident") >= 0;
  54. var isIos = cordova.platformId === "ios";
  55. var isIot = cordova.platformId === "android" && navigator.userAgent.indexOf("iot") >= 0;
  56. // tests
  57. describe("FileTransferError", function () {
  58. it("should exist", function () {
  59. expect(FileTransferError).toBeDefined();
  60. });
  61. it("should be constructable", function () {
  62. var transferError = new FileTransferError();
  63. expect(transferError).toBeDefined();
  64. });
  65. it("filetransfer.spec.3 should expose proper constants", function () {
  66. expect(FileTransferError.FILE_NOT_FOUND_ERR).toBeDefined();
  67. expect(FileTransferError.INVALID_URL_ERR).toBeDefined();
  68. expect(FileTransferError.CONNECTION_ERR).toBeDefined();
  69. expect(FileTransferError.ABORT_ERR).toBeDefined();
  70. expect(FileTransferError.NOT_MODIFIED_ERR).toBeDefined();
  71. expect(FileTransferError.FILE_NOT_FOUND_ERR).toBe(1);
  72. expect(FileTransferError.INVALID_URL_ERR).toBe(2);
  73. expect(FileTransferError.CONNECTION_ERR).toBe(3);
  74. expect(FileTransferError.ABORT_ERR).toBe(4);
  75. expect(FileTransferError.NOT_MODIFIED_ERR).toBe(5);
  76. });
  77. });
  78. describe("FileUploadOptions", function () {
  79. it("should exist", function () {
  80. expect(FileUploadOptions).toBeDefined();
  81. });
  82. it("should be constructable", function () {
  83. var transferOptions = new FileUploadOptions();
  84. expect(transferOptions).toBeDefined();
  85. });
  86. });
  87. describe("FileTransfer", function () {
  88. this.persistentRoot = null;
  89. this.tempRoot = null;
  90. // named callbacks
  91. var unexpectedCallbacks = {
  92. httpFail: function () {},
  93. httpWin: function () {},
  94. fileSystemFail: function () {},
  95. fileSystemWin: function () {},
  96. fileOperationFail: function () {},
  97. fileOperationWin: function () {},
  98. };
  99. var expectedCallbacks = {
  100. unsupportedOperation: function (response) {
  101. console.log("spec called unsupported functionality; response:", response);
  102. },
  103. };
  104. // helpers
  105. var deleteFile = function (fileSystem, name, done) {
  106. fileSystem.getFile(name, null,
  107. function (fileEntry) {
  108. fileEntry.remove(
  109. function () {
  110. done();
  111. },
  112. function () {
  113. throw new Error("failed to delete: '" + name + "'");
  114. }
  115. );
  116. },
  117. function () {
  118. done();
  119. }
  120. );
  121. };
  122. var writeFile = function (fileSystem, name, content, success, done) {
  123. var fileOperationFail = function() {
  124. unexpectedCallbacks.fileOperationFail();
  125. done();
  126. };
  127. fileSystem.getFile(name, { create: true },
  128. function (fileEntry) {
  129. fileEntry.createWriter(function (writer) {
  130. writer.onwrite = function () {
  131. success(fileEntry);
  132. };
  133. writer.onabort = function (evt) {
  134. throw new Error("aborted creating test file '" + name + "': " + evt);
  135. };
  136. writer.error = function (evt) {
  137. throw new Error("aborted creating test file '" + name + "': " + evt);
  138. };
  139. if (cordova.platformId === "browser") {
  140. var blob = new Blob([content + "\n"], { type: "text/plain" });
  141. writer.write(blob);
  142. } else {
  143. writer.write(content + "\n");
  144. }
  145. }, fileOperationFail);
  146. },
  147. function () {
  148. throw new Error("could not create test file '" + name + "'");
  149. }
  150. );
  151. };
  152. // according to documentation, wp8 does not support onProgress:
  153. // https://github.com/apache/cordova-plugin-file-transfer/blob/master/doc/index.md#supported-platforms
  154. var wp8OnProgressHandler = function () {};
  155. var defaultOnProgressHandler = function (event) {
  156. if (event.lengthComputable) {
  157. expect(event.loaded).toBeGreaterThan(1);
  158. expect(event.total).toBeGreaterThan(0);
  159. expect(event.total).not.toBeLessThan(event.loaded);
  160. expect(event.lengthComputable).toBe(true, "lengthComputable");
  161. } else {
  162. // In IE, when lengthComputable === false, event.total somehow is equal to 2^64
  163. if (isIE) {
  164. expect(event.total).toBe(Math.pow(2, 64));
  165. } else {
  166. // iOS returns -1, and other platforms return 0
  167. expect(event.total).toBeLessThan(1);
  168. }
  169. }
  170. };
  171. var getMalformedUrl = function () {
  172. if (cordova.platformId === "android" || cordova.platformId === "amazon-fireos") {
  173. // bad protocol causes a MalformedUrlException on Android
  174. return "httpssss://example.com";
  175. } else {
  176. // iOS doesn't care about protocol, space in hostname causes error
  177. return "httpssss://exa mple.com";
  178. }
  179. };
  180. var setServerAddress = function (address) {
  181. SERVER = address;
  182. SERVER_WITH_CREDENTIALS = SERVER.replace('http://', 'http://cordova_user:cordova_password@');
  183. };
  184. // NOTE:
  185. // there are several beforeEach calls, one per async call; since calling done()
  186. // signifies a completed async call, each async call needs its own done(), and
  187. // therefore its own beforeEach
  188. beforeEach(function (done) {
  189. var specContext = this;
  190. window.requestFileSystem(LocalFileSystem.PERSISTENT, DEFAULT_FILESYSTEM_SIZE,
  191. function (fileSystem) {
  192. specContext.persistentRoot = fileSystem.root;
  193. done();
  194. },
  195. function () {
  196. throw new Error("Failed to initialize persistent file system.");
  197. }
  198. );
  199. });
  200. beforeEach(function (done) {
  201. var specContext = this;
  202. window.requestFileSystem(LocalFileSystem.TEMPORARY, DEFAULT_FILESYSTEM_SIZE,
  203. function (fileSystem) {
  204. specContext.tempRoot = fileSystem.root;
  205. done();
  206. },
  207. function () {
  208. throw new Error("Failed to initialize temporary file system.");
  209. }
  210. );
  211. });
  212. // spy on all named callbacks
  213. beforeEach(function() {
  214. // ignore the actual implementations of the unexpected callbacks
  215. for (var callback in unexpectedCallbacks) {
  216. if (unexpectedCallbacks.hasOwnProperty(callback)) {
  217. spyOn(unexpectedCallbacks, callback);
  218. }
  219. }
  220. // but run the implementations of the expected callbacks
  221. for (callback in expectedCallbacks) { // jshint ignore: line
  222. if (expectedCallbacks.hasOwnProperty(callback)) {
  223. spyOn(expectedCallbacks, callback).and.callThrough();
  224. }
  225. }
  226. });
  227. // at the end, check that none of the unexpected callbacks got called,
  228. // and act on the expected callbacks
  229. afterEach(function() {
  230. for (var callback in unexpectedCallbacks) {
  231. if (unexpectedCallbacks.hasOwnProperty(callback)) {
  232. expect(unexpectedCallbacks[callback]).not.toHaveBeenCalled();
  233. }
  234. }
  235. if (expectedCallbacks.unsupportedOperation.calls.any()) {
  236. pending();
  237. }
  238. });
  239. it ("util spec: get file transfer server url", function () {
  240. try {
  241. // attempt to synchronously load medic config
  242. var xhr = new XMLHttpRequest();
  243. xhr.open("GET", "../fileTransferOpts.json", false);
  244. xhr.send(null);
  245. var parsedCfg = JSON.parse(xhr.responseText);
  246. if (parsedCfg.serverAddress) {
  247. setServerAddress(parsedCfg.serverAddress);
  248. }
  249. } catch (ex) {
  250. console.error('Unable to load file transfer server url: ' + ex);
  251. console.error('Note: if you are testing this on cordova-ios with cordova-plugin-wkwebview-engine, that may be why you are getting this error. See https://issues.apache.org/jira/browse/CB-10144.');
  252. fail(ex);
  253. }
  254. });
  255. it("should initialise correctly", function() {
  256. expect(this.persistentRoot).toBeDefined();
  257. expect(this.tempRoot).toBeDefined();
  258. });
  259. it("should exist", function () {
  260. expect(FileTransfer).toBeDefined();
  261. });
  262. it("filetransfer.spec.1 should be constructable", function () {
  263. var transfer = new FileTransfer();
  264. expect(transfer).toBeDefined();
  265. });
  266. it("filetransfer.spec.2 should expose proper functions", function () {
  267. var transfer = new FileTransfer();
  268. expect(transfer.upload).toBeDefined();
  269. expect(transfer.download).toBeDefined();
  270. expect(transfer.upload).toEqual(jasmine.any(Function));
  271. expect(transfer.download).toEqual(jasmine.any(Function));
  272. });
  273. describe("methods", function() {
  274. this.transfer = null;
  275. this.root = null;
  276. this.fileName = null;
  277. this.localFilePath = null;
  278. beforeEach(function() {
  279. this.transfer = new FileTransfer();
  280. // assign onprogress handler
  281. this.transfer.onprogress = isWP8 ? wp8OnProgressHandler : defaultOnProgressHandler;
  282. // spy on the onprogress handler, but still call through to it
  283. spyOn(this.transfer, "onprogress").and.callThrough();
  284. this.root = this.persistentRoot;
  285. this.fileName = "testFile.txt";
  286. this.localFilePath = this.root.toURL() + this.fileName;
  287. });
  288. // NOTE:
  289. // if download tests are failing, check the
  290. // URL white list for the following URLs:
  291. // - 'httpssss://example.com'
  292. // - 'apache.org', with subdomains="true"
  293. // - 'cordova-filetransfer.jitsu.com'
  294. describe("download", function () {
  295. // helpers
  296. var verifyDownload = function (fileEntry, specContext) {
  297. expect(fileEntry.name).toBe(specContext.fileName);
  298. };
  299. // delete the downloaded file
  300. afterEach(function (done) {
  301. deleteFile(this.root, this.fileName, done);
  302. });
  303. it("ensures that test file does not exist", function (done) {
  304. deleteFile(this.root, this.fileName, done);
  305. });
  306. it("filetransfer.spec.4 should download a file", function (done) {
  307. var fileURL = SERVER + "/robots.txt";
  308. var specContext = this;
  309. var fileWin = function (blob) {
  310. if (specContext.transfer.onprogress.calls.any()) {
  311. var lastProgressEvent = specContext.transfer.onprogress.calls.mostRecent().args[0];
  312. expect(lastProgressEvent.loaded).not.toBeGreaterThan(blob.size);
  313. } else {
  314. console.log("no progress events were emitted");
  315. }
  316. done();
  317. };
  318. var fileSystemFail = function() {
  319. unexpectedCallbacks.fileSystemFail();
  320. done();
  321. };
  322. var downloadFail = function() {
  323. unexpectedCallbacks.httpFail();
  324. done();
  325. };
  326. var downloadWin = function (entry) {
  327. verifyDownload(entry, specContext);
  328. // verify the FileEntry representing this file
  329. entry.file(fileWin, fileSystemFail);
  330. };
  331. specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
  332. }, DOWNLOAD_TIMEOUT * 10); // to give Heroku server some time to wake up
  333. it("filetransfer.spec.4.1 should download a file using target name with space", function (done) {
  334. var fileURL = SERVER + "/robots.txt";
  335. this.fileName = "test file.txt";
  336. this.localFilePath = this.root.toURL() + this.fileName;
  337. var specContext = this;
  338. var fileWin = function (blob) {
  339. if (specContext.transfer.onprogress.calls.any()) {
  340. var lastProgressEvent = specContext.transfer.onprogress.calls.mostRecent().args[0];
  341. expect(lastProgressEvent.loaded).not.toBeGreaterThan(blob.size);
  342. } else {
  343. console.log("no progress events were emitted");
  344. }
  345. done();
  346. };
  347. var fileSystemFail = function() {
  348. unexpectedCallbacks.fileSystemFail();
  349. done();
  350. };
  351. var downloadFail = function() {
  352. unexpectedCallbacks.httpFail();
  353. done();
  354. };
  355. var downloadWin = function (entry) {
  356. verifyDownload(entry, specContext);
  357. // verify the FileEntry representing this file
  358. entry.file(fileWin, fileSystemFail);
  359. };
  360. specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
  361. }, DOWNLOAD_TIMEOUT);
  362. it("filetransfer.spec.5 should download a file using http basic auth", function (done) {
  363. var fileURL = SERVER_WITH_CREDENTIALS + "/download_basic_auth";
  364. var specContext = this;
  365. var downloadWin = function (entry) {
  366. verifyDownload(entry, specContext);
  367. done();
  368. };
  369. var downloadFail = function() {
  370. unexpectedCallbacks.httpFail();
  371. done();
  372. };
  373. specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
  374. }, DOWNLOAD_TIMEOUT);
  375. it("filetransfer.spec.6 should get 401 status on http basic auth failure", function (done) {
  376. // NOTE:
  377. // using server without credentials
  378. var fileURL = SERVER + "/download_basic_auth";
  379. var downloadFail = function (error) {
  380. expect(error.http_status).toBe(401);
  381. expect(error.http_status).not.toBe(404, "Ensure " + fileURL + " is in the white list");
  382. done();
  383. };
  384. var downloadWin = function() {
  385. unexpectedCallbacks.httpWin();
  386. done();
  387. };
  388. this.transfer.download(fileURL, this.localFilePath, downloadWin, downloadFail, null,
  389. {
  390. headers: {
  391. "If-Modified-Since": "Thu, 19 Mar 2015 00:00:00 GMT"
  392. }
  393. });
  394. }, DOWNLOAD_TIMEOUT);
  395. it("filetransfer.spec.7 should download a file using file:// (when hosted from file://)", function (done) {
  396. // for Windows platform it's ms-appdata:/// by default, not file://
  397. if (isWindows) {
  398. pending();
  399. return;
  400. }
  401. var fileURL = window.location.protocol + "//" + window.location.pathname.replace(/ /g, "%20");
  402. var specContext = this;
  403. if (!/^file:/.exec(fileURL) && cordova.platformId !== "blackberry10") {
  404. if (cordova.platformId === "windowsphone") {
  405. expect(fileURL).toMatch(/^x-wmapp0:/);
  406. }
  407. done();
  408. return;
  409. }
  410. var downloadWin = function (entry) {
  411. verifyDownload(entry, specContext);
  412. done();
  413. };
  414. var downloadFail = function() {
  415. unexpectedCallbacks.httpFail();
  416. done();
  417. };
  418. specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
  419. }, DOWNLOAD_TIMEOUT);
  420. it("filetransfer.spec.8 should download a file using https://", function (done) {
  421. var fileURL = "https://www.apache.org/licenses/";
  422. var specContext = this;
  423. var downloadFail = function() {
  424. unexpectedCallbacks.httpFail();
  425. done();
  426. };
  427. var fileOperationFail = function() {
  428. unexpectedCallbacks.fileOperationFail();
  429. done();
  430. };
  431. var fileSystemFail = function() {
  432. unexpectedCallbacks.fileSystemFail();
  433. done();
  434. };
  435. var fileWin = function (file) {
  436. var reader = new FileReader();
  437. reader.onerror = fileOperationFail;
  438. reader.onload = function () {
  439. expect(reader.result).toMatch(/The Apache Software Foundation/);
  440. done();
  441. };
  442. reader.readAsText(file);
  443. };
  444. var downloadWin = function (entry) {
  445. verifyDownload(entry, specContext);
  446. entry.file(fileWin, fileSystemFail);
  447. };
  448. specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
  449. }, isWindows ? LONG_TIMEOUT : DOWNLOAD_TIMEOUT);
  450. it("filetransfer.spec.11 should call the error callback on abort()", function (done) {
  451. var fileURL = "http://cordova.apache.org/downloads/BlueZedEx.mp3";
  452. fileURL = fileURL + "?q=" + (new Date()).getTime();
  453. var specContext = this;
  454. var downloadWin = function () {
  455. unexpectedCallbacks.httpWin();
  456. done();
  457. };
  458. specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, done);
  459. setTimeout(function() {
  460. specContext.transfer.abort();
  461. }, ABORT_DELAY);
  462. }, DOWNLOAD_TIMEOUT);
  463. it("filetransfer.spec.9 should not leave partial file due to abort", function (done) {
  464. var fileURL = "http://cordova.apache.org/downloads/logos_2.zip";
  465. var specContext = this;
  466. var fileSystemWin = function() {
  467. unexpectedCallbacks.fileSystemWin();
  468. done();
  469. };
  470. var downloadWin = function() {
  471. unexpectedCallbacks.httpWin();
  472. done();
  473. };
  474. var downloadFail = function (error) {
  475. var result = (error.code === FileTransferError.ABORT_ERR || error.code === FileTransferError.CONNECTION_ERR)? true: false;
  476. if (!result) {
  477. fail("Expected " + error.code + " to be " + FileTransferError.ABORT_ERR + " or " + FileTransferError.CONNECTION_ERR);
  478. }
  479. expect(specContext.transfer.onprogress).toHaveBeenCalled();
  480. // check that there is no file
  481. specContext.root.getFile(specContext.fileName, null, fileSystemWin, done);
  482. };
  483. // abort at the first onprogress event
  484. specContext.transfer.onprogress = function (event) {
  485. if (event.loaded > 0) {
  486. specContext.transfer.abort();
  487. }
  488. };
  489. spyOn(specContext.transfer, "onprogress").and.callThrough();
  490. specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
  491. }, isWindows ? LONG_TIMEOUT : DOWNLOAD_TIMEOUT);
  492. it("filetransfer.spec.10 should be stopped by abort()", function (done) {
  493. var fileURL = "http://cordova.apache.org/downloads/BlueZedEx.mp3";
  494. fileURL = fileURL + "?q=" + (new Date()).getTime();
  495. var specContext = this;
  496. expect(specContext.transfer.abort).not.toThrow(); // should be a no-op.
  497. var downloadWin = function() {
  498. unexpectedCallbacks.httpWin();
  499. done();
  500. };
  501. var downloadFail = function (error) {
  502. expect(error.code).toBe(FileTransferError.ABORT_ERR);
  503. // delay calling done() to wait for the bogus abort()
  504. setTimeout(done, GRACE_TIME_DELTA * 2);
  505. };
  506. specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, downloadFail);
  507. setTimeout(function() {
  508. specContext.transfer.abort();
  509. }, ABORT_DELAY);
  510. // call abort() again, after a time greater than the grace period
  511. setTimeout(function () {
  512. expect(specContext.transfer.abort).not.toThrow();
  513. }, GRACE_TIME_DELTA);
  514. }, DOWNLOAD_TIMEOUT);
  515. it("filetransfer.spec.12 should get http status on failure", function (done) {
  516. var fileURL = SERVER + "/404";
  517. var downloadFail = function (error) {
  518. expect(error.http_status).not.toBe(401, "Ensure " + fileURL + " is in the white list");
  519. expect(error.http_status).toBe(404);
  520. // wp8 does not make difference between 404 and unknown host
  521. if (isWP8) {
  522. expect(error.code).toBe(FileTransferError.CONNECTION_ERR);
  523. } else {
  524. expect(error.code).toBe(FileTransferError.FILE_NOT_FOUND_ERR);
  525. }
  526. done();
  527. };
  528. var downloadWin = function() {
  529. unexpectedCallbacks.httpWin();
  530. done();
  531. };
  532. this.transfer.download(fileURL, this.localFilePath, downloadWin, downloadFail);
  533. }, DOWNLOAD_TIMEOUT);
  534. it("filetransfer.spec.13 should get http body on failure", function (done) {
  535. var fileURL = SERVER + "/404";
  536. var downloadFail = function (error) {
  537. expect(error.http_status).not.toBe(401, "Ensure " + fileURL + " is in the white list");
  538. expect(error.http_status).toBe(404);
  539. expect(error.body).toBeDefined();
  540. expect(error.body).toMatch("You requested a 404");
  541. done();
  542. };
  543. var downloadWin = function() {
  544. unexpectedCallbacks.httpWin();
  545. done();
  546. };
  547. this.transfer.download(fileURL, this.localFilePath, downloadWin, downloadFail);
  548. }, DOWNLOAD_TIMEOUT);
  549. it("filetransfer.spec.14 should handle malformed urls", function (done) {
  550. var fileURL = getMalformedUrl();
  551. var downloadFail = function (error) {
  552. // Note: Android needs the bad protocol to be added to the access list
  553. // <access origin=".*"/> won't match because ^https?:// is prepended to the regex
  554. // The bad protocol must begin with http to avoid automatic prefix
  555. expect(error.http_status).not.toBe(401, "Ensure " + fileURL + " is in the white list");
  556. expect(error.code).toBe(FileTransferError.INVALID_URL_ERR);
  557. done();
  558. };
  559. var downloadWin = function() {
  560. unexpectedCallbacks.httpWin();
  561. done();
  562. };
  563. this.transfer.download(fileURL, this.localFilePath, downloadWin, downloadFail);
  564. });
  565. it("filetransfer.spec.15 should handle unknown host", function (done) {
  566. var fileURL = UNKNOWN_HOST;
  567. var downloadFail = function (error) {
  568. expect(error.code).toBe(FileTransferError.CONNECTION_ERR);
  569. done();
  570. };
  571. var downloadWin = function() {
  572. unexpectedCallbacks.httpWin();
  573. done();
  574. };
  575. // turn off the onprogress handler
  576. this.transfer.onprogress = function () {};
  577. this.transfer.download(fileURL, this.localFilePath, downloadWin, downloadFail);
  578. }, isWindows ? LONG_TIMEOUT : DOWNLOAD_TIMEOUT);
  579. it("filetransfer.spec.16 should handle bad file path", function (done) {
  580. var fileURL = SERVER;
  581. var downloadWin = function() {
  582. unexpectedCallbacks.httpWin();
  583. done();
  584. };
  585. this.transfer.download(fileURL, "c:\\54321", downloadWin, done);
  586. });
  587. it("filetransfer.spec.17 progress should work with gzip encoding", function (done) {
  588. // lengthComputable false on bb10 when downloading gzip
  589. if (cordova.platformId === "blackberry10") {
  590. pending();
  591. return;
  592. }
  593. var fileURL = "http://www.apache.org/";
  594. var specContext = this;
  595. var downloadWin = function (entry) {
  596. verifyDownload(entry, specContext);
  597. done();
  598. };
  599. var downloadFail = function () {
  600. unexpectedCallbacks.httpFail();
  601. done();
  602. };
  603. specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin,downloadFail);
  604. }, DOWNLOAD_TIMEOUT);
  605. it("filetransfer.spec.30 downloaded file entries should have a toNativeURL method", function (done) {
  606. if (cordova.platformId === "browser") {
  607. pending();
  608. return;
  609. }
  610. var fileURL = SERVER + "/robots.txt";
  611. var downloadWin = function (entry) {
  612. expect(entry.toNativeURL).toBeDefined();
  613. expect(entry.toNativeURL).toEqual(jasmine.any(Function));
  614. var nativeURL = entry.toNativeURL();
  615. expect(nativeURL).toBeTruthy();
  616. expect(nativeURL).toEqual(jasmine.any(String));
  617. if (isWindows) {
  618. expect(nativeURL.substring(0, 14)).toBe("ms-appdata:///");
  619. } else if (isWP8) {
  620. expect(nativeURL.substring(0, 1)).toBe("/");
  621. } else {
  622. expect(nativeURL.substring(0, 7)).toBe("file://");
  623. }
  624. done();
  625. };
  626. var downloadFail = function() {
  627. unexpectedCallbacks.httpFail();
  628. done();
  629. };
  630. this.transfer.download(fileURL, this.localFilePath, downloadWin, downloadFail);
  631. }, DOWNLOAD_TIMEOUT);
  632. it("filetransfer.spec.28 (compatibility) should be able to download a file using local paths", function (done) {
  633. var fileURL = SERVER + "/robots.txt";
  634. var specContext = this;
  635. var unsupported = function (response) {
  636. expectedCallbacks.unsupportedOperation(response);
  637. done();
  638. };
  639. var downloadWin = function (entry) {
  640. verifyDownload(entry, specContext);
  641. done();
  642. };
  643. var internalFilePath;
  644. if (specContext.root.toInternalURL) {
  645. internalFilePath = specContext.root.toInternalURL() + specContext.fileName;
  646. } else {
  647. internalFilePath = specContext.localFilePath;
  648. }
  649. var downloadFail = function() {
  650. unexpectedCallbacks.httpFail();
  651. done();
  652. };
  653. // This is an undocumented interface to File which exists only for testing
  654. // backwards compatibilty. By obtaining the raw filesystem path of the download
  655. // location, we can pass that to transfer.download() to make sure that previously-stored
  656. // paths are still valid.
  657. cordova.exec(function (localPath) {
  658. specContext.transfer.download(fileURL, localPath, downloadWin, downloadFail);
  659. }, unsupported, "File", "_getLocalFilesystemPath", [internalFilePath]);
  660. });
  661. it("filetransfer.spec.33 should properly handle 304", function (done) {
  662. if (isWP8) {
  663. pending();
  664. return;
  665. }
  666. var downloadFail = function (error) {
  667. expect(error.http_status).toBe(304);
  668. expect(error.code).toBe(FileTransferError.NOT_MODIFIED_ERR);
  669. done();
  670. };
  671. var downloadWin = function() {
  672. unexpectedCallbacks.httpWin();
  673. done();
  674. };
  675. this.transfer.download(SERVER + '/304', this.localFilePath, downloadWin, downloadFail);
  676. }, DOWNLOAD_TIMEOUT);
  677. it("filetransfer.spec.35 304 should not result in the deletion of a cached file", function (done) {
  678. if (isWP8) {
  679. pending();
  680. return;
  681. }
  682. var specContext = this;
  683. var fileOperationFail = function() {
  684. unexpectedCallbacks.fileOperationFail();
  685. done();
  686. };
  687. var fileSystemFail = function() {
  688. unexpectedCallbacks.fileSystemFail();
  689. done();
  690. };
  691. var httpWin = function() {
  692. unexpectedCallbacks.httpWin();
  693. done();
  694. };
  695. var downloadFail = function (error) {
  696. expect(error.http_status).toBe(304);
  697. expect(error.code).toBe(FileTransferError.NOT_MODIFIED_ERR);
  698. specContext.persistentRoot.getFile(specContext.fileName, { create: false },
  699. function (entry) {
  700. var fileWin = function (file) {
  701. var reader = new FileReader();
  702. reader.onerror = fileOperationFail;
  703. reader.onloadend = function () {
  704. expect(reader.result).toBeTruthy();
  705. if (reader.result !== null) {
  706. expect(reader.result.length).toBeGreaterThan(0);
  707. }
  708. done();
  709. };
  710. reader.readAsBinaryString(file);
  711. };
  712. entry.file(fileWin, fileSystemFail);
  713. },
  714. function (err) {
  715. expect("Could not open test file '" + specContext.fileName + "': " + JSON.stringify(err)).not.toBeDefined();
  716. done();
  717. }
  718. );
  719. };
  720. writeFile(specContext.root, specContext.fileName, 'Temp data', function () {
  721. specContext.transfer.download(SERVER + '/304', specContext.localFilePath, httpWin, downloadFail);
  722. }, fileOperationFail);
  723. }, DOWNLOAD_TIMEOUT);
  724. it("filetransfer.spec.36 should handle non-UTF8 encoded download response", function (done) {
  725. // Only iOS is supported: https://issues.apache.org/jira/browse/CB-9840
  726. if (!isIos) {
  727. pending();
  728. }
  729. var fileURL = SERVER + '/download_non_utf';
  730. var specContext = this;
  731. var fileOperationFail = function() {
  732. unexpectedCallbacks.fileOperationFail();
  733. done();
  734. };
  735. var fileSystemFail = function() {
  736. unexpectedCallbacks.fileSystemFail();
  737. done();
  738. };
  739. var httpFail = function() {
  740. unexpectedCallbacks.httpFail();
  741. done();
  742. };
  743. var fileWin = function (blob) {
  744. if (specContext.transfer.onprogress.calls.any()) {
  745. var lastProgressEvent = specContext.transfer.onprogress.calls.mostRecent().args[0];
  746. expect(lastProgressEvent.loaded).not.toBeGreaterThan(blob.size);
  747. } else {
  748. console.log("no progress events were emitted");
  749. }
  750. expect(blob.size).toBeGreaterThan(0);
  751. var reader = new FileReader();
  752. reader.onerror = fileOperationFail;
  753. reader.onloadend = function () {
  754. expect(reader.result.indexOf(LATIN1_SYMBOLS)).not.toBe(-1);
  755. done();
  756. };
  757. reader.readAsBinaryString(blob);
  758. };
  759. var downloadWin = function (entry) {
  760. verifyDownload(entry, specContext);
  761. // verify the FileEntry representing this file
  762. entry.file(fileWin, fileSystemFail);
  763. };
  764. specContext.transfer.download(fileURL, specContext.localFilePath, downloadWin, httpFail);
  765. }, UPLOAD_TIMEOUT);
  766. });
  767. describe("upload", function() {
  768. this.uploadParams = null;
  769. this.uploadOptions = null;
  770. this.fileName = null;
  771. this.fileContents = null;
  772. this.localFilePath = null;
  773. // helpers
  774. var verifyUpload = function (uploadResult, specContext) {
  775. expect(uploadResult.bytesSent).toBeGreaterThan(0);
  776. expect(uploadResult.responseCode).toBe(200);
  777. var obj = null;
  778. try {
  779. obj = JSON.parse(uploadResult.response);
  780. expect(obj.fields).toBeDefined();
  781. expect(obj.fields.value1).toBe("test");
  782. expect(obj.fields.value2).toBe("param");
  783. } catch (e) {
  784. expect(obj).not.toBeNull("returned data from server should be valid json");
  785. }
  786. expect(specContext.transfer.onprogress).toHaveBeenCalled();
  787. };
  788. beforeEach(function(done) {
  789. var specContext = this;
  790. specContext.fileName = "fileToUpload.txt";
  791. specContext.fileContents = "upload test file";
  792. specContext.uploadParams = {};
  793. specContext.uploadParams.value1 = "test";
  794. specContext.uploadParams.value2 = "param";
  795. specContext.uploadOptions = new FileUploadOptions();
  796. specContext.uploadOptions.fileKey = "file";
  797. specContext.uploadOptions.fileName = specContext.fileName;
  798. specContext.uploadOptions.mimeType = "text/plain";
  799. specContext.uploadOptions.params = specContext.uploadParams;
  800. var fileWin = function (entry) {
  801. specContext.localFilePath = entry.toURL();
  802. done();
  803. };
  804. // create a file to upload
  805. writeFile(specContext.root, specContext.fileName, specContext.fileContents, fileWin, done);
  806. });
  807. // delete the uploaded file
  808. afterEach(function (done) {
  809. deleteFile(this.root, this.fileName, done);
  810. });
  811. it("filetransfer.spec.18 should be able to upload a file", function (done) {
  812. var fileURL = SERVER + "/upload";
  813. var specContext = this;
  814. var uploadWin = function (uploadResult) {
  815. verifyUpload(uploadResult, specContext);
  816. if (cordova.platformId === "ios") {
  817. expect(uploadResult.headers).toBeDefined("Expected headers to be defined.");
  818. expect(uploadResult.headers["Content-Type"]).toBeDefined("Expected content-type header to be defined.");
  819. }
  820. done();
  821. };
  822. var uploadFail = function() {
  823. unexpectedCallbacks.httpFail();
  824. done();
  825. };
  826. // NOTE: removing uploadOptions cause Android to timeout
  827. specContext.transfer.upload(specContext.localFilePath, fileURL, uploadWin, uploadFail, specContext.uploadOptions);
  828. }, UPLOAD_TIMEOUT);
  829. it("filetransfer.spec.19 should be able to upload a file with http basic auth", function (done) {
  830. var fileURL = SERVER_WITH_CREDENTIALS + "/upload_basic_auth";
  831. var specContext = this;
  832. var uploadWin = function (uploadResult) {
  833. verifyUpload(uploadResult, specContext);
  834. done();
  835. };
  836. var uploadFail = function() {
  837. unexpectedCallbacks.httpFail();
  838. done();
  839. };
  840. // NOTE: removing uploadOptions cause Android to timeout
  841. specContext.transfer.upload(specContext.localFilePath, fileURL, uploadWin, uploadFail, specContext.uploadOptions);
  842. }, UPLOAD_TIMEOUT);
  843. it("filetransfer.spec.21 should be stopped by abort()", function (done) {
  844. var fileURL = SERVER + "/upload";
  845. var specContext = this;
  846. var uploadFail = function (e) {
  847. expect(e.code).toBe(FileTransferError.ABORT_ERR);
  848. // delay calling done() to wait for the bogus abort()
  849. setTimeout(done, GRACE_TIME_DELTA * 2);
  850. };
  851. var uploadWin = function() {
  852. unexpectedCallbacks.httpWin();
  853. done();
  854. };
  855. var fileWin = function () {
  856. expect(specContext.transfer.abort).not.toThrow();
  857. // NOTE: removing uploadOptions cause Android to timeout
  858. specContext.transfer.upload(specContext.localFilePath, fileURL, uploadWin, uploadFail, specContext.uploadOptions);
  859. setTimeout(function() {
  860. specContext.transfer.abort();
  861. }, ABORT_DELAY);
  862. setTimeout(function () {
  863. expect(specContext.transfer.abort).not.toThrow();
  864. }, GRACE_TIME_DELTA);
  865. };
  866. // windows store and ios are too fast, win is called before we have a chance to abort
  867. // so let's get them busy - while not providing an extra load to the slow Android emulators
  868. var arrayLength = ((isWindows && !isWindowsPhone81) || isIos) ? 3000000 : isIot ? 150000 : 200000;
  869. writeFile(specContext.root, specContext.fileName, new Array(arrayLength).join("aborttest!"), fileWin, done);
  870. }, UPLOAD_TIMEOUT);
  871. it("filetransfer.spec.22 should get http status and body on failure", function (done) {
  872. var fileURL = SERVER + "/403";
  873. var retryCount = 0;
  874. var self = this;
  875. var uploadWin = function() {
  876. unexpectedCallbacks.httpWin();
  877. done();
  878. };
  879. var uploadFail = function (error) {
  880. if (error.http_status === 503 && ++retryCount <= RETRY_COUNT) {
  881. // Heroku often gives this error, retry in 1 second
  882. console.log('retrying... ' + retryCount);
  883. setTimeout(function () {
  884. self.transfer.upload(self.localFilePath, fileURL, uploadWin, uploadFail, self.uploadOptions);
  885. }, RETRY_INTERVAL);
  886. } else {
  887. expect(error.http_status).toBe(403);
  888. expect(error.http_status).not.toBe(401, "Ensure " + fileURL + " is in the white list");
  889. done();
  890. }
  891. };
  892. self.transfer.upload(this.localFilePath, fileURL, uploadWin, uploadFail, this.uploadOptions);
  893. }, UPLOAD_TIMEOUT * 11);
  894. it("filetransfer.spec.24 should handle malformed urls", function (done) {
  895. var fileURL = getMalformedUrl();
  896. var uploadFail = function (error) {
  897. expect(error.code).toBe(FileTransferError.INVALID_URL_ERR);
  898. expect(error.http_status).not.toBe(401, "Ensure " + fileURL + " is in the white list");
  899. done();
  900. };
  901. var uploadWin = function() {
  902. unexpectedCallbacks.httpWin();
  903. done();
  904. };
  905. this.transfer.upload(this.localFilePath, fileURL, uploadWin, uploadFail, {});
  906. });
  907. it("filetransfer.spec.25 should handle unknown host", function (done) {
  908. var fileURL = UNKNOWN_HOST;
  909. var uploadFail = function (error) {
  910. expect(error.code).toBe(FileTransferError.CONNECTION_ERR);
  911. expect(error.http_status).not.toBe(401, "Ensure " + fileURL + " is in the white list");
  912. done();
  913. };
  914. var uploadWin = function() {
  915. unexpectedCallbacks.httpWin();
  916. done();
  917. };
  918. this.transfer.upload(this.localFilePath, fileURL, uploadWin, uploadFail, {});
  919. }, UPLOAD_TIMEOUT);
  920. it("filetransfer.spec.25 should handle missing file", function (done) {
  921. var fileURL = SERVER + "/upload";
  922. var uploadFail = function (error) {
  923. expect(error.code).toBe(FileTransferError.FILE_NOT_FOUND_ERR);
  924. expect(error.http_status).not.toBe(401, "Ensure " + fileURL + " is in the white list");
  925. done();
  926. };
  927. var uploadWin = function() {
  928. unexpectedCallbacks.httpWin();
  929. done();
  930. };
  931. this.transfer.upload("does_not_exist.txt", fileURL, uploadWin, uploadFail);
  932. }, UPLOAD_TIMEOUT);
  933. it("filetransfer.spec.26 should handle bad file path", function (done) {
  934. var fileURL = SERVER + "/upload";
  935. var uploadFail = function (error) {
  936. expect(error.http_status).not.toBe(401, "Ensure " + fileURL + " is in the white list");
  937. done();
  938. };
  939. var uploadWin = function() {
  940. unexpectedCallbacks.httpWin();
  941. done();
  942. };
  943. this.transfer.upload("c:\\54321", fileURL, uploadWin, uploadFail);
  944. });
  945. it("filetransfer.spec.27 should be able to set custom headers", function (done) {
  946. var fileURL = SERVER + '/upload_echo_headers';
  947. var retryCount = 0;
  948. var self = this;
  949. var uploadWin = function (uploadResult) {
  950. expect(uploadResult.bytesSent).toBeGreaterThan(0);
  951. expect(uploadResult.responseCode).toBe(200);
  952. expect(uploadResult.response).toBeDefined();
  953. var responseHtml = decodeURIComponent(uploadResult.response);
  954. expect(responseHtml).toMatch(/CustomHeader1[\s\S]*CustomValue1/i);
  955. expect(responseHtml).toMatch(/CustomHeader2[\s\S]*CustomValue2[\s\S]*CustomValue3/i, "Should allow array values");
  956. done();
  957. };
  958. var uploadFail = function() {
  959. if (++retryCount >= RETRY_COUNT) {
  960. unexpectedCallbacks.httpFail();
  961. done();
  962. } else {
  963. console.log('retrying... ' + retryCount);
  964. setTimeout(function () {
  965. // NOTE: removing uploadOptions will cause Android to timeout
  966. self.transfer.upload(self.localFilePath, fileURL, uploadWin, uploadFail, self.uploadOptions);
  967. }, RETRY_INTERVAL);
  968. }
  969. };
  970. this.uploadOptions.headers = {
  971. "CustomHeader1": "CustomValue1",
  972. "CustomHeader2": ["CustomValue2", "CustomValue3"],
  973. };
  974. // http://whatheaders.com does not support Transfer-Encoding: chunked
  975. this.uploadOptions.chunkedMode = false;
  976. // NOTE: removing uploadOptions cause Android to timeout
  977. this.transfer.upload(this.localFilePath, fileURL, uploadWin, uploadFail, this.uploadOptions);
  978. }, UPLOAD_TIMEOUT * 11);
  979. it("filetransfer.spec.29 (compatibility) should be able to upload a file using local paths", function (done) {
  980. var fileURL = SERVER + "/upload";
  981. var specContext = this;
  982. var unsupported = function (response) {
  983. expectedCallbacks.unsupportedOperation(response);
  984. done();
  985. };
  986. var uploadWin = function (uploadResult) {
  987. verifyUpload(uploadResult, specContext);
  988. done();
  989. };
  990. var uploadFail = function() {
  991. unexpectedCallbacks.httpFail();
  992. done();
  993. };
  994. var internalFilePath;
  995. if (specContext.root.toInternalURL) {
  996. internalFilePath = specContext.root.toInternalURL() + specContext.fileName;
  997. } else {
  998. internalFilePath = specContext.localFilePath;
  999. }
  1000. // This is an undocumented interface to File which exists only for testing
  1001. // backwards compatibilty. By obtaining the raw filesystem path of the download
  1002. // location, we can pass that to transfer.download() to make sure that previously-stored
  1003. // paths are still valid.
  1004. cordova.exec(function (localPath) {
  1005. specContext.transfer.upload(localPath, fileURL, uploadWin, uploadFail, specContext.uploadOptions);
  1006. }, unsupported, "File", "_getLocalFilesystemPath", [internalFilePath]);
  1007. }, UPLOAD_TIMEOUT);
  1008. it("filetransfer.spec.31 should be able to upload a file using PUT method", function (done) {
  1009. var fileURL = SERVER + "/upload";
  1010. var specContext = this;
  1011. var uploadWin = function (uploadResult) {
  1012. verifyUpload(uploadResult, specContext);
  1013. if (cordova.platformId === "ios") {
  1014. expect(uploadResult.headers).toBeDefined("Expected headers to be defined.");
  1015. expect(uploadResult.headers["Content-Type"]).toBeDefined("Expected content-type header to be defined.");
  1016. }
  1017. done();
  1018. };
  1019. var uploadFail = function() {
  1020. unexpectedCallbacks.httpFail();
  1021. done();
  1022. };
  1023. specContext.uploadOptions.httpMethod = "PUT";
  1024. // NOTE: removing uploadOptions cause Android to timeout
  1025. specContext.transfer.upload(specContext.localFilePath, fileURL, uploadWin, uploadFail, specContext.uploadOptions);
  1026. }, UPLOAD_TIMEOUT);
  1027. it("filetransfer.spec.32 should be able to upload a file (non-multipart)", function (done) {
  1028. var fileURL = SERVER + "/upload";
  1029. var specContext = this;
  1030. var uploadWin = function (uploadResult) {
  1031. expect(uploadResult.bytesSent).toBeGreaterThan(0);
  1032. expect(uploadResult.responseCode).toBe(200);
  1033. expect(uploadResult.response).toBeDefined();
  1034. if (uploadResult.response) {
  1035. expect(uploadResult.response).toEqual(specContext.fileContents + "\n");
  1036. }
  1037. expect(specContext.transfer.onprogress).toHaveBeenCalled();
  1038. if (cordova.platformId === "ios") {
  1039. expect(uploadResult.headers).toBeDefined("Expected headers to be defined.");
  1040. expect(uploadResult.headers["Content-Type"]).toBeDefined("Expected content-type header to be defined.");
  1041. }
  1042. done();
  1043. };
  1044. var uploadFail = function() {
  1045. unexpectedCallbacks.httpFail();
  1046. done();
  1047. };
  1048. // Content-Type header disables multipart
  1049. specContext.uploadOptions.headers = {
  1050. "Content-Type": "text/plain"
  1051. };
  1052. // NOTE: removing uploadOptions cause Android to timeout
  1053. specContext.transfer.upload(specContext.localFilePath, fileURL, uploadWin, uploadFail, specContext.uploadOptions);
  1054. }, UPLOAD_TIMEOUT);
  1055. it("filetransfer.spec.34 should not delete a file on upload error", function (done) {
  1056. var fileURL = SERVER + "/upload";
  1057. var specContext = this;
  1058. var uploadFail = function (e) {
  1059. expect(e.code).toBe(FileTransferError.ABORT_ERR);
  1060. // check that the file is there
  1061. specContext.root.getFile(specContext.fileName, null, function(entry) {
  1062. expect(entry).toBeDefined();
  1063. // delay calling done() to wait for the bogus abort()
  1064. setTimeout(done, GRACE_TIME_DELTA * 2);
  1065. }, function(err) {
  1066. expect(err).not.toBeDefined(err && err.code);
  1067. done();
  1068. });
  1069. };
  1070. var uploadWin = function() {
  1071. unexpectedCallbacks.httpWin();
  1072. done();
  1073. };
  1074. var fileWin = function () {
  1075. expect(specContext.transfer.abort).not.toThrow();
  1076. // abort at the first onprogress event
  1077. specContext.transfer.onprogress = function (event) {
  1078. if (event.loaded > 0) {
  1079. specContext.transfer.abort();
  1080. }
  1081. };
  1082. // NOTE: removing uploadOptions cause Android to timeout
  1083. specContext.transfer.upload(specContext.localFilePath, fileURL, uploadWin, uploadFail, specContext.uploadOptions);
  1084. };
  1085. writeFile(specContext.root, specContext.fileName, new Array(100000).join("aborttest!"), fileWin, done);
  1086. }, UPLOAD_TIMEOUT);
  1087. it("filetransfer.spec.37 should handle non-UTF8 encoded upload response", function (done) {
  1088. // Only iOS is supported: https://issues.apache.org/jira/browse/CB-9840
  1089. if (!isIos) {
  1090. pending();
  1091. }
  1092. var fileURL = SERVER + '/upload_non_utf';
  1093. var specContext = this;
  1094. var uploadWin = function (uploadResult) {
  1095. verifyUpload(uploadResult, specContext);
  1096. var obj = null;
  1097. try {
  1098. obj = JSON.parse(uploadResult.response);
  1099. expect(obj.latin1Symbols).toBe(LATIN1_SYMBOLS);
  1100. } catch (e) {
  1101. expect(obj).not.toBeNull("returned data from server should be valid json");
  1102. }
  1103. if (cordova.platformId === 'ios') {
  1104. expect(uploadResult.headers).toBeDefined('Expected headers to be defined.');
  1105. expect(uploadResult.headers['Content-Type']).toBeDefined('Expected content-type header to be defined.');
  1106. }
  1107. done();
  1108. };
  1109. var uploadFail = function() {
  1110. unexpectedCallbacks.httpFail();
  1111. done();
  1112. };
  1113. // NOTE: removing uploadOptions cause Android to timeout
  1114. specContext.transfer.upload(specContext.localFilePath, fileURL, uploadWin, uploadFail, specContext.uploadOptions);
  1115. }, UPLOAD_TIMEOUT);
  1116. it("filetransfer.spec.38 should be able to upload a file using data: source uri", function (done) {
  1117. var fileURL = SERVER + "/upload";
  1118. var specContext = this;
  1119. var uploadWin = function (uploadResult) {
  1120. verifyUpload(uploadResult,specContext);
  1121. var obj = null;
  1122. try {
  1123. obj = JSON.parse(uploadResult.response);
  1124. expect(obj.files.file.size).toBe(DATA_URI_CONTENT_LENGTH);
  1125. } catch (e) {
  1126. expect(obj).not.toBeNull("returned data from server should be valid json");
  1127. }
  1128. if (cordova.platformId === "ios") {
  1129. expect(uploadResult.headers).toBeDefined("Expected headers to be defined.");
  1130. expect(uploadResult.headers["Content-Type"]).toBeDefined("Expected content-type header to be defined.");
  1131. }
  1132. done();
  1133. };
  1134. var dataUri = DATA_URI_PREFIX + DATA_URI_CONTENT;
  1135. // NOTE: removing uploadOptions cause Android to timeout
  1136. specContext.transfer.upload(dataUri, fileURL, uploadWin, function (err) {
  1137. console.error('err: ' + JSON.stringify(err));
  1138. expect(err).not.toBeDefined();
  1139. done();
  1140. }, specContext.uploadOptions);
  1141. }, UPLOAD_TIMEOUT);
  1142. it("filetransfer.spec.39 should be able to upload a file using data: source uri (non-multipart)", function (done) {
  1143. var fileURL = SERVER + "/upload";
  1144. var uploadWin = function (uploadResult) {
  1145. expect(uploadResult.responseCode).toBe(200);
  1146. expect(uploadResult.bytesSent).toBeGreaterThan(0);
  1147. if (cordova.platformId === "ios") {
  1148. expect(uploadResult.headers).toBeDefined("Expected headers to be defined.");
  1149. expect(uploadResult.headers["Content-Type"]).toBeDefined("Expected content-type header to be defined.");
  1150. }
  1151. done();
  1152. };
  1153. var uploadFail = function() {
  1154. unexpectedCallbacks.httpFail();
  1155. done();
  1156. };
  1157. // Content-Type header disables multipart
  1158. this.uploadOptions.headers = {
  1159. "Content-Type": "image/png"
  1160. };
  1161. var dataUri = DATA_URI_PREFIX + DATA_URI_CONTENT;
  1162. // NOTE: removing uploadOptions cause Android to timeout
  1163. this.transfer.upload(dataUri, fileURL, uploadWin, uploadFail, this.uploadOptions);
  1164. }, UPLOAD_TIMEOUT);
  1165. it("filetransfer.spec.40 should not fail to upload a file using data: source uri when the data is empty", function (done) {
  1166. var fileURL = SERVER + "/upload";
  1167. var dataUri = DATA_URI_PREFIX;
  1168. var uploadFail = function() {
  1169. unexpectedCallbacks.httpFail();
  1170. done();
  1171. };
  1172. // NOTE: removing uploadOptions cause Android to timeout
  1173. this.transfer.upload(dataUri, fileURL, done, uploadFail, this.uploadOptions);
  1174. }, UPLOAD_TIMEOUT);
  1175. it("filetransfer.spec.41 should not fail to upload a file using data: source uri when the data is empty (non-multipart)", function (done) {
  1176. if (isIos) {
  1177. // iOS does not support uploads of an empty file with __chunkedMode=true__ and `multipartMode=false`:
  1178. // request body will be empty in this case instead of 0\n\n.
  1179. pending();
  1180. }
  1181. var fileURL = SERVER + "/upload";
  1182. // Content-Type header disables multipart
  1183. this.uploadOptions.headers = {
  1184. "Content-Type": "image/png"
  1185. };
  1186. // turn off the onprogress handler
  1187. this.transfer.onprogress = function () { };
  1188. var dataUri = DATA_URI_PREFIX;
  1189. var uploadFail = function() {
  1190. unexpectedCallbacks.httpFail();
  1191. done();
  1192. };
  1193. // NOTE: removing uploadOptions cause Android to timeout
  1194. this.transfer.upload(dataUri, fileURL, done, uploadFail, this.uploadOptions);
  1195. }, UPLOAD_TIMEOUT);
  1196. describe("chunkedMode handling", function() {
  1197. var testChunkedModeWin = function (uploadResult, specContext) {
  1198. var multipartModeEnabled = !(specContext.uploadOptions.headers && specContext.uploadOptions.headers["Content-Type"]);
  1199. var obj = null;
  1200. try {
  1201. obj = JSON.parse(uploadResult.response);
  1202. if (specContext.uploadOptions.chunkedMode) {
  1203. if (!isIos) {
  1204. expect(obj["content-length"]).not.toBeDefined("Expected Content-Length not to be defined");
  1205. }
  1206. expect(obj["transfer-encoding"].toLowerCase()).toEqual("chunked");
  1207. } else {
  1208. expect(obj["content-length"]).toBeDefined("Expected Content-Length to be defined");
  1209. expect(obj["transfer-encoding"].toLowerCase()).not.toEqual("chunked");
  1210. }
  1211. if (multipartModeEnabled) {
  1212. expect(obj["content-type"].indexOf("multipart/form-data")).not.toBe(-1);
  1213. } else {
  1214. expect(obj["content-type"].indexOf("multipart/form-data")).toBe(-1);
  1215. }
  1216. } catch (e) {
  1217. expect(obj).not.toBeNull("returned data from server should be valid json");
  1218. }
  1219. };
  1220. var testChunkedModeBase = function(chunkedMode, multipart, done) {
  1221. var retryCount = 0;
  1222. var fileURL = SERVER + "/upload_echo_headers";
  1223. var specContext = this;
  1224. specContext.uploadOptions.chunkedMode = chunkedMode;
  1225. if (!multipart) {
  1226. // Content-Type header disables multipart
  1227. specContext.uploadOptions.headers = {
  1228. "Content-Type": "text/plain"
  1229. };
  1230. }
  1231. var uploadFail = function() {
  1232. if (++retryCount >= RETRY_COUNT) {
  1233. unexpectedCallbacks.httpFail();
  1234. done();
  1235. } else {
  1236. console.log('retrying... ' + retryCount);
  1237. setTimeout(function () {
  1238. // NOTE: removing uploadOptions will cause Android to timeout
  1239. specContext.transfer.upload(specContext.localFilePath, fileURL, function (uploadResult) {
  1240. testChunkedModeWin(uploadResult, specContext);
  1241. done();
  1242. }, uploadFail, specContext.uploadOptions);
  1243. }, RETRY_INTERVAL);
  1244. }
  1245. };
  1246. // turn off the onprogress handler
  1247. this.transfer.onprogress = function () {};
  1248. // NOTE: removing uploadOptions cause Android to timeout
  1249. specContext.transfer.upload(specContext.localFilePath, fileURL, function (uploadResult) {
  1250. testChunkedModeWin(uploadResult, specContext);
  1251. done();
  1252. }, uploadFail, specContext.uploadOptions);
  1253. };
  1254. it("filetransfer.spec.42 chunkedMode=false, multipart=false", function (done) {
  1255. testChunkedModeBase.call(this, false, false, done);
  1256. }, UPLOAD_TIMEOUT * 11);
  1257. it("filetransfer.spec.43 chunkedMode=true, multipart=false", function (done) {
  1258. if (isWindows) {
  1259. pending();
  1260. }
  1261. testChunkedModeBase.call(this, true, false, done);
  1262. }, UPLOAD_TIMEOUT * 11);
  1263. it("filetransfer.spec.44 chunkedMode=false, multipart=true", function (done) {
  1264. testChunkedModeBase.call(this, false, true, done);
  1265. }, UPLOAD_TIMEOUT * 11);
  1266. it("filetransfer.spec.45 chunkedMode=true, multipart=true", function (done) {
  1267. if (isWindows) {
  1268. pending();
  1269. }
  1270. testChunkedModeBase.call(this, true, true, done);
  1271. }, UPLOAD_TIMEOUT * 11);
  1272. });
  1273. });
  1274. });
  1275. });
  1276. };
  1277. /******************************************************************************/
  1278. /******************************************************************************/
  1279. /******************************************************************************/
  1280. exports.defineManualTests = function (contentEl, createActionButton) {
  1281. "use strict";
  1282. var imageURL = "http://apache.org/images/feather-small.gif";
  1283. var videoURL = "http://techslides.com/demos/sample-videos/small.mp4";
  1284. function clearResults() {
  1285. var results = document.getElementById("info");
  1286. results.innerHTML = "";
  1287. }
  1288. function downloadImg(source, urlFn, element, directory) {
  1289. var filename = source.substring(source.lastIndexOf("/") + 1);
  1290. filename = (directory || "") + filename;
  1291. function download(fileSystem) {
  1292. var ft = new FileTransfer();
  1293. console.log("Starting download");
  1294. var progress = document.getElementById("loadingStatus");
  1295. progress.value = 0;
  1296. ft.onprogress = function(progressEvent) {
  1297. if (progressEvent.lengthComputable) {
  1298. var currPercents = parseInt(100 * (progressEvent.loaded / progressEvent.total), 10);
  1299. if (currPercents > progress.value) {
  1300. progress.value = currPercents;
  1301. }
  1302. } else {
  1303. progress.value = null;
  1304. }
  1305. };
  1306. ft.download(source, fileSystem.root.toURL() + filename, function (entry) {
  1307. console.log("Download complete");
  1308. element.src = urlFn(entry);
  1309. console.log("Src URL is " + element.src);
  1310. console.log("Inserting element");
  1311. document.getElementById("info").appendChild(element);
  1312. }, function (e) { console.log("ERROR: ft.download " + e.code); });
  1313. }
  1314. console.log("Requesting filesystem");
  1315. clearResults();
  1316. window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (fileSystem) {
  1317. console.log("Checking for existing file");
  1318. if (typeof directory !== "undefined") {
  1319. console.log("Checking for existing directory.");
  1320. fileSystem.root.getDirectory(directory, {}, function (dirEntry) {
  1321. dirEntry.removeRecursively(function () {
  1322. download(fileSystem);
  1323. }, function () { console.log("ERROR: dirEntry.removeRecursively"); });
  1324. }, function () {
  1325. download(fileSystem);
  1326. });
  1327. } else {
  1328. fileSystem.root.getFile(filename, { create: false }, function (entry) {
  1329. console.log("Removing existing file");
  1330. entry.remove(function () {
  1331. download(fileSystem);
  1332. }, function () { console.log("ERROR: entry.remove"); });
  1333. }, function () {
  1334. download(fileSystem);
  1335. });
  1336. }
  1337. }, function () { console.log("ERROR: requestFileSystem"); });
  1338. }
  1339. /******************************************************************************/
  1340. var progress_tag = "<progress id=\"loadingStatus\" value=\"0\" max=\"100\" style=\"width: 100%;\"></progress>";
  1341. var file_transfer_tests = "<h2>Image File Transfer Tests</h2>" +
  1342. "<h3>The following tests should display an image of the Apache feather in the status box</h3>" +
  1343. "<div id=\"cdv_image\"></div>" +
  1344. "<div id=\"native_image\"></div>" +
  1345. "<div id=\"non-existent_dir\"></div>" +
  1346. "<h2>Video File Transfer Tests</h2>" +
  1347. "<h3>The following tests should display a video in the status box. The video should play when play is pressed</h3>" +
  1348. "<div id=\"cdv_video\"></div>" +
  1349. "<div id=\"native_video\"></div>";
  1350. contentEl.innerHTML = "<div id=\"info\"></div>" + "<br>" + progress_tag +
  1351. file_transfer_tests;
  1352. createActionButton("Download and display img (cdvfile)", function () {
  1353. downloadImg(imageURL, function (entry) { return entry.toInternalURL(); }, new Image());
  1354. }, "cdv_image");
  1355. createActionButton("Download and display img (native)", function () {
  1356. downloadImg(imageURL, function (entry) { return entry.toURL(); }, new Image());
  1357. }, "native_image");
  1358. createActionButton("Download to a non-existent dir (should work)", function () {
  1359. downloadImg(imageURL, function (entry) { return entry.toURL(); }, new Image(), "/nonExistentDirTest/");
  1360. }, "non-existent_dir");
  1361. createActionButton("Download and play video (cdvfile)", function () {
  1362. var videoElement = document.createElement("video");
  1363. videoElement.controls = "controls";
  1364. downloadImg(videoURL, function (entry) { return entry.toInternalURL(); }, videoElement);
  1365. }, "cdv_video");
  1366. createActionButton("Download and play video (native)", function () {
  1367. var videoElement = document.createElement("video");
  1368. videoElement.controls = "controls";
  1369. downloadImg(videoURL, function (entry) { return entry.toURL(); }, videoElement);
  1370. }, "native_video");
  1371. };