diff --git a/test/app-mocha-specs/test.js b/test/app-mocha-specs/test.js index 09049dc..6047e4c 100644 --- a/test/app-mocha-specs/test.js +++ b/test/app-mocha-specs/test.js @@ -8,7 +8,7 @@ const testDefinitions = require('../app-test-definitions'); const pkgjson = require('../../package.json'); describe('Advanced HTTP', function() { - let driver; + let driver = null; let allPassed = true; this.timeout(900000); @@ -37,10 +37,27 @@ describe('Advanced HTTP', function() { .then(text => text.match(/\d+:\ (.*)/)[1]) .should.eventually.become(testTitle, 'Test description is not matching!'); - const validateResult = text => driver - .elementById('resultTextarea') - .getAttribute('value') - .should.eventually.include(text); + const waitToBeFinished = timeout => new Promise((resolve, reject) => { + const timeoutTimestamp = Date.now() + timeout; + const checkIfFinished = () => driver + .elementById('statusInput') + .getValue() + .then(value => { + if (value === 'finished') { + resolve(); + } else if (Date.now() > timeoutTimestamp) { + reject('Test function timed out!'); + } else { + setTimeout(checkIfFinished, 500); + } + }); + + checkIfFinished(); + }); + + const validateResult = testDefinition => driver + .safeExecute('app.lastResult') + .then(result => testDefinition.validationFunc(driver, result)); const clickNext = () => driver .elementById('nextBtn') @@ -67,7 +84,8 @@ describe('Advanced HTTP', function() { return clickNext() .then(() => validateTestIndex(index)) .then(() => validateTestTitle(this.test.title)) - .then(() => validateResult(definition.expected)) + .then(() => waitToBeFinished(definition.timeout || 10000)) + .then(() => validateResult(definition)) }); }); }); diff --git a/test/app-template/www/css/index.css b/test/app-template/www/css/index.css index 37f86e1..cc47e1c 100644 --- a/test/app-template/www/css/index.css +++ b/test/app-template/www/css/index.css @@ -18,6 +18,10 @@ button, input, textarea { width: 100%; } +input { + text-align: center; +} + h1 { font-size: 12pt; text-align: center; diff --git a/test/app-template/www/index.html b/test/app-template/www/index.html index fa9273a..94da10f 100644 --- a/test/app-template/www/index.html +++ b/test/app-template/www/index.html @@ -9,6 +9,7 @@

Advanced HTTP test suite

+ diff --git a/test/app-template/www/js/index.js b/test/app-template/www/js/index.js index f001589..0bba4b9 100644 --- a/test/app-template/www/js/index.js +++ b/test/app-template/www/js/index.js @@ -1,6 +1,8 @@ const app = { testIndex: -1, + lastResult: null, + initialize: function() { document.getElementById('nextBtn').addEventListener('click', app.onNextBtnClick); }, @@ -12,11 +14,27 @@ const app = { }, reject: function(content) { + document.getElementById('statusInput').value = 'finished'; app.printResult('result - rejected', content); + + app.lastResult = { + type: 'rejected', + data: content + }; }, resolve: function(content) { + document.getElementById('statusInput').value = 'finished'; app.printResult('result - resolved', content); + + app.lastResult = { + type: 'resolved', + data: content + }; + }, + + getResult: function(cb) { + cb(app.lastResult); }, runTest: function(index) { @@ -24,6 +42,7 @@ const app = { const titleText = app.testIndex + ': ' + testDefinition.description; const expectedText = 'expected - ' + testDefinition.expected; + document.getElementById('statusInput').value = 'running'; document.getElementById('expectedTextarea').value = expectedText; document.getElementById('resultTextarea').value = ''; document.getElementById('descriptionLbl').innerText = titleText; @@ -31,6 +50,8 @@ const app = { }, onBeforeTest: function(testIndex, cb) { + app.lastResult = null; + if (hooks && hooks.onBeforeEachTest) { return hooks.onBeforeEachTest(function() { const testDefinition = tests[testIndex]; diff --git a/test/app-test-definitions.js b/test/app-test-definitions.js index 7cb8f2f..626aabc 100644 --- a/test/app-test-definitions.js +++ b/test/app-test-definitions.js @@ -12,53 +12,97 @@ const tests = [ { description: 'should reject self signed cert (GET)', expected: 'rejected: {"status":-1,"error":"cancelled"}', - func: function(resolve, reject) { cordova.plugin.http.get('https://self-signed.badssl.com/', {}, {}, resolve, reject); } + func: function(resolve, reject) { cordova.plugin.http.get('https://self-signed.badssl.com/', {}, {}, resolve, reject); }, + validationFunc: function(driver, result) { + result.type.should.be.equal('rejected'); + result.data.should.be.eql({ status: -1, error:'cancelled' }); + } },{ description: 'should reject self signed cert (PUT)', expected: 'rejected: {"status":-1,"error":"cancelled"}', - func: function(resolve, reject) { cordova.plugin.http.put('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); } + func: function(resolve, reject) { cordova.plugin.http.put('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); }, + validationFunc: function(driver, result) { + result.type.should.be.equal('rejected'); + result.data.should.be.eql({ status: -1, error:'cancelled' }); + } },{ description: 'should reject self signed cert (POST)', expected: 'rejected: {"status":-1,"error":"cancelled"}', - func: function(resolve, reject) { cordova.plugin.http.post('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); } + func: function(resolve, reject) { cordova.plugin.http.post('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); }, + validationFunc: function(driver, result) { + result.type.should.be.equal('rejected'); + result.data.should.be.eql({ status: -1, error:'cancelled' }); + } },{ description: 'should reject self signed cert (PATCH)', expected: 'rejected: {"status":-1,"error":"cancelled"}', - func: function(resolve, reject) { cordova.plugin.http.patch('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); } + func: function(resolve, reject) { cordova.plugin.http.patch('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); }, + validationFunc: function(driver, result) { + result.type.should.be.equal('rejected'); + result.data.should.be.eql({ status: -1, error:'cancelled' }); + } },{ description: 'should reject self signed cert (DELETE)', expected: 'rejected: {"status":-1,"error":"cancelled"}', - func: function(resolve, reject) { cordova.plugin.http.delete('https://self-signed.badssl.com/', {}, {}, resolve, reject); } + func: function(resolve, reject) { cordova.plugin.http.delete('https://self-signed.badssl.com/', {}, {}, resolve, reject); }, + validationFunc: function(driver, result) { + result.type.should.be.equal('rejected'); + result.data.should.be.eql({ status: -1, error:'cancelled' }); + } },{ description: 'should accept bad cert (GET)', - expected: 'resolved: {\"status\":200,', + expected: 'resolved: {\"status\":200, ...', before: helpers.acceptAllCerts, - func: function(resolve, reject) { cordova.plugin.http.get('https://self-signed.badssl.com/', {}, {}, resolve, reject); } + func: function(resolve, reject) { cordova.plugin.http.get('https://self-signed.badssl.com/', {}, {}, resolve, reject); }, + validationFunc: function(driver, result) { + result.type.should.be.equal('resolved'); + result.data.should.include({ status: 200 }); + } },{ description: 'should accept bad cert (PUT)', - expected: 'rejected: {\"status\":405,', // will be rejected because PUT is not allowed + expected: 'rejected: {\"status\":405, ... // will be rejected because PUT is not allowed', before: helpers.acceptAllCerts, - func: function(resolve, reject) { cordova.plugin.http.put('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); } + func: function(resolve, reject) { cordova.plugin.http.put('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); }, + validationFunc: function(driver, result) { + result.type.should.be.equal('rejected'); + result.data.should.include({ status: 405 }); + } },{ description: 'should accept bad cert (POST)', - expected: 'rejected: {\"status\":405,', // will be rejected because POST is not allowed + expected: 'rejected: {\"status\":405, ... // will be rejected because POST is not allowed', before: helpers.acceptAllCerts, - func: function(resolve, reject) { cordova.plugin.http.post('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); } + func: function(resolve, reject) { cordova.plugin.http.post('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); }, + validationFunc: function(driver, result) { + result.type.should.be.equal('rejected'); + result.data.should.include({ status: 405 }); + } },{ description: 'should accept bad cert (PATCH)', - expected: 'rejected: {\"status\":405,', // will be rejected because PATCH is not allowed + expected: 'rejected: {\"status\":405, ... // will be rejected because PATCH is not allowed', before: helpers.acceptAllCerts, - func: function(resolve, reject) { cordova.plugin.http.patch('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); } + func: function(resolve, reject) { cordova.plugin.http.patch('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); }, + validationFunc: function(driver, result) { + result.type.should.be.equal('rejected'); + result.data.should.include({ status: 405 }); + } },{ description: 'should accept bad cert (DELETE)', - expected: 'rejected: {\"status\":405,', // will be rejected because DELETE is not allowed + expected: 'rejected: {\"status\":405, ... // will be rejected because DELETE is not allowed', before: helpers.acceptAllCerts, - func: function(resolve, reject) { cordova.plugin.http.delete('https://self-signed.badssl.com/', {}, {}, resolve, reject); } + func: function(resolve, reject) { cordova.plugin.http.delete('https://self-signed.badssl.com/', {}, {}, resolve, reject); }, + validationFunc: function(driver, result) { + result.type.should.be.equal('rejected'); + result.data.should.include({ status: 405 }); + } },{ description: 'should fetch data from http://google.com/ (GET)', - expected: 'resolved: {\"status\":200,', + expected: 'resolved: {\"status\":200, ...', before: helpers.acceptAllCerts, - func: function(resolve, reject) { cordova.plugin.http.get('http://google.com/', {}, {}, resolve, reject); } + func: function(resolve, reject) { cordova.plugin.http.get('http://google.com/', {}, {}, resolve, reject); }, + validationFunc: function(driver, result) { + result.type.should.be.equal('resolved'); + result.data.should.include({ status: 200 }); + } } ];