Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Last active January 2, 2018 07:50
Show Gist options
  • Save hansemannn/5021f0aaf2bcbe5ed054449912f3b92b to your computer and use it in GitHub Desktop.
Save hansemannn/5021f0aaf2bcbe5ed054449912f3b92b to your computer and use it in GitHub Desktop.
Unit-Tests for moving Ti.Network.HTTPClient to the NSURLSession API
var win = Ti.UI.createWindow({
title: 'HTTPClient Test - TIMOB-19154',
translucent: false
});
var nav = Ti.UI.iOS.createNavigationWindow({
window: win
});
var textColor = 'black';
var plainTemplate = {
childTemplates: [{
type: 'Ti.UI.Label', // Use a label
bindId: 'rowtitle', // Bind ID for this label
properties: { // Sets the Label.left property
left: 15,
top: 5,
color: textColor
}
},
{
type: 'Ti.UI.Label', // Use an image view
bindId: 'statusLabel', // Bind ID for this label
id: 'statusLabel',
properties: { // Sets the label property
text: 'Status: Not started',
left: 15,
bottom: 5,
font: {
fontSize: 12
},
color: textColor
}
},
{
type: 'Ti.UI.Button', // Use a button
bindId: 'button', // Bind ID for this button
properties: { // Sets several button properties
right: 20,
title: 'Start'
},
events: {
click: report
} // Binds a callback to the button's click event
}
]
};
function report(e) {
Ti.API.info(e.type);
Ti.API.info(e.itemIndex);
testFunction(e);
}
var listView = Ti.UI.createListView({
// Maps the plainTemplate object to the 'plain' style name
templates: {
'plain': plainTemplate
},
// Use the plain template, that is, the plainTemplate object defined earlier
// for all data list items in this list view
defaultItemTemplate: 'plain'
});
var tests = [
// case 0
{
name: 'Post - Image to Imgur',
description: 'Posts an Image to Imgur. Will return a reply with url link of image posted. You can see the progress of the Upload in the logcat/Ti.API.info'
},
// case 1
{
name: 'Get - Website',
description: 'Do a simple Get request to Appcelerator website. This will return the website.'
},
// case 2. Getting tests from https://github.com/appcelerator/titanium_mobile/blob/17dc8754653daaff170ea0c18f74218168a5633e/support/anvil/configSet/Resources/suites/network_httpclient.js
{
name: 'Get - Headers',
description: 'This test sets 2 headers and clears 1 header. This will do a get request at http://httpbin.org/headers that returns the headers used by the HTTPClient.'
},
// case 3
{
name: 'Get - Gzip-encoded',
description: 'Get - Returns gzip-encoded data that is readable. Compare this when you run on a terminal `curl http://httpbin.org/gzip`. The curl will reply human unreadble information.'
},
// case 4
{
name: 'Get - UTF-8 data',
description: 'Get - Returns page containing UTF-8 data. The data should be readable.'
},
// case 5
{
name: 'Get - Redirects',
description: 'Get - 302 Redirects to the Appcelerator URL. There will be a redirect to Appcelerator website.'
},
// case 6
{
name: 'Get - No Redirects',
description: 'HTTPClient autoRedirect is set to false. The link will redirect to Appcelerator website. The HTTPClient will not follow the redirect and hence return null.'
},
// case 7
{
name: 'Get - 3 Redirects',
description: 'Get - Follow redirects 3 times. This link will redirect 3 times before going to the Appcelerator website.'
},
// case 8
{
name: 'Get - XML',
description: 'Get - Returns XML. This will return an XML document. This will parse the document and show same information from the XML'
},
// case 9
{
name: 'Get - PNG',
description: 'Get - Returns PNG. This will download a PNG and show it in an Alert.'
},
// case 10
{
name: 'No Cert Validation',
description: 'Get - without Validating TIMOB-19196. HTTPClient is set to not validate certificates. The link is a website with a certificate that is not validated. This will return the website as it is set to non-validating.'
},
// case 11
{
name: 'Get - Cookies',
description: 'Get - Check the cookies being sent at http://httpbin.org/cookies'
},
// case 12
{
name: 'Add a cookie',
description: 'Add a cookie for http://httpbin.org/cookies to see. Used in the item above.'
},
// case 13
{
name: 'Remove all Cookies',
description: 'Remove all Cookies. Used in the item above.'
},
// case 14 [TIMOB-17891]
{
name: 'TLS v1.2',
description: 'Get - TLS Version, set to 1.2'
},
// case 15 [TIMOB-17891]
{
name: 'Default TLS',
description: 'Get - TLS Version, using Default'
},
// case 16
{
name: 'Basic Auth Success',
description: 'This is to test the success of Basic-Auth of the HTTPClient at http://httpbin.org/basic-auth/user/passwd'
},
// case 17
{
name: 'Basic Auth Failure',
description: 'This is to test the Basic-Auth of the HTTPClient at http://httpbin.org/basic-auth/user/passwd'
},
// case 18
{
name: 'Forward Geocoder',
description: 'Geolocation test forwardGeocoder. Geolocation module Apache HTTPClient has been replaced with HttpURLConnection. This needs to be tested too.'
},
// case 19
{
name: 'Reverse Geocoder',
description: 'Geolocation test reverseGeocoder. Geolocation module Apache HTTPClient has been replaced with HttpURLConnection. This needs to be tested too.'
},
// case 20
{
name: 'Image View',
description: 'ImageView uses Apache HTTPClient. This has been replaced with HttpURLConnection. Set a URL for ImageView to load an image from.'
},
// case 21
{
name: 'Save to Gallery',
description: 'Save an Image from HTTPClient to photo gallery'
},
// case 22 https://jira.appcelerator.org/browse/TIMOB-18861
{
name: 'SNI TIMOB-18861',
description: 'Test for SNI. https://jira.appcelerator.org/browse/TIMOB-18861 (DEVOPS-4514)'
},
// case 23
{
name: 'Get gzipped error',
description: 'Titanium Android: HTTPClient doesnt decompress gzipped data for error responses. https://jira.appcelerator.org/browse/TIMOB-17612'
},
// case 24
{
name: 'TIMOB-17585',
description: 'Crash was happening when getting Headers from HTTPClient. -> Android: HTTPClient: Invalid Status Codes returned in 3.3.0.GA SDK https://jira.appcelerator.org/browse/TIMOB-17585'
},
// case 25
{
name: 'TIMOB-15380',
description: 'Android: HTTPClient location not updated when page redirects https://jira.appcelerator.org/browse/TIMOB-15380'
},
// case 26
{
name: 'TIMOB-17573',
description: 'Android: Set Multiple Cookies in Request Header https://jira.appcelerator.org/browse/TIMOB-17573'
},
// case 27
{
name: 'TIMOB-17704',
description: 'XHR onreadystatechange called multiple times https://jira.appcelerator.org/browse/TIMOB-17704'
},
// case 28
{
name: 'Test HTTP-2 requests (iOS 9+)',
description: 'NSURLSession supports HTTP-2, so it should return the correct response indicating its support'
},
];
function testFunction(e) {
switch (e.itemIndex) {
case 0:
Titanium.Media.openPhotoGallery({
success: function(event) {
Ti.API.info("success! event: " + JSON.stringify(event));
var image = event.media;
var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(200000);
runningTest(e);
xhr.onload = function(response) {
Ti.API.info('IN ONLOAD ' + this.status + ' readyState ' + this.readyState);
Ti.API.info('Additional Information: ' + this.responseText);
setPass(e, true);
};
xhr.onsendstream = function(response) {
Ti.API.info('ONSENDSTREAM - PROGRESS: ' + response.progress);
};
xhr.onerror = function(response) {
setPass(e, false);
}
// open the client
xhr.open('POST', 'https://api.imgur.com/3/image');
// set the Authorization header
xhr.setRequestHeader('Authorization', 'Client-ID 6126cbc35f6f07c');
// send the data
xhr.send({
image: image,
title: 'check me out'
});
},
cancel: function() {
},
error: function(error) {
Ti.API.error(this.responseText);
},
allowEditing: true
});
break;
case 1:
runningTest(e);
var url = "http://www.appcelerator.com";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.info("getAllResponseHeaders text: " + this.getAllResponseHeaders());
Ti.API.info("Received text: " + this.responseText);
setPass(e, true);
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 2:
runningTest(e);
var url = "http://httpbin.org/headers";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
var jsonResponse = JSON.parse(this.responseText);
Ti.API.info("Received text: " + this.responseText);
if ((jsonResponse.headers.Adhocheader == 'notcleared') && (jsonResponse.headers.Clearedheader == undefined)) {
Ti.API.info("Adhocheader: " + jsonResponse.headers.Adhocheader);
setPass(e, true);
} else {
Ti.API.error("Received text: " + this.responseText);
setPass(e, false);
}
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
client.setRequestHeader('Adhocheader', 'notcleared');
client.setRequestHeader('Clearedheader', 'notcleared');
client.setRequestHeader('Clearedheader', null);
// Send the request.
client.send();
break;
case 3:
//Get - Returns gzip-encoded data
runningTest(e);
var url = "http://httpbin.org/gzip";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.info("Received text: " + this.responseText);
setPass(e, true);
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 4:
//Get - Returns page containing UTF-8 data.
runningTest(e);
var url = "http://httpbin.org/encoding/utf8";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.info("Received text: " + this.responseText);
setPass(e, true);
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 5:
//Get - 302 Redirects to the Appcelerator URL.
runningTest(e);
var url = "http://httpbin.org/redirect-to?url=http://www.appcelerator.com";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.info("Received text must have website: " + this.responseText);
if (this.responseText != null) {
setPass(e, true);
} else {
setPass(e, false);
}
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 6:
//Get - 302 No Redirects to the Appcelerator URL.
runningTest(e);
var url = "http://httpbin.org/redirect-to?url=http://www.appcelerator.com";
var client = Ti.Network.createHTTPClient({
autoRedirect: false,
// function called when the response data is available
onload: function(response) {
Ti.API.info("Received text must be null: " + this.responseText);
if (this.responseText == null) {
setPass(e, true);
} else {
setPass(e, false);
}
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 7:
//Get - Follow redirects 3 times
runningTest(e);
var url = "http://httpbin.org/redirect/3";
var client = Ti.Network.createHTTPClient({
autoRedirect: false,
// function called when the response data is available
onload: function(response) {
Ti.API.info("Received text must not be null: " + this.responseText);
if (this.responseText != null) {
setPass(e, true);
} else {
setPass(e, false);
}
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 8:
//Get - XML
runningTest(e);
var url = "http://httpbin.org/xml";
var client = Ti.Network.createHTTPClient({
autoRedirect: false,
// function called when the response data is available
onload: function(response) {
var node = this.getResponseXML().documentElement;
var stringResponse = node.attributes.length + ' attributes, ';
for (var i = 0; i < node.attributes.length; i++) {
var attribute = node.attributes.item(i);
if (attribute) {
stringResponse += attribute.nodeName + " = " + attribute.nodeValue + ', ';
} else {
stringResponse += 'FAIL: attribute undefined.';
}
}
Ti.API.info("Received xml: " + stringResponse);
setPass(e, true);
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 9:
//Get - PNG Image
runningTest(e);
var url = "http://httpbin.org/image/png";
var client = Ti.Network.createHTTPClient({
autoRedirect: false,
// function called when the response data is available
onload: function(response) {
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'mygraphic.png');
f.write(this.responseData); // write to the file
if (this.responseData != null) {
setPass(e, true);
} else {
setPass(e, false);
}
var imageView = Ti.UI.createImageView({
image: f.nativePath
});
var window = Titanium.UI.createWindow({
title: 'You must see an Image to pass',
});
window.addEventListener('click', function(e) {
window.close();
});
window.add(imageView);
window.open();
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 10:
//Get - Does not validate certs TIMOB-19196
runningTest(e);
var url = "https://www.pcwebshop.co.uk/";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.info("Received text: " + this.responseText);
setPass(e, true);
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
client.setValidatesSecureCertificate(false);
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 11:
//Get - Check cookies at http://httpbin.org/cookies
runningTest(e);
var url = "http://httpbin.org/cookies";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.info("Received text: " + this.responseText);
setPass(e, true);
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 12:
//Add a Cookie
var cookie = Titanium.Network.createCookie({
name: 'CookieOne',
value: 'CookieOneValue',
domain: 'httpbin.org',
path: '/cookies',
version: 0
})
Titanium.Network.addHTTPCookie(cookie);
setPass(e, cookie.isValid());
break;
case 13:
//Remove a Cookie
Titanium.Network.removeAllHTTPCookies();
var result = Ti.Network.getHTTPCookies('httpbin.org', '/cookies', 'CookieOne')
setPass(e, result.length == 0);
break;
case 14:
//Get - TLS Version, set to 1.2
runningTest(e);
var url = "https://www.howsmyssl.com/a/check";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.info("Received text: " + this.responseText);
var myData = JSON.parse(this.responseText);
Ti.API.info("Received tls: " + myData.tls_version);
if (myData.tls_version == "TLS 1.2") {
setPass(e, true);
} else {
setPass(e, false);
}
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
tlsVersion: Ti.Network.TLS_VERSION_1_0,
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 15:
//Get - TLS Version, using Default
runningTest(e);
var url = "https://www.howsmyssl.com/a/check";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.info("Received text: " + this.responseText);
var myData = JSON.parse(this.responseText);
//Version is dependent on Android device
Ti.API.info("Received tls: " + myData.tls_version);
setPass(e, true);
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 16:
//Get - TLS Version, using Default
runningTest(e);
var url = "http://httpbin.org/basic-auth/user/passwd";
// The username is set to 'user', and password is 'passwd'
// You can change this by changing the path of the link above
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.info("Received text: " + this.responseText);
setPass(e, true);
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
username: 'user',
password: 'passwd',
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 17:
runningTest(e);
var url = "http://httpbin.org/basic-auth/user/passwd";
// The username is set to 'user', and password is 'passwd'
// You can change this by changing the path of the link above
var client = Ti.Network.createHTTPClient({
// Error should call as we are passing wrong password
onload: function(response) {
Ti.API.info("Received text: " + this.responseText);
setPass(e, false);
},
//
onerror: function(response) {
Ti.API.error("Received text: " + this.responseText);
setPass(e, true);
},
username: 'user',
password: 'wrong_passwd',
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 18:
//forwardGeocoder
runningTest(e);
Titanium.Geolocation.forwardGeocoder("Singapore", function(evt) {
Ti.API.info(evt);
setPass(e, evt.success);
});
break;
case 19:
//reverseGeocoder
runningTest(e);
//Singapore long and lat
var longitude = 103.85166701272;
var latitude = 1.290606;
Titanium.Geolocation.reverseGeocoder(latitude, longitude, function(evt) {
setPass(e, evt.success);
});
break;
case 20:
var imageView = Ti.UI.createImageView({
top: 50,
backgroundColor: 'blue',
//image : 'https://dl.dropboxusercontent.com/u/255893/appcelerator@2x.png'
image: 'http://www.appcelerator.com.s3.amazonaws.com/web/home/platform/cd-mini-graphic.png'
});
var window = Titanium.UI.createWindow({
title: 'You must see an Image to pass',
});
window.addEventListener('open', function() {
var alertDialog = Ti.UI.createAlertDialog({
title: 'Do you see an image?',
buttonNames: ['Yes', 'No']
});
alertDialog.addEventListener('click', function(_e) {
setPass(e, _e.index == 0);
window.close();
});
setTimeout(function() {
alertDialog.show();
}, 1000);
})
window.add(imageView);
window.open();
break;
case 21:
//Somke test from:-
//https://github.com/appcelerator/titanium_mobile/blob/17dc8754653daaff170ea0c18f74218168a5633e/demos/SmokeTest/Resources/examples/photo_gallery_xhr.js
runningTest(e);
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Titanium.Media.saveToPhotoGallery(this.responseData);
Titanium.UI.createAlertDialog({
title: 'Photo Gallery',
message: 'Check your photo gallery for a titanium crystal bar'
}).show();
setPass(e, true);
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// open the client
client.open('GET', 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/db/Titan-crystal_bar.JPG/320px-Titan-crystal_bar.JPG');
// send the data
client.send();
break;
case 22:
//Get - TLS Version, using Default
runningTest(e);
var url = "https://www.appcelerator.com/";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.info("Received text: " + this.responseText);
setPass(e, true);
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 10000 // in milliseconds
});
client.validatesSecureCertificate = true;
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 23:
runningTest(e);
var url = "https://api.stackexchange.com/2.2/answers?order=desc&sort=activity&site=stackoverflo2w";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.error("Received text: " + this.responseText);
setPass(e, false);
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.debug(response.error);
Ti.API.error("Received text: " + this.responseText);
if (this.responseText != null) {
setPass(e, true);
} else {
setPass(e, false);
}
},
timeout: 10000 // in milliseconds
});
client.validatesSecureCertificate = true;
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 24:
runningTest(e);
var url = "http://news.google.co.in";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.log('Status = ' + this.getStatus());
if (this.getStatus() == 301) {
Ti.API.log('Redirect to ' + this.getResponseHeader('location'));
setPass(e, true);
} else {
Ti.API.log('This should have returned a 301 not a 200');
setPass(e, false);
}
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.debug(response.error);
Ti.API.error("Received text: " + this.responseText);
setPass(e, false);
},
timeout: 15000 // in milliseconds
});
client.autoRedirect = false;
client.allResponseHeaders = true;
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
break;
case 25:
//https://jira.appcelerator.org/browse/TIMOB-15380
runningTest(e);
var url = "http://www.nasa.gov/multimedia/nasatv/NTV-Public-IPS.m3u8";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
Ti.API.debug("event.js -> android video check -> received text: " + this.responseText);
Ti.API.debug("event.js -> url: " + url);
Ti.API.debug("event.js -> redirected location: " + this.location);
if (this.location == url) {
setPass(e, false);
} else {
setPass(e, true);
//Video player handles redirects too, hence it would not
//be a good example to show redirection for HTTPClient
}
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.debug(response.error);
Ti.API.error("Received text: " + this.responseText);
setPass(e, false);
},
timeout: 10000 // in milliseconds
});
// Prepare the connection.
client.open("HEAD", url);
// Send the request.
client.send();
break;
case 26:
runningTest(e);
var url = "http://httpbin.org/headers";
var client = Ti.Network.createHTTPClient({
// function called when the response data is available
onload: function(response) {
var jsonResponse = JSON.parse(this.responseText);
Ti.API.info("Received text: " + this.responseText);
if (jsonResponse.headers.Cookie == 'Context=123; sessionID=567; Auth_token=90') {
Ti.API.info("Cookie: " + jsonResponse.headers.Cookie);
setPass(e, true);
} else {
Ti.API.error("Received text: " + this.responseText);
setPass(e, false);
}
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response.error);
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
client.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
client.setRequestHeader('User-Agent', Titanium.userAgent);
client.setRequestHeader('Accept', 'text/plain; q=0.5');
client.setRequestHeader('Accept', 'text/html');
client.setRequestHeader('Cookie', 'Context=' + '123');
client.setRequestHeader('Cookie', 'sessionID=' + '567');
client.setRequestHeader('Cookie', 'Auth_token=' + '90');
// Send the request.
client.send();
break;
case 27:
runningTest(e);
var url = "http://speedtest.dal05.softlayer.com/downloads/test100.zip";
var client = Ti.Network.createHTTPClient({
onreadystatechange: function(response) {
if (response.readyState) {
Titanium.API.info('Event readystate: ' + response.readyState + ' Object readyState: ' + this.readyState);
if (response.readyState === this.DONE) {
Titanium.API.info('size ' + parseInt(this.getResponseHeader('Content-Length')));
setPass(e, true);
}
} else {
Titanium.API.info("readystate: " + this.readyState);
if (this.readyState === this.DONE) {
Titanium.API.info('size ' + parseInt(this.getResponseHeader('Content-Length')));
}
}
},
// function called when an error occurs, including a timeout
onerror: function(response) {
Ti.API.error(response)
setPass(e, false);
},
timeout: 5000 // in milliseconds
});
// Prepare the connection.
client.open("HEAD", url);
// Send the request.
client.send();
break;
case 28:
runningTest(e);
var url = 'https://http2.akamai.com/';
var client = Ti.Network.createHTTPClient({
onload: function(response) {
setPass(e, this.responseText.indexOf('You are using HTTP/2 right now!') !== -1);
},
onerror: function(response) {
setPass(e, false);
}
});
client.open("GET", url);
client.send();
break;
default:
Ti.API.error('Unhandled test-case! You clicked ' + e.itemIndex);
actionDone(e);
break;
}
}
function actionDone(e) {
var item = e.section.getItemAt(e.itemIndex);
//Ti.API.info(JSON.stringify(item));
item.statusLabel.text = "Action done"
item.properties.backgroundColor = 'white';
e.section.updateItemAt(e.itemIndex, item);
}
function runningTest(e) {
var item = e.section.getItemAt(e.itemIndex);
//Ti.API.info(JSON.stringify(item));
item.statusLabel.text = "Test Running"
item.properties.backgroundColor = 'orange';
e.section.updateItemAt(e.itemIndex, item);
}
function setPass(e, pass) {
var item = e.section.getItemAt(e.itemIndex);
//Ti.API.info(JSON.stringify(item));
if (pass) {
item.statusLabel.text = "Status Pass"
item.properties.backgroundColor = 'green';
e.section.updateItemAt(e.itemIndex, item);
} else {
item.statusLabel.text = "Status FAIL"
item.properties.backgroundColor = 'red';
e.section.updateItemAt(e.itemIndex, item);
}
}
var data = [];
for (var i = 0; i < tests.length; i++) {
data.push({
rowtitle: {
text: tests[i].name
},
// Sets the regular list data properties
statusLabel: {
text: 'Status: Not started'
},
properties: {
//This will change to green if pass, red if fail
backgroundColor: 'white',
itemId: 'row' + (i + 1),
height: 50
}
});
}
var section = Ti.UI.createListSection({
items: data
});
listView.sections = [section];
listView.addEventListener('itemclick', function(e) {
// Only respond to clicks on the label (rowtitle)
if (e.bindId == 'rowtitle') {
var item = e.section.getItemAt(e.itemIndex);
Ti.API.info(tests[e.itemIndex].description);
e.section.updateItemAt(e.itemIndex, item);
}
});
win.add(listView);
nav.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment