mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-01-31 00:00:03 +08:00
added tests for abort
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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 () {
|
||||
|
||||
Reference in New Issue
Block a user