Skip to content

Commit

Permalink
[TIMOB-25663] add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drauggres committed Feb 2, 2018
1 parent 93f1b0f commit 8aaab57
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/Resources/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require('./es6.string.interpolation.test');
require('./ti.accelerometer.test');
require('./ti.analytics.test');
require('./ti.api.test');
require('./ti.android.httpresponsecache.test');
require('./ti.app.test');
require('./ti.app.ios.searchquery.test');
require('./ti.app.properties.test');
Expand Down
142 changes: 142 additions & 0 deletions tests/Resources/ti.android.httpresponsecache.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2011-Present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* global Ti */
/* eslint no-unused-expressions: "off" */
'use strict';
var should = require('./utilities/assertions');

describe('Titanium.Android.HttpResponseCache', function () {
this.timeout(6e4);
var cache = Ti.Android.HttpResponseCache;

function makeRequest(url, params, callback) {
var xhr = Ti.Network.createHTTPClient(params);
var attempts = 3;
xhr.setTimeout(6e4);

xhr.onload = function () {
callback(null);
};
xhr.onerror = function (e) {
if (attempts-- > 0) {
Ti.API.warn('failed, attempting to retry request...');
xhr.send();
} else {
Ti.API.warn('request failed');
callback(new Error('failed to "' + url + '": ' + e));
}
};

xhr.open('GET', url);
xhr.send();
}

afterEach(function () {
cache.flush();
cache.remove();
});

it.android('apiName', function () {
should(cache).have.a.readOnlyProperty('apiName').which.is.a.String;
should(cache.apiName).be.eql('Ti.Android.HttpResponseCache');
});

it.android('httpCachePath', function () {
should(cache).have.property('httpCachePath').which.is.a.String;
});

it.android('#getHttpCachePath', function () {
should(cache).have.property('getHttpCachePath').which.is.a.Function;
should(cache.getHttpCachePath()).be.a.String;
});

it.android('httpCacheSize', function () {
should(cache).have.property('httpCacheSize').which.is.a.Number;
});

it.android('#getHttpCacheSize', function () {
should(cache).have.property('getHttpCacheSize').which.is.a.Function;
should(cache.getHttpCacheSize()).be.a.Number;
});

it.android('#setHttpCachePath', function () {
should(cache).have.property('setHttpCachePath').which.is.a.Function;
});

it.android('#setHttpCacheSize', function () {
should(cache).have.property('setHttpCacheSize').which.is.a.Function;
});

it.android('#getHttpCacheSize', function () {
should(cache).have.property('getHttpCacheSize').which.is.a.Function;
should(cache.getHttpCacheSize()).be.a.Number;
});

it.android('#install', function () {
should(cache).have.property('install').which.is.a.Function;
});

it.android('#flush', function () {
should(cache).have.property('flush').which.is.a.Function;
});

it.android('#remove', function () {
should(cache).have.property('remove').which.is.a.Function;
});

it.android('#close', function () {
should(cache).have.property('close').which.is.a.Function;
});

it.android('#getHitCount', function () {
should(cache).have.property('getHitCount').which.is.a.Function;
});

it.android('#getNetworkCount', function () {
should(cache).have.property('getNetworkCount').which.is.a.Function;
});

it.android('#getRequestCount', function () {
should(cache).have.property('getRequestCount').which.is.a.Function;
});

it.android('#size', function () {
should(cache).have.property('size').which.is.a.Function;
});

it.android('defaultValues', function() {
should(cache.httpCachePath).be.eql('http');
should(cache.httpCacheSize).be.eql(25 * 1024 * 1024);
});

it.android('set custom properties 1', function() {
var path = 'path_1';
var size = 2 * 1024 * 1024;
cache.httpCachePath = path;
cache.httpCacheSize = size;
should(cache.httpCachePath).be.eql(path);
should(cache.httpCacheSize).be.eql(size);
});

it.android('set custom properties 2', function() {
var path = 'path_2';
var size = 3 * 1024 * 1024;
cache.setHttpCachePath(path);
cache.setHttpCacheSize(size);
should(cache.getHttpCachePath()).be.eql(path);
should(cache.getHttpCacheSize()).be.eql(size);
});

it.android('install', function() {
cache.setHttpCachePath('path_3');
cache.setHttpCacheSize(30 * 1024 * 1024);
var result = cache.install();
should(result).be.a.Boolean;
should(result).be.true;
});
});
82 changes: 82 additions & 0 deletions tests/Resources/ti.network.httpclient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,86 @@ describe('Titanium.Network.HTTPClient', function () {

xhr.send(form);
});

it.android('HttpResponseCache xhr.cache default', function(finish) {
var cache = Ti.Android.HttpResponseCache,
url = 'http://www.httpbin.org/etag/default' + Math.random(),
xhr = Ti.Network.createHTTPClient(),
attempts = 3;

cache.setHttpCachePath('path_4');
cache.setHttpCacheSize(30 * 1024 * 1024);
cache.install();

xhr.setTimeout(6e4);
xhr.onload = function () {
should(cache.getRequestCount()).be.eql(0);
should(cache.getHitCount()).be.eql(0);
finish()
};
xhr.onerror = function (e) {
if (attempts-- > 0) {
Ti.API.warn('failed, attempting to retry request...');
xhr.send();
} else {
Ti.API.warn('request failed');
finish(new Error('failed to "' + url + '": ' + e));
}
};

xhr.open('GET', url);
xhr.send();
});

it.android('HttpResponseCache xhr.cache custom', function(finish) {
var cache = Ti.Android.HttpResponseCache,
url = 'http://www.httpbin.org/etag/custom' + Math.random();

cache.setHttpCachePath('path_5');
cache.setHttpCacheSize(30 * 1024 * 1024);
cache.install();

function makeRequest(url, params, callback) {
var xhr = Ti.Network.createHTTPClient(params);
var attempts = 3;
xhr.setTimeout(6e4);

xhr.onload = function () {
callback(null);
};
xhr.onerror = function (e) {
if (attempts-- > 0) {
Ti.API.warn('failed, attempting to retry request...');
xhr.send();
} else {
Ti.API.warn('request failed');
callback(new Error('failed to "' + url + '": ' + e));
}
};

xhr.open('GET', url);
xhr.send();
}

makeRequest(url, {cache: true}, function(err) {
if (err) {
return finish(err)
}
should(cache.getRequestCount()).be.eql(1);
makeRequest(url, {cache: false}, function(err) {
if (err) {
return finish(err)
}
should(cache.getRequestCount()).be.eql(1);
makeRequest(url, {cache: true}, function(err) {
if (err) {
return finish(err)
}
should(cache.getRequestCount()).be.eql(2);
should(cache.getHitCount()).be.eql(1);
finish();
});
});
});
});
});

0 comments on commit 8aaab57

Please sign in to comment.