diff --git a/test/e2e-specs.js b/test/e2e-specs.js index 6c210d9..2ef3210 100644 --- a/test/e2e-specs.js +++ b/test/e2e-specs.js @@ -83,7 +83,8 @@ const helpers = { } result.type.should.be.equal(expected); - } + }, + getAbortDelay: function () { return 10; }, }; const messageFactory = { @@ -988,6 +989,102 @@ const tests = [ JSON.parse(result.data.data).cookies.should.be.eql({}); } }, + { + description: 'should be able to abort (POST)', + expected: 'rejected: {"status":-8, "error": "Request ...}', + before: helpers.setRawSerializer, + func: function (resolve, reject) { + helpers.getWithXhr(function (buffer) { + var reqId = cordova.plugin.http.post('http://httpbin.org/anything', buffer, {}, resolve, reject); + + setTimeout(function(){ + cordova.plugin.http.abort(reqId); + }, helpers.getAbortDelay()); + + }, './res/cordova_logo.png', 'arraybuffer'); + }, + validationFunc: function (driver, result) { + helpers.checkResult(result, 'rejected'); + result.data.status.should.be.equal(-8); + } + }, + { + description: 'should be able to abort (GET)', + expected: 'rejected: {"status":-8, "error": "Request ...}', + func: function (resolve, reject) { + var url = 'https://httpbin.org/image/jpeg'; + var options = { method: 'get', responseType: 'blob' }; + var success = function (response) { + resolve({ + isBlob: response.data.constructor === Blob, + type: response.data.type, + byteLength: response.data.size + }); + }; + + var reqId = cordova.plugin.http.sendRequest(url, options, success, reject); + setTimeout(function(){ + cordova.plugin.http.abort(reqId); + }, helpers.getAbortDelay()); + }, + validationFunc: function (driver, result) { + helpers.checkResult(result, 'rejected'); + result.data.status.should.be.equal(-8); + } + }, + { + description: 'should be able to abort downloading a file', + expected: 'rejected: {"status":-8, "error": "Request ...}', + func: function (resolve, reject) { + var sourceUrl = 'http://httpbin.org/xml'; + var targetPath = cordova.file.cacheDirectory + 'test.xml'; + + var reqId = cordova.plugin.http.downloadFile(sourceUrl, {}, {}, targetPath, function (entry) { + helpers.getWithXhr(function (content) { + resolve({ + sourceUrl: sourceUrl, + targetPath: targetPath, + fullPath: entry.fullPath, + name: entry.name, + content: content + }); + }, targetPath); + }, reject); + + setTimeout(function(){ + cordova.plugin.http.abort(reqId); + }, helpers.getAbortDelay()); + + }, + validationFunc: function (driver, result) { + helpers.checkResult(result, 'rejected'); + result.data.status.should.be.equal(-8); + } + }, + { + description: 'should be able to abort uploading a file', + expected: 'rejected: {"status":-8, "error": "Request ...}', + func: function (resolve, reject) { + var fileName = 'test-file.txt'; + var fileContent = 'I am a dummy file. I am used for testing purposes!'; + var sourcePath = cordova.file.cacheDirectory + fileName; + var targetUrl = 'http://httpbin.org/post'; + + helpers.writeToFile(function () { + + var reqId = cordova.plugin.http.uploadFile(targetUrl, {}, {}, sourcePath, fileName, resolve, reject); + + setTimeout(function(){ + cordova.plugin.http.abort(reqId); + }, helpers.getAbortDelay()); + + }, fileName, fileContent); + }, + validationFunc: function (driver, result) { + helpers.checkResult(result, 'rejected'); + result.data.status.should.be.equal(-8); + } + }, ]; if (typeof module !== 'undefined' && module.exports) { diff --git a/test/js-specs.js b/test/js-specs.js index 10c6236..384a846 100644 --- a/test/js-specs.js +++ b/test/js-specs.js @@ -676,6 +676,20 @@ describe('Common helpers', function () { }) }); }); + + describe('nextRequestId()', function () { + const helpers = require('../www/helpers')(null, null, null, null, null, null); + + it('returns number requestIds', () => { + helpers.nextRequestId().should.be.a('number'); + }); + + it('returns unique requestIds', () => { + const ids = [helpers.nextRequestId(), helpers.nextRequestId(), helpers.nextRequestId()]; + const set = new Set(ids); + ids.should.to.deep.equal(Array.from(set)); + }); + }); }); describe('Dependency Validator', function () {